Preparing your Livewire component
Implement theHasForms interface and use the InteractsWithForms trait:
getFormSchema() method:
getFormSchema().
Initializing forms
You must initialize forms when the Livewire component is first loaded. This is done with thefill() form method, often called in the mount() method of the Livewire component.
For your fields to hold data, they should have a corresponding property on your Livewire component, just as in Livewire normally.
afterStateHydrated() method.
Filling forms with data
To fill a form with data, call thefill() method on your form, and pass an array of data to fill it with:
Getting data from forms
To get all form data in an array, call thegetState() method on your form.
getState() is run:
- Validation rules are checked, and if errors are present, the form is not submitted.
- Any pending file uploads are stored permanently in the filesystem.
- Field relationships, if they are defined, are saved.
You may transform the value that is dehydrated from a field using the dehydrateStateUsing() method.
Registering a model
You may register a model to a form. The form builder is able to use this model to unlock DX features, such as:- Automatically retrieving the database table name when using database validation rules like
existsandunique. - Automatically attaching relationships to the model when the form is saved, when using fields such as the
Select,Repeater,SpatieMediaLibraryFileUpload, orSpatieTagsInput.
getFormModel() method:
model() method:
Registering a model class
In some cases, the model instance is not available until the form has been submitted. For example, in a form that creates a post, the post model instance cannot be passed to the form before it has been submitted. You may receive some of the same benefits of registering a model by registering its class instead:Field relationships
Some fields, such as theSelect, Repeater, SpatieMediaLibraryFileUpload, or SpatieTagsInput are able to interact with model relationships.
For example, Select can be used to attach multiple records to a BelongstoMany relationship. When registering a model to the form or component, these relationships will be automatically saved to the pivot table when getState() is called:
Saving field relationships manually
In some cases, the model instance is not available until the form has been submitted. For example, in a form that creates a post, the post model instance cannot be passed to the form before it has been submitted. In this case, you will pass the model class instead, but any field relationships will need to be saved manually after. In this situation, you may call themodel() and saveRelationships() methods on the form after the instance has been created:
Saving relationships when the field is hidden
By default, relationships will only be saved if the field is visible. For example, if you have aRepeater field that is only visible on a certain condition, the relationships will not be saved when it is hidden.
This might cause unexpected behaviour if you still want to save the relationship, even when the field is hidden. To force relationships to be saved, you may call the saveRelationshipsWhenHidden() method on the form component:
Using multiple forms
By default, theInteractsWithForms trait only handles one form per Livewire component. To change this, you can override the getForms() method to return more than one form, each with a unique name:
Scoping form data to an array property
You may scope the entire form data to a single array property on your Livewire component. This will allow you to avoid having to define a new property for each field:$data array.