How to prefill data after "createAnother" action?

Hey, just wanna maintain some inputs prefilled when I hit the "createAnother" button. I'm trying some Action methods but can't get it :/
protected function getCreateAnotherFormAction(): Action
{
return Action::make('createAnother')
->label(__('createAnother'))
->dispatchSuccessRedirect()
->beforeFormFilled(function () {
$this->data['employee_id'] = request('employee_id');
$this->data['project_id'] = request('project_id');
$this->data['date'] = request('date');
$this->data['imputation_type'] = request('imputation_type');
})
->action('createAnother')
->keyBindings(['mod+shift+s'])
->color('secondary');
}
protected function getCreateAnotherFormAction(): Action
{
return Action::make('createAnother')
->label(__('createAnother'))
->dispatchSuccessRedirect()
->beforeFormFilled(function () {
$this->data['employee_id'] = request('employee_id');
$this->data['project_id'] = request('project_id');
$this->data['date'] = request('date');
$this->data['imputation_type'] = request('imputation_type');
})
->action('createAnother')
->keyBindings(['mod+shift+s'])
->color('secondary');
}
5 Replies
Dan Harrin
Dan Harrin2y ago
you just need default() values on the fields request() wont work, this is not a controller. store the fields in the session or something before they are saved and then access from there
magarrent
magarrentOP2y ago
hm yep it works with session flash, but how can I store the session only when "createAnother" event. I've tried using "afterCreate" method
Dan Harrin
Dan Harrin2y ago
you could override createAnother(), store the data in session, and then parent::createAnother()
magarrent
magarrentOP2y ago
Allright it works! Just added the session store after:
if ($another) {
session()->flash('dataFill', $this->data);
if ($another) {
session()->flash('dataFill', $this->data);
public function create(bool $another = false): void
public function create(bool $another = false): void
` Thanks mate!
Hamidullo Rahmonberdiyev
Hey, just wanna maintain some inputs prefilled when I hit the "createAnother" button. I'm trying some Action methods but can't get it :/ class CreateQuestion extends CreateRecord { protected static string $resource = QuestionResource::class; public function getTitle(): string|Htmlable { return __('models/question.action.create'); } protected function getHeaderActions(): array { return [ $this->getCreateFormAction() ->submit(null) ->action('create'), $this->getCreateAnotherFormAction() ->submit(null) ->action(fn () => $this->createAnother()), $this->getCancelFormAction() ->submit(null) ->action('cancel'), ]; } protected function getFormActions(): array { return []; } public function createAnother(): void { session()->flash('dataFill', [ 'purpose' => $this->data['purpose'], 'books' => $this->data['books'], ]); parent::createAnother(); }
Want results from more Discord servers?
Add your server