Modal in getHeaderActions keeps submitting parent form

I might just be bad at RTFM'ing, but whatever I do my submit action in my modal in getHeaderActions keeps trying to submit the actual resource form. The action:
Action::make('save_as_template')
->label('Save as Template')
->icon('heroicon-o-document-duplicate')
->modal()
->modalHeading('Save Form as Template')
->modalSubmitActionLabel('Save')
->form([
TextInput::make('template_name')
->label('Template Name')
->required(),
])
->action(function (array $data, $livewire): void {
$resourceName = Str::of(static::$resource)
->afterLast('\\')
->before('Resource')
->snake()
->toString();

FormTemplate::create([
'name' => $data['template_name'],
'resource' => $resourceName,
'data' => $livewire->form->getState(),
]);

$livewire->notify('success', 'Template saved');
})
->closeModalByClickingAway(false),
Action::make('save_as_template')
->label('Save as Template')
->icon('heroicon-o-document-duplicate')
->modal()
->modalHeading('Save Form as Template')
->modalSubmitActionLabel('Save')
->form([
TextInput::make('template_name')
->label('Template Name')
->required(),
])
->action(function (array $data, $livewire): void {
$resourceName = Str::of(static::$resource)
->afterLast('\\')
->before('Resource')
->snake()
->toString();

FormTemplate::create([
'name' => $data['template_name'],
'resource' => $resourceName,
'data' => $livewire->form->getState(),
]);

$livewire->notify('success', 'Template saved');
})
->closeModalByClickingAway(false),
Anything to stop this from happening? I just need the modal form to be submitted, in this case save the form template, not the whole resource with it.
15 Replies
Никола Стојков
I simply do not understand what's happening even $data is empty
awcodes
awcodes6d ago
Is it actually submitting the form or just showing validation errors? You’re referencing the livewire component in your action which is actually the page and not the actions’ form.
Никола Стојков
validation errors about required fields, I assumed it's trying to submit it
awcodes
awcodes6d ago
$livewire->form->getState() runs the validations for the form before save. I think that’s what you’re seeing.
Никола Стојков
question is why is it running validations on the resource form not on the action form
awcodes
awcodes6d ago
Because your referencing $livewire which is the page not the action form.
Никола Стојков
nvm I'm tired I read like an idiot
awcodes
awcodes6d ago
Actions are not livewire components, pages are. In an ->action() $data is the action’s form data.
Никола Стојков
so assuming that form->getState() tries to validate the whole form, anything that can grab all the current field values off the form?
awcodes
awcodes6d ago
If you need the pages form data in your action without the validation then $livewire->form->getRawState() But in an action you are responsible for validation, etc. think of it as a controller.
Никола Стојков
I just need a dump of all the current populated fileds, I don't care for validation I'll check with getRawState in a sec
awcodes
awcodes6d ago
One thing to be aware of is that get raw state doesn’t persist fileuploads if you have them, just another thing that ->getState() handles other than validation.
Никола Стојков
Since the form is huge and lots of the fields are boilerplate for certain "clients", the idea was to make a template that you can save once then pre-fill the form getRawState seems to be working perfectly for this so far thank you ❤️ I assume I can just fillForm this data back, but that's a help question for tomorrow 😄
awcodes
awcodes6d ago
Glad it’s working for you. But yea, get some sleep.
Никола Стојков
fyi yeah it just worked with fillForm with no additional magic needed, thanks again

Did you find this page helpful?