In the last blog Creating Dynamic Modals in Livewire 3.0, we explored creating dynamic modals in Livewire 3.0. Today, we’ll focus on building multi-step forms in Livewire 3.0. Multi-step forms are a great way to enhance user experience for long or complex forms by dividing them into smaller, manageable steps. With Livewire’s reactive features, you can easily create dynamic multi-step forms with real-time validation and state management.
Run the following command to create a new Livewire component:
php artisan make:livewire MultiStepForm
This will generate:
app/Livewire/MultiStepForm.php
– The PHP class for the component.
resources/views/livewire/multi-step-form.blade.php
– The Blade view for the component.
Open MultiStepForm.php
and define the properties and methods to handle the form steps:
'',
'email' => '',
'address' => '',
];
public function nextStep()
{
$this->validateStep();
$this->step++;
}
public function previousStep()
{
$this->step--;
}
public function submit()
{
$this->validate([
'formData.name' => 'required|min:3',
'formData.email' => 'required|email',
'formData.address' => 'required|min:5',
]);
// Save data or perform any action
session()->flash('success', 'Form submitted successfully!');
$this->reset();
}
private function validateStep()
{
if ($this->step === 1) {
$this->validate([
'formData.name' => 'required|min:3',
]);
} elseif ($this->step === 2) {
$this->validate([
'formData.email' => 'required|email',
]);
}
}
public function render()
{
return view('livewire.multi-step-form');
}
}
Step Management: Tracks the current step with the $step
property.
Validation: Validates fields dynamically based on the current step.
Form Reset: Resets the form after submission.
Open multi-step-form.blade.php
and create a dynamic form layout:
@if (session()->has('success'))
{{ session('success') }}
@endif
@if ($step === 1)
Step 1: Personal Details
@error('formData.name') {{ $message }} @enderror
@endif
@if ($step === 2)
Step 2: Contact Information
@error('formData.email') {{ $message }} @enderror
@endif
@if ($step === 3)
Step 3: Address
@error('formData.address') {{ $message }} @enderror
@endif
Conditional Rendering: Displays content based on the current step.
Error Handling: Displays validation errors for each field.
Dynamic Buttons: Navigates between steps or submits the form.
Use a CSS framework like Tailwind CSS or Bootstrap for better styling. For example:
Step {{ $step }}
@if ($step === 1)
@endif
@if ($step === 2)
@endif
@if ($step === 3)
@endif
@if ($step > 1)
@endif
@if ($step < 3)
@else
@endif
Show users their progress through the steps:
Store form data in the session to avoid losing it if the user refreshes the page:
public function mount()
{
$this->formData = session('formData', $this->formData);
}
public function updatedFormData()
{
session(['formData' => $this->formData]);
}
Dynamically adjust steps based on user input:
if ($this->formData['userType'] === 'business') {
$this->totalSteps = 4;
} else {
$this->totalSteps = 3;
}
By following these steps, your multi-step form will:
Guide users through a structured form submission process.
Dynamically validate inputs at each step.
Provide a seamless and interactive user experience.
Creating multi-step forms in Livewire 3.0.
Managing state and validation across steps.
Extending functionality with progress indicators and conditional steps.
Enhancing UX with styling and persistent form data.
410 C, Jaina Tower-1, District Center, Janak Puri New Delhi-110058, India.
© Copyright 2025 Wontonee. All Right Reserved.