Adding a save button to EditPage header

So I'm trying to move the save button from the form actions to the header actions, and snatching it out of there with parent::getFormActions() and putting that into my header actions does show the button, but the button itself doesn't work, as it's no longer within the form tag. How would I go about adding a custom action that actually saves the form currently being viewed in an edit page? It's a standard EditRecord page we're talking about here.
protected function getHeaderActions(): array
{
$actions = parent::getFormActions();

return [
Arr::first($actions)->action('save'),
Action::make('optimized')
->label('Mark as Optimized')
->requiresConfirmation()
->action(function (Task $task) {
$task->calculateNewDeadline();
$this->refreshFormData([
'status',
'urgency',
]);
}),
Arr::last($actions)
];
}
protected function getHeaderActions(): array
{
$actions = parent::getFormActions();

return [
Arr::first($actions)->action('save'),
Action::make('optimized')
->label('Mark as Optimized')
->requiresConfirmation()
->action(function (Task $task) {
$task->calculateNewDeadline();
$this->refreshFormData([
'status',
'urgency',
]);
}),
Arr::last($actions)
];
}
This is how I grabbed the buttons from the edit form. The cancel button works fine, but that's just a link so it should. Only the save button isn't working as intended. It's the Arr::first($actions) one in that list.
Solution:
For others trying to replicate adding a save button to the header: ```php Action::make('save') ->label('Save changes')...
Jump to solution
7 Replies
Sauravisus
SauravisusOP13mo ago
Right in here @Dan Harrin. At least that's the current incarnation. Does it have to be a wholly custom action to work, maybe? Ah, yep. It does. Can't just slap the action onto the OG save action, gotta be a new one.
Solution
Sauravisus
Sauravisus13mo ago
For others trying to replicate adding a save button to the header:
Action::make('save')
->label('Save changes')
->action('save'),
Action::make('save')
->label('Save changes')
->action('save'),
Toss that into your getHeaderActions array, and you're off to the races. 👍
Dan Harrin
Dan Harrin13mo ago
$this->getSaveFormAction()
->submit(null)
->action('save')
$this->getSaveFormAction()
->submit(null)
->action('save')
Melody
Melody11mo ago
how would I hide the default save/cancel buttons on the form after creating them in the header?
Tobias Platen
Tobias Platen11mo ago
protected function getFormActions(): array
{
return [];
}
protected function getFormActions(): array
{
return [];
}
awcodes
awcodes11mo ago
if they aren't in the form actions array they won't get registered, iirc. You also will probably loose the ability to submit the form with 'enter' if they are removed too.
Tobias Platen
Tobias Platen11mo ago
You are right. I played around a bit and found a solution.
protected function getFormActions(): array
{
return [
$this->getSaveFormAction()->extraAttributes(['style' => 'display:none']),
$this->getCancelFormAction()->extraAttributes(['style' => 'display:none']),
];
}

protected function getHeaderActions(): array
{
return [
$this->getSaveFormAction()
->submit(null)
->action('save'),
$this->getCancelFormAction(),
];
}
protected function getFormActions(): array
{
return [
$this->getSaveFormAction()->extraAttributes(['style' => 'display:none']),
$this->getCancelFormAction()->extraAttributes(['style' => 'display:none']),
];
}

protected function getHeaderActions(): array
{
return [
$this->getSaveFormAction()
->submit(null)
->action('save'),
$this->getCancelFormAction(),
];
}
Want results from more Discord servers?
Add your server