Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hynixo's avatar
Level 1

Filament 3 - Issue with form validation (Validating a form before performing form's Modal Action)

How do I validate a form before displaying a modal using a form Action? ->before, ->action and ->mountUsing doesn't work.

The goal here is to grab the current form's state without saving it to the database, generate a Pdf from it and output it into the modal content so that the user can see the generated pdf preview right away, without downloading it - and it's already working.

The problem is that the form isn't validated before the action is performed. How do I fix that?

I tried adding these things but it doesn't work, the form still isn't validated before the modal is shown. The code inside isn't even being ran before the modal is shown:

->before(function ($livewire) {
                            $livewire->validate();
                        })
->action(function ($livewire) {
                            $livewire->validate();
                        })
->mountUsing(function ($livewire) {
                            $livewire->validate();
                        })

Here's the code that's inside of the resource's form:

Forms\Components\Actions::make([
Forms\Components\Actions\Action::make('preview')
                        ->label('Preview')
                        ->icon('heroicon-m-eye')
->modalContent(function (Get $get) {
                            $pdf_transformer = new FormDataToPdf($get);
                            $pdf = $pdf_transformer->getPdf();

                            return view('modalpreview', [
                                'pdf_content' => base64_encode($pdf->output())
                            ]);
                        })
                        ->modalSubmitAction(false)
                        ->modalCancelAction(false)
])

I tried:

Forms\Components\Actions::make([
Forms\Components\Actions\Action::make('preview')
                        ->label('Preview')
                        ->icon('heroicon-m-eye')
->before(function ($livewire) {
                            $livewire->validate();
                        })
->modalContent(function (Get $get) {
                            $pdf_transformer = new FormDataToPdf($get);
                            $pdf = $pdf_transformer->getPdf();

                            return view('modalpreview', [
                                'pdf_content' => base64_encode($pdf->output())
                            ]);
                        })
                        ->modalSubmitAction(false)
                        ->modalCancelAction(false)
])
0 likes
3 replies
Hynixo's avatar
Level 1

Interestingly, if we remove the whole ->modalContent thing, the form is validating correctly.

beefsoupclover's avatar

@hynixo, did you find a solution to this? I got this to work based on putting closures in the modal methods. Maybe you have something better?

1 like

Please or to participate in this conversation.