F
Filament7mo ago
Ben

Custom Save Button for Create/Edit with Disable on FileUpload

Hello Filament Friends, I have a problem with implementing different Save Buttons. My process should be such that the Save buttons determine whether an entry is active or not (published). So, my implementation has several buttons below the form, including "Save" and "Activate and Save." I'm not sure if my implementation is good. Depending on which button is pressed, I adjust data, like this:
Action::make('saveAndActivate')
->label('Activate & Save')
->color('success')
->action(fn () => $this->saveAndActivate());
...
public function saveAndActivate() {
$this->data['active'] = 1;
return $this->save();
}
Action::make('saveAndActivate')
->label('Activate & Save')
->color('success')
->action(fn () => $this->saveAndActivate());
...
public function saveAndActivate() {
$this->data['active'] = 1;
return $this->save();
}
Well, that works, but if uploads are not yet completed, one can click these buttons and the uploads are lost. In the case of the standard Save button, submission is blocked until completion. And that's what i want to achieve. I tried to use the standard Actions and change them, but it didn't work. Like this...
$this->getSaveFormAction()
->mutateFormDataUsing(function (array $data): array {
// this is not executed
$data['active'] = true;
return $data;
})
->label('Activate & Save')
$this->getSaveFormAction()
->mutateFormDataUsing(function (array $data): array {
// this is not executed
$data['active'] = true;
return $data;
})
->label('Activate & Save')
I know that there are methods to modify data before saving, but I'm not sure how to determine which button was pressed at that point. And honestly, I haven't fully understood what the "submit('save')" in the standard action exactly means. I still need to figure that out. Thank you!
2 Replies
Ben
BenOP7mo ago
Alternatively, it would help me if I knew how to manipulate the data when clicking on the standard create/save action, but it must be considered that I want to include these buttons twice, once with changing 'active' to true and once without. I also found out that in the template, it checks if it is a submit button, and these buttons receive an indicator when a file upload is in progress. vendor/filament/support/resources/views/components/button/index.blade.php $hasFormProcessingLoadingIndicator = $type === 'submit' && filled($form);
_andypeacock
_andypeacock5mo ago
Hi @Ben Did you get this working? I've got the same question - how do you know which button is pressed. Well waddyaknow. Chatgpt helped, on the second attempt: Add the action:
$actions[] = \Filament\Actions\Action::make('save_continue_editing')
->color('danger')
->label(__('Save and Publish'))
->action('saveAndPublish');
$actions[] = \Filament\Actions\Action::make('save_continue_editing')
->color('danger')
->label(__('Save and Publish'))
->action('saveAndPublish');
Add the saveAndPublish function
public function saveAndPublish()
{
$data = $this->form->getState();
$data['status'] = 'published';
$record = static::getModel()::create($data);

Notification::make()
->success()
->title('Content published')
->send();

$this->redirect($this->getResource()::getUrl('index'));
}
public function saveAndPublish()
{
$data = $this->form->getState();
$data['status'] = 'published';
$record = static::getModel()::create($data);

Notification::make()
->success()
->title('Content published')
->send();

$this->redirect($this->getResource()::getUrl('index'));
}
And for the edit page:
public function saveAndPublish()
{

$record = $this->getRecord();
$record['status'] = 'published';
$record->save();

Notification::make()
->success()
->title('Content published')
->send();

$this->redirect($this->getResource()::getUrl('index'));
}
public function saveAndPublish()
{

$record = $this->getRecord();
$record['status'] = 'published';
$record->save();

Notification::make()
->success()
->title('Content published')
->send();

$this->redirect($this->getResource()::getUrl('index'));
}
Want results from more Discord servers?
Add your server