Installation
composer require allyoullneed/livewire-forms
Publish the config (if needed):
php artisan vendor:publish --provider="Allyoullneed\LivewireForms\LivewireFormsServiceProvider"
First Use Case
contact_form).<livewire:statamic-form in="contact_form" />
Key Files to Review
config/livewire-forms.php (customization options like default validation rules).resources/views/vendor/livewire-forms/ (default Blade templates for overrides).Blueprint-Driven Forms
contact_form).TextInput, EmailInput).fields:
name:
type: text
required: true
message:
type: textarea
Livewire Integration
$rules in a custom component:
public $rules = [
'name' => 'required|min:3',
'message' => 'required|max:1000',
];
submit() in a custom Livewire class:
public function submit()
{
$validated = $this->validate();
// Process data (e.g., save to DB or send email)
session()->flash('success', 'Form submitted!');
$this->reset();
}
Component Customization
TextInput) by publishing views:
php artisan vendor:publish --tag=livewire-forms-views
resources/views/vendor/livewire-forms/fields/text.blade.php):
<input type="text" wire:model="state.{{ $field->handle }}" class="my-custom-class">
Dynamic Form Rendering
<livewire:statamic-form
in="contact_form"
:data="['default_name' => 'John Doe']"
/>
Multi-Step Forms
currentStep property to manage steps:
public $currentStep = 1;
public function nextStep() { $this->currentStep++; }
@if($currentStep === 1)
<livewire:statamic-form in="contact_form" fields="name,email" />
@endif
Reusable Form Logic
class BaseForm extends Component
{
public function save()
{
// Shared logic (e.g., logging, analytics)
}
}
class ContactForm extends BaseForm { ... }
Conditional Fields
conditional field settings in blueprints:
fields:
newsletter:
type: checkbox
conditional:
on: subscribe
fields: email
Blueprint Mismatches
handle matches the in attribute (case-sensitive).php artisan statamic:blueprints for typos.Livewire State Persistence
wire:preserve or store data in session:
protected $listeners = ['refreshForm' => 'loadData'];
public function loadData() { $this->state = session('form_data'); }
Validation Conflicts
'validation' => [
'use_blueprint_rules' => false,
],
submit():
$this->validate([
'name' => ['required', 'min:3'], // Override blueprint rules
]);
Asset Loading
@livewireStyles
@livewireScripts
Log Field Data
submit():
dd($this->state); // Inspect all submitted data
Check Livewire Events
protected $listeners = [
'form.submitted' => 'handleSubmission',
];
Blueprint Validation Errors
'debug' => true,
Custom Field Types
RatingField):
class RatingField extends Field
{
public function render()
{
return view('livewire-forms.fields.rating', ['field' => $this]);
}
}
config/livewire-forms.php:
'fields' => [
'rating' => \App\LivewireForms\Fields\RatingField::class,
],
Form Events
$this->dispatch('form-validated', data: $this->state);
window.addEventListener('form-validated', event => {
console.log(event.detail.data);
});
Statamic Entry Integration
use Statamic\Facades\Entry;
public function submit()
{
$validated = $this->validate();
Entry::create([
'collection' => 'contacts',
'data' => $validated,
]);
}
Use wire:ignore for Non-Interactive Elements
Improve performance by excluding static elements:
<div wire:ignore>
<p>Terms and conditions...</p>
</div>
Leverage Livewire’s wire:model.lazy
For non-immediate validation (e.g., on blur):
<input wire:model.lazy="state.name">
Cache Blueprint Data Reduce Statamic API calls by caching blueprints:
public function mount()
{
$this->blueprint = cache()->remember(
"livewire-forms:{$this->formHandle}",
now()->addHours(1),
fn() => Statamic::blueprints()->get($this->formHandle)
);
}
How can I help you explore Laravel packages today?