Tutorial: Multi-step Wizard
Build a three-step ticket wizard: subject → body → confirm and submit. The server guarantees steps can't be skipped, and when a step fails validation the screen jumps back to it on its own.
1. Decide where state lives first
| Option | Skip protection | Survives reload | Round-trips |
|---|---|---|---|
| A. Stateful Linker, one call per step | ✅ enforced by the server | ❌ | One per step |
| B. Client state, submit once at the end | Validated together at the end | ❌ | One |
| C. DB draft row | ✅ | ✅ | One per step |
This tutorial builds A and shows B at the end. /ticket/index has both, so you can compare their Network panels side by side.
2. The component
namespace App\Http\LinkerComponents;
use App\Attributes\Linker\Computable;
use App\Attributes\Linker\RequiresAuth;
use App\Attributes\Linker\Validate;
use App\Models\Ticket;
use App\Support\Linker\LinkerResponse;
#[RequiresAuth]
class TicketWizardLinker extends LinkerComponent
{
public static string $alias = 'ticketWizard';
public function mount(array $params = []): void
{
$this->set('step', 1);
}
#[Computable]
#[Validate(['subject' => 'required|string|max:120'])]
public function saveSubject(array $args): LinkerResponse
{
$this->set('subject', $args['subject'])->set('step', 2);
return $this->ok(['step' => 2])->call('form.focus', ['target' => '#ticket-body']);
}
#[Computable]
#[Validate(['body' => 'required|string|max:5000'])]
public function saveBody(array $args): LinkerResponse
{
if ($this->get('step') < 2) { // server-side guard: the browser can't change step
return LinkerResponse::policy();
}
$this->set('body', $args['body'])->set('step', 3);
return $this->ok([
'step' => 3,
'previewSubject' => $this->get('subject'),
]);
}
#[Computable] // no #[Validate]: takes no arguments, just "pressed"
public function submit(array $args): LinkerResponse
{
if ($this->get('step') !== 3) {
return LinkerResponse::policy();
}
$ticket = Ticket::create([
'user_id' => auth()->id(),
'subject' => $this->get('subject'),
'body' => $this->get('body'),
]);
return $this->ok(['step' => 4])
->call('ui.toast', ['level' => 'success', 'message' => "Created #{$ticket->id}"]);
}
#[Computable]
#[Validate(['to' => 'required|integer|min:1|max:2'])]
public function back(array $args): LinkerResponse
{
$to = min($args['to'], $this->get('step')); // only backwards
$this->set('step', $to);
return $this->ok(['step' => $to]);
}
}Don't forget to register it: 'ticketWizard' => TicketWizardLinker::class (make:linker writes this for you).
3. The template
<div @linker('ticketWizard')
@renderObject('ticketWizard', ['state:read', 'state:write'])
data-state-init="step=1">
<ol class="stepper">
<li data-state-is-class="step=1:is-current; step>1:is-done">Subject</li>
<li data-state-is-class="step=2:is-current; step>2:is-done">Body</li>
<li data-state-is-class="step=3:is-current; step>3:is-done">Confirm</li>
</ol>
<form data-state-is="step=1">
<input name="subject" data-state-bind="subject">
<button type="button" data-linker-call="saveSubject" data-linker-args="form">Next</button>
</form>
<form data-state-is="step=2">
<textarea id="ticket-body" name="body" data-state-bind="body"></textarea>
<button type="button" data-linker-call="back" data-linker-args='{"to":1}'>Back</button>
<button type="button" data-linker-call="saveBody" data-linker-args="form">Next</button>
</form>
<section data-state-is="step=3">
<p>Subject: <strong data-state-text="previewSubject"></strong></p>
<button type="button" data-linker-call="back" data-linker-args='{"to":2}'>Back</button>
<button type="button" data-linker-call="submit" data-linker-args='{}'
data-linker-confirm="Submit now?">Submit</button>
</section>
<p data-state-is="step=4">Done!</p>
</div>How it works:
- Each method returns
['step' => n]. Thedatais written to the client state keyticketWizard:step, anddata-state-is/-is-classswitch the screen automatically. - The client's
stepis only a display. If the user changes it to 3 in DevTools, the screen jumps there, but on "Submit" the server'sstepis still 1, so it returnspolicy. data-state-bindkeeps what was typed when going back a step.
4. Verify
- Leave the subject empty and click Next → the error appears next to the field and you stay on step one.
- In the console run
document.querySelector('[data-state-is="step=3"]').hidden = false, then click Submit →policy. - Reload after submitting → back to step one (a new instance). That's by design, not a bug.
5. Variant: submit once (option B)
Every step switches on the client only, with one submit at the end. Fewer round-trips, but validation happens all at once, and the failing field may be on a hidden step, so catch brings the screen back:
<div @linker('ticketQuick') @renderObject('ticketQuickWizard', ['state:read', 'state:write']) data-state-init="step=1">
<form>
<fieldset data-state-is="step=1">
<input name="subject">
<button type="button" data-state-set="step=2">Next</button>
</fieldset>
<fieldset data-state-is="step=2">
<textarea name="body"></textarea>
<button type="button" data-linker-call="submit" data-linker-args="form">Submit</button>
</fieldset>
</form>
</div>#[Computable]
#[Validate([ // undeclared arguments are never received, so accept them all as nullable first
'subject' => 'nullable|string|max:120',
'body' => 'nullable|string|max:5000',
])]
public function submit(array $args): LinkerResponse
{
$steps = [
1 => ['subject' => 'required|string|max:120'],
2 => ['body' => 'required|string|max:5000'],
];
foreach ($steps as $step => $rules) {
$validator = Validator::make($args, $rules);
if ($validator->fails()) {
// catch, not call: the whole call failed, so only catch / always run.
// Without moving step back, the error is painted on a hidden panel and the user sees "nothing happened".
return LinkerResponse::validation($validator->errors()->toArray())
->catch('state.set', ['scope' => 'ticketQuickWizard', 'key' => 'step', 'value' => $step]);
}
}
Ticket::create([...]);
return $this->ok()->call('ui.toast', ['level' => 'success', 'message' => 'Created']);
}If you wrote required directly in #[Validate], a failure would return validation before the method ran, leaving no chance to catch and return to the right step. That's why per-step validation lives inside the method. Real example: TicketLinker::quickSubmit().
Which option to choose, and when a round-trip is worth it, is reasoned out in full in the repo's docs/Linker/decision-wizard-state.md.