How to validate a single field when using a form action?

Is there a way to validate a single field inside a form action? I tried to validate the field keywords, but it didn't work:
TextInput::make('keywords')
->required()
->maxLength(255)
,
Filament\Forms\Components\Actions::make([
Filament\Forms\Components\Actions\Action::make('generate')
->action(function () {
$this->validateOnly('keywords');

})
TextInput::make('keywords')
->required()
->maxLength(255)
,
Filament\Forms\Components\Actions::make([
Filament\Forms\Components\Actions\Action::make('generate')
->action(function () {
$this->validateOnly('keywords');

})
15 Replies
LeandroFerreira
LeandroFerreira10mo ago
->action(function ($livewire) {
$livewire->validateOnly('data.keywords');
})
->action(function ($livewire) {
$livewire->validateOnly('data.keywords');
})
Alex Manase
Alex Manase10mo ago
It still didn't work
LeandroFerreira
LeandroFerreira10mo ago
Are you using admin panel?
Alex Manase
Alex Manase10mo ago
inside
$table->actions([
// my actions
])
$table->actions([
// my actions
])
Like this:
LeandroFerreira
LeandroFerreira10mo ago
ok, and where is the form? are you using statePath?
Alex Manase
Alex Manase10mo ago
ok, and where is the form?
Inside an action of a table function
Action::make('edit')->form([
// like this
])
Action::make('edit')->form([
// like this
])
are you using statePath?
No.
you can also use a suffixAction
I tried, but I need to use an icon in order for the suffix action to work. I just want a button with the label "Submit". Also I think there should be a more suitable way to validate specified fields. What if someone wants a min-form inside the form?
LeandroFerreira
LeandroFerreira10mo ago
hum.. If you have an advanced form, I think you can use a modal content/custom view to render a livewire component
Tables\Actions\Action::make('customAction')
->modalContent(view('custom-form'))
Tables\Actions\Action::make('customAction')
->modalContent(view('custom-form'))
adding a form to a Livewire component class https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component#adding-the-form
Alex Manase
Alex Manase10mo ago
this seems to be too much configuration for having a button that validates a single input. I will open tomorrow a feature request/bug report on Github Thank you for your help!
LeandroFerreira
LeandroFerreira10mo ago
why bug?
Alex Manase
Alex Manase10mo ago
Because there is a method validateOnly inside InteractsWithForm and it doesn't seem to work. Anyway, I have to spend more time in order to be sure if this is a bug, a new feature or if it's not documented enough.
LeandroFerreira
LeandroFerreira10mo ago
ok validateOnly is a livewire feature
Alex Manase
Alex Manase10mo ago
I'll check on this Actually, I just fixed it. All I had to do was
$this->validateOnly('mountedTableActionsData.0.keywords')
$this->validateOnly('mountedTableActionsData.0.keywords')
I solved my issue. Thank you @leandro_ferreira for your help!