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
Hi Leandro, thanks for the suggestion, I have, this refreshes the data inside the fields but the section description stays the same
could you share some code?
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();
}
I mean, what are you doing in the section description..
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
try
->after(fn() => $this->getRecord()->refresh())
Thank you, Where does that need to be chained to sorry?
in your action.. is it a modal action, right?
It's a header action
ok, but where is your form?
On a page
are you using a simple resource?
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