F
Filament11mo ago
mamamia

How to mimic create action?

So i have 2 button, the default create form action and save as draft, i want save as draft is 99% same with the normal one, the only difference is the status how do i do that? i've tried this but i have to manually give validation how do i mimic the original one?
protected function getFormActions(): array
{
return array_merge(
[
Action::make('Save as draft')
->action('saveAsDraft')
],
parent::getFormActions()
);
}

public function saveAsDraft($data) {
$data = $this->form->getRawState();
$data["status_project"] = ProjectStatus::DRAFT;
static::getModel()::create($data);
}
protected function getFormActions(): array
{
return array_merge(
[
Action::make('Save as draft')
->action('saveAsDraft')
],
parent::getFormActions()
);
}

public function saveAsDraft($data) {
$data = $this->form->getRawState();
$data["status_project"] = ProjectStatus::DRAFT;
static::getModel()::create($data);
}
10 Replies
toeknee
toeknee11mo ago
Save as draft would skip validation as if it's a draft it's unfinished, if it's unfinished you will have required fields. I would suggest for what you want, you make the input fields live and ensure they are validated on the input.
Matthew
Matthew11mo ago
I reccomend somethng like this:
CreateAction::make()
->mutateFormDataUsing(function (array $data): array {
$data['user_id'] = auth()->id();

return $data;
})
CreateAction::make()
->mutateFormDataUsing(function (array $data): array {
$data['user_id'] = auth()->id();

return $data;
})
mamamia
mamamiaOP11mo ago
i tried that but somehow i didnt get status_project from the action, do you know why? here is my code
Action::make('create')
->label('Create')
->mutateFormDataUsing(function (array $data){
$data['status_project'] = ProjectStatus::SUBMISSION;
return $data;
})
->action(function(){
$this->authorizeAccess();
try {
$data = $this->form->getState();
// somehow there is not status_project inside $data
} catch (Halt $exception) {
return;
}

})
->after(function(){
$record = $this->record;
$history = [
"project_id" => $record->id,
"start_date" => $record->start_date,
"end_date" => $record->end_date,
"responded_at" => null,
"responded_by" => null,
"submission_at" => date("Y-m-d H:i:s"),
"submission_by" => auth()->user()->id,
"submission_description" => $record->submission_description,
"submission_type" => ProjectHistorySubmission::NEW,
"status" => ProjectStatus::SUBMISSION,
];
ProjectHistory::create($history);
}),
Action::make('create')
->label('Create')
->mutateFormDataUsing(function (array $data){
$data['status_project'] = ProjectStatus::SUBMISSION;
return $data;
})
->action(function(){
$this->authorizeAccess();
try {
$data = $this->form->getState();
// somehow there is not status_project inside $data
} catch (Halt $exception) {
return;
}

})
->after(function(){
$record = $this->record;
$history = [
"project_id" => $record->id,
"start_date" => $record->start_date,
"end_date" => $record->end_date,
"responded_at" => null,
"responded_by" => null,
"submission_at" => date("Y-m-d H:i:s"),
"submission_by" => auth()->user()->id,
"submission_description" => $record->submission_description,
"submission_type" => ProjectHistorySubmission::NEW,
"status" => ProjectStatus::SUBMISSION,
];
ProjectHistory::create($history);
}),
toeknee
toeknee11mo ago
$data only contains the data from a submitting form.... There is no form in the above action? if you DD on the action, do you have the form data?
mamamia
mamamiaOP11mo ago
thank you for the answer, so i have the form but there is no status_project field, because i want status_project field automatically generated. how do i do that?
toeknee
toeknee11mo ago
Just set it as per the above and ensure it is fillable in the model
mamamia
mamamiaOP11mo ago
but when i dd the $data the "status_project" is not appearing, im thinking to do this one instead of mutateForm but idk if this is the best practice or nah
Action::make('create')
->label('Create')
->action(function(){
$this->authorizeAccess();

try {
$data = $this->form->getState();
$data['status_project'] = ProjectStatus::SUBMISSION;

$this->record = $this->handleRecordCreation($data);

} catch (Halt $exception) {
return;
}

})
->after(function(){
$record = $this->record;
$history = [
"project_id" => $record->id,
"start_date" => $record->start_date,
"end_date" => $record->end_date,
"responded_at" => null,
"responded_by" => null,
"submission_at" => date("Y-m-d H:i:s"),
"submission_by" => auth()->user()->id,
"submission_description" => $record->submission_description,
"submission_type" => ProjectHistorySubmission::NEW,
"status" => ProjectStatus::SUBMISSION,
];
ProjectHistory::create($history);
})
Action::make('create')
->label('Create')
->action(function(){
$this->authorizeAccess();

try {
$data = $this->form->getState();
$data['status_project'] = ProjectStatus::SUBMISSION;

$this->record = $this->handleRecordCreation($data);

} catch (Halt $exception) {
return;
}

})
->after(function(){
$record = $this->record;
$history = [
"project_id" => $record->id,
"start_date" => $record->start_date,
"end_date" => $record->end_date,
"responded_at" => null,
"responded_by" => null,
"submission_at" => date("Y-m-d H:i:s"),
"submission_by" => auth()->user()->id,
"submission_description" => $record->submission_description,
"submission_type" => ProjectHistorySubmission::NEW,
"status" => ProjectStatus::SUBMISSION,
];
ProjectHistory::create($history);
})
toeknee
toeknee11mo ago
You need to put it in the mutateBeforeCreate if using the creation action, otherwise it's fine
mamamia
mamamiaOP11mo ago
okay thank you so muchh for answering my question
Matthew
Matthew11mo ago
First of all, you need a record which means Edit Page in this case If you are creating, then there isnt really a record to begin with
Want results from more Discord servers?
Add your server