Setting up the Livewire component
First, generate a new Livewire component:Adding the form
There are 5 main tasks when adding a form to a Livewire component class. Each one is essential:- Implement the
HasSchemasinterface and use theInteractsWithSchemastrait. - Define a public Livewire property to store your form’s data. In our example, we’ll call this
$data, but you can call it whatever you want. - Add a
form()method, which is where you configure the form. Add the form’s schema, and tell Filament to store the form data in the$dataproperty (usingstatePath('data')). - Initialize the form with
$this->form->fill()inmount(). This is imperative for every form that you build, even if it doesn’t have any initial data. - Define a method to handle the form submission. In our example, we’ll call this
create(), but you can call it whatever you want. Inside that method, you can validate and get the form’s data using$this->form->getState(). It’s important that you use this method instead of accessing the$this->dataproperty directly, because the form’s data needs to be validated and transformed into a useful format before being returned.
<x-filament-actions::modals /> is used to render form component action modals. The code can be put anywhere outside the <form> element, as long as it’s within the Livewire component.components():
Submit the form with data, and you’ll see the form’s data dumped to the screen. You can save the data to a model instead of dumping it:
filament/forms also includes the following packages:filament/actionsfilament/schemasfilament/support
HasActions interface and use the InteractsWithActions trait on your Livewire component class.If you are using any other Filament components in your form, make sure to install and integrate the corresponding package as well.Initializing the form with data
To fill the form with data, just pass that data to the$this->form->fill() method. For example, if you’re editing an existing post, you might do something like this:
$this->form->fill() method instead of assigning the data directly to the $this->data property. This is because the post’s data needs to be internally transformed into a useful format before being stored.
Setting a form model
Giving the$form access to a model is useful for a few reasons:
- It allows fields within that form to load information from that model. For example, select fields can load their options from the database automatically.
- The form can load and save the model’s relationship data automatically. For example, you have an Edit Post form, with a Repeater which manages comments associated with that post. Filament will automatically load the comments for that post when you call
$this->form->fill([...]), and save them back to the relationship when you call$this->form->getState(). - Validation rules like
exists()andunique()can automatically retrieve the database table name from the model.
$form->model() method:
Passing the form model after the form has been submitted
In some cases, the form’s model is not available until the form has been submitted. For example, in a Create Post form, the post does not exist until the form has been submitted. Therefore, you can’t pass it in to$form->model(). However, you can pass a model class instead:
saveRelationships() to save the relationships to it:
Saving form data to individual properties
In all of our previous examples, we’ve been saving the form’s data to the public$data property on the Livewire component. However, you can save the data to individual properties instead. For example, if you have a form with a title field, you can save the form’s data to the $title property instead. To do this, don’t pass a statePath() to the form at all. Ensure that all of your fields have their own public properties on the class.
Using multiple forms
Many forms can be defined using theInteractsWithSchemas trait. Each of the forms should use a method with the same name:
form. For example, to fill the post form, you can use $this->editPostForm->fill([...]), or to get the data from the comment form you can use $this->createCommentForm->getState().
You’ll notice that each form has its own unique statePath(). Each form will write its state to a different array on your Livewire component, so it’s important to define the public properties, $postData and $commentData in this example.
Resetting a form’s data
You can reset a form back to its default data at any time by calling$this->form->fill(). For example, you may wish to clear the contents of a form every time it’s submitted:
Generating form Livewire components with the CLI
It’s advised that you learn how to set up a Livewire component with forms manually, but once you are confident, you can use the CLI to generate a form for you.app/Livewire/RegistrationForm.php component, which you can customize.
Generating a form for an Eloquent model
Filament is also able to generate forms for a specific Eloquent model. These are more powerful, as they will automatically save the data in the form for you, and ensure the form fields are properly configured to access that model. When generating a form with themake:livewire-form command, it will ask for the name of the model:
Generating an edit form for an Eloquent record
By default, passing a model to themake:livewire-form command will result in a form that creates a new record in your database. If you pass the --edit flag to the command, it will generate an edit form for a specific record. This will automatically fill the form with the data from the record, and save the data back to the model when the form is submitted.
Automatically generating form schemas
Filament is also able to guess which form fields you want in the schema, based on the model’s database columns. You can use the--generate flag when generating your form: