Using closure customization
All configuration methods for fields and layout components accept closures as parameters instead of hardcoded values:$state parameter:
$component parameter:
$livewire parameter:
$record parameter:
$get parameter:
$get, you may also set the value of another field from within a callback, using a closure $set parameter:
create, edit or view, use the $context parameter:
Outside of the admin panel, you can set a form’s context by defining a getFormContext() method on your Livewire component.
Callbacks are evaluated using Laravel’s app()->call() under the hood, so you are able to combine multiple parameters in any order:
Reloading the form when a field is updated
By default, forms are only reloaded when they are validated or submitted. You may allow a form to be reloaded when a field is changed, by using thereactive() method on that field:
Dependant fields / components
You may use the techniques described in the closure customization section to build completely dependent fields and components, with full control over customization based on the values of other fields in your form. For example, you can build dependant select inputs:
Sometimes, you may wish to conditionally hide any form component based on the value of a field. You may do this with a
hidden() method:
reactive(), to ensure the Livewire component is reloaded when they are updated.
Field lifecycle
Hydration
Hydration is the process which fill fields with data. It runs when you call the form’sfill() method. You may customize what happens after a field is hydrated using the afterStateHydrated().
In this example, the name field will always be hydrated with the correctly capitalized name:
Updates
You may use theafterStateUpdated() method to customize what happens after a field is updated.
In this example, the slug field is updated with the slug version of the title field automatically:
Dehydration
Dehydration is the process which gets data from fields, and transforms it. It runs when you call the form’sgetState() method. You may customize how the state is dehydrated from the form by returning the transformed state from the dehydrateStateUsing() callback.
In this example, the name field will always be dehydrated with the correctly capitalized name:
false to dehydrated().
In this example, the passwordConfirmation field will not be present in the array returned from getData():
Using form events
Forms can dispatch and listen to events, which allow the frontend and backend to communicate. These events can be dispatched in a component view, and then listened to by a component’s class.Dispatching events
To dispatch a form event, call thedispatchFormEvent() Livewire method with the event name:
Listening to events
You may register listeners for form events by calling theregisterListeners() method when your component is setUp():