F
Filament2y ago
Abi

Additional validations on the `action` method for Standalone action

When using a standalone action, is it possible to do additional form validations inside the action() method. The reason why I need to do this is because some of the validations are based on the argument passed to the Action (https://filamentphp.com/docs/3.x/actions/adding-an-action-to-a-livewire-component#passing-action-arguments) on the blade file.
5 Replies
Abi
AbiOP2y ago
because the arguments are not available inside the rules() method of the component basically want to do the following based on the blade code below
function addToCart(): Action{
return Action::make('addToCart')
->form([
//All the schema fields go here with validations
])->action(function($data, $arguments){
//Do additional validations based on the value received from the arguments
if($this->isAlreadyInTheCart($arguments['product_id'])) {
//show an error message on the form
}
})
}
function addToCart(): Action{
return Action::make('addToCart')
->form([
//All the schema fields go here with validations
])->action(function($data, $arguments){
//Do additional validations based on the value received from the arguments
if($this->isAlreadyInTheCart($arguments['product_id'])) {
//show an error message on the form
}
})
}
{{ ($this->addToCart)(['productId'=>$product->id])}}
{{ ($this->addToCart)(['productId'=>$product->id])}}
Patrick Boivin
I've never tried in this context but I think you may be able to use Livewire's validate() with a set of custom rules:
->action(function ($livewire) {
$livewire->validate([
// custom rules...
]);
})
->action(function ($livewire) {
$livewire->validate([
// custom rules...
]);
})
Johan
Johan17mo ago
@Abishek you found a fix for this?
Abi
AbiOP17mo ago
the validate() method worked, but I do show a small modal with the errors to be more intutive as the number of error messages on my UI is more.
Johan
Johan17mo ago
Are you willing to share some code of it? I have a similar use case. I use the modal to upload an Excel file which I process with Maatwerk/Laravel-Excel I want to show the errors triggered by that import

Did you find this page helpful?