Validate on suffixAction

I've got a form that is mostly visual, and only a couple of editing points, and doesn't have a general submit. I thought I could be clever where I wanted a field to be editable in that view, by having a suffix Action acting as a save button for that field.
FormField::telephone('rf_referee_telephone')
->columnSpan(1)
->live()
->suffixAction(
Action::make('saveTelephone')
->icon('heroicon-m-cloud-arrow-up')
->tooltip('Save Telephone')
->action(function ($record, $get) {

$record->rf_referee_telephone = $get('rf_referee_telephone');
$record->save();

Notification::make()
->title('Telephone Saved')
->success()
->send();
})
->visible(function ($record, $get) : bool {

return $record->rf_referee_telephone !== $get('rf_referee_telephone');

})

),
FormField::telephone('rf_referee_telephone')
->columnSpan(1)
->live()
->suffixAction(
Action::make('saveTelephone')
->icon('heroicon-m-cloud-arrow-up')
->tooltip('Save Telephone')
->action(function ($record, $get) {

$record->rf_referee_telephone = $get('rf_referee_telephone');
$record->save();

Notification::make()
->title('Telephone Saved')
->success()
->send();
})
->visible(function ($record, $get) : bool {

return $record->rf_referee_telephone !== $get('rf_referee_telephone');

})

),
Works fine, but there is no validation run. Is there a command I can have within the action to run the field validation prior to the save? Cheers
3 Replies
LeandroFerreira
use validateOnly
TextInput::make('rf_referee_telephone')
->required()
->minValue(10)
->suffixAction(
Action::make('saveTelephone')
->icon('heroicon-m-cloud-arrow-up')
->action(function (YourModel $record, Get $get, Page $livewire) {
$livewire->validateOnly('data.rf_referee_telephone');
...
$record->save();
...
})
)
TextInput::make('rf_referee_telephone')
->required()
->minValue(10)
->suffixAction(
Action::make('saveTelephone')
->icon('heroicon-m-cloud-arrow-up')
->action(function (YourModel $record, Get $get, Page $livewire) {
$livewire->validateOnly('data.rf_referee_telephone');
...
$record->save();
...
})
)
Matthew
MatthewOP3d ago
Thanks for this. I tried this, but it didn't work initially. I thought it may be something to do with the form being in a modal, and sure enough the prefix on the wire:model is not 'data'. So: $livewire->validateOnly('mountedTableActionsData.0.rf_referee_telephone'); works...but can I rely on that always targetting the right element?
LeandroFerreira
ahh ok, you are using an action. Use this
$livewire->validateOnly($livewire->getMountedActionForm()->getStatePath().'.rf_referee_telephone');
$livewire->validateOnly($livewire->getMountedActionForm()->getStatePath().'.rf_referee_telephone');

Did you find this page helpful?