Refresh form section description after save

Is there a way to refresh the form after a save action? I'm trying to add a section description that displays a percentage based on what inputs within it have been filled. This needs to update when the form is saved. Many thanks for any help!
14 Replies
RedSquirrel
RedSquirrelOP6mo ago
Hi Leandro, thanks for the suggestion, I have, this refreshes the data inside the fields but the section description stays the same
LeandroFerreira
LeandroFerreira6mo ago
could you share some code?
RedSquirrel
RedSquirrelOP6mo ago
The error I get for that is that refreshFormData does not exist protected function getHeaderActions(): array { return [ Action::make('save') ->label('Save Answers') ->action(function() { $this->answerform(); $this->refreshFormData(); }) ]; } public function answerform(): void { foreach($this->form->getState() as $key => $answer) { $answers = Answer::where('lead_id', $this->record->id)->where('question_set_id', $this->question_set->id)->where('question_id', $key)->first(); if(!$answers) { $answers = new Answer; } $answers->lead_id = $this->record->id; $answers->question_set_id = $this->question_set->id; $answers->question_id = $key; $answers->value = $answer; $answers->save(); } Notification::make() ->title('Updated') ->body('Your answers have been saved') ->success() ->send(); }
LeandroFerreira
LeandroFerreira6mo ago
I mean, what are you doing in the section description..
RedSquirrel
RedSquirrelOP6mo ago
public function form(Form $form): Form { $dynamicForm = []; if($this->question_groups) { foreach($this->question_groups as $qg) { $dynamicQuestions = []; $questions = Question::where('question_group_id', $qg->id)->get(); if($questions) { $total_answered = 0; foreach($questions as $q) { $dynamicQuestions[] = $this->getDynamicField($q); if($this->checkIfAnswered($q->id, $this->existing_answers)) { $total_answered = $total_answered + 1; } } } $total_questions = $questions->count(); $dynamicForm[] = Section::make($qg->name)->collapsed()->description('Completed ' . $total_answered . " of " . $total_questions)->schema($dynamicQuestions); } } return $form ->schema($dynamicForm) ->statePath('data'); } So in an ideal world, on save, the form would be rebuilt and then the section description would be updated
LeandroFerreira
LeandroFerreira6mo ago
try ->after(fn() => $this->getRecord()->refresh())
RedSquirrel
RedSquirrelOP6mo ago
Thank you, Where does that need to be chained to sorry?
LeandroFerreira
LeandroFerreira6mo ago
in your action.. is it a modal action, right?
RedSquirrel
RedSquirrelOP6mo ago
It's a header action
LeandroFerreira
LeandroFerreira6mo ago
ok, but where is your form?
RedSquirrel
RedSquirrelOP6mo ago
On a page
LeandroFerreira
LeandroFerreira6mo ago
are you using a simple resource?
RedSquirrel
RedSquirrelOP6mo ago
It's a page within a simple resource It's a "Lead" model which is the resource, which has a page where you can manage answers to questions about the lead So basically a relationship form
Want results from more Discord servers?
Add your server