Form Validation inside Action

Hi, I'm trying to add a simple form validation inside my 'delete action'. I want to show a random number and the user to introduce the same to delete the record. I've tried changing the modal view, but it justs keeps dee¡leting everything no matter the form: < Tables\Actions\Action::make('Delete') ->label('Eliminar') ->color('danger') ->modalContent(fn (Pedido $record,): View => view( 'filament.actions.deletePedido', ['record' => $record, 'aleatorio' => rand(1000, 9999)], )) ->modalSubmitActionLabel('Eliminar') ->modalCancelActionLabel('Cancelar') ->color('danger') ->action(function (Pedido $record, Request $request) { $userNum = $request->input('userNum'); error_log($userNum."el del susuario"); $aleatorio = $request->input('aleatorio'); error_log($aleatorio."el del susuario"); if ($userNum === $aleatorio) { error_log("Se borra"); } else{ error_log(" No Se borra"); } })>
10 Replies
Tally
Tally2mo ago
maybe this can nudge you in the correct direction
Actions\DeleteAction::make()
->mountUsing(function (Form $form) {
$form->fill(['secret' => strval(rand(1000, 9999))]);
})
->form([
Forms\Components\Placeholder::make('secret')
->content(fn(Get $get) => 'Please fill in this code to delete this record ' . $get('secret') ),
Forms\Components\Hidden::make('secret'),
Forms\Components\TextInput::make('code')
->label('Secret code')
->required(),
])
->action(function (array $data, Model $record): void {
if ($data['code']!==$data['secret'])
{
throw ValidationException::withMessages([
'mountedActionsData.0.code' => 'The secret code is invalid.',
]);
}

$record->delete();

$this->redirect($this->getResource()::getUrl('index'));
})
Actions\DeleteAction::make()
->mountUsing(function (Form $form) {
$form->fill(['secret' => strval(rand(1000, 9999))]);
})
->form([
Forms\Components\Placeholder::make('secret')
->content(fn(Get $get) => 'Please fill in this code to delete this record ' . $get('secret') ),
Forms\Components\Hidden::make('secret'),
Forms\Components\TextInput::make('code')
->label('Secret code')
->required(),
])
->action(function (array $data, Model $record): void {
if ($data['code']!==$data['secret'])
{
throw ValidationException::withMessages([
'mountedActionsData.0.code' => 'The secret code is invalid.',
]);
}

$record->delete();

$this->redirect($this->getResource()::getUrl('index'));
})
Barbaracrlp
Barbaracrlp2mo ago
Looks good, Bu in the end I used the form of the actions, then a new Blade, and rewrite the action() But thanks a lot
Tally
Tally2mo ago
🙂 in the example you can see that you didn't need a seperate view to make it happen 😉
Barbaracrlp
Barbaracrlp2mo ago
I've tried it but the ValidationException is not showing anything. It justs does nothing if the secretcode is wrong
Tally
Tally2mo ago
you could change it to mountedActionsData.code should display a message
Barbaracrlp
Barbaracrlp2mo ago
still nothing , it justs stops charging and that's it
Tally
Tally2mo ago
strange I'm seeing this
No description
Tally
Tally2mo ago
are you using my example or did you put the ValidationException in your code... in that case using 'code' => 'message' would be enough
Barbaracrlp
Barbaracrlp2mo ago
which import of Actions are you using? I'm using Tables\Actions\DeleteAction That's it, in the Table Actions does not works but in normal Filament Actions it does. don't know why
Tally
Tally2mo ago
🙂