F
Filament3mo ago
Xavi

Reload on send form

I have a dynamic form to make random questions for exams, but when i press submit button, questions changed. How can i do to avoid this? This is my code:
class Exam extends Page implements HasForms
{
use InteractsWithForms;

protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static string $view = 'filament.user.pages.exam';

public ?array $data = [];


public function form(Form $form): Form
{
return $form
->schema(self::getQuestions())
->statePath('data');
}

private static function getQuestions(): array
{
$qs = \App\Models\Exam::get();
$questions = [];

foreach ($qs as $question) {
$questions[] =
Radio::make('question_' . $question->id)
->label(function () use ($question) {
return $question->question;
})
->options(function () use ($question) {
$options = [];
foreach ($question->answers as $key => $answer) {
$options[$key] = $answer;
}

return $options;
})
->required();
}

return $questions;
}

public function submit()
{
$this->form->getState();
}

public function mount()
{
$this->form->fill();
}
}
class Exam extends Page implements HasForms
{
use InteractsWithForms;

protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static string $view = 'filament.user.pages.exam';

public ?array $data = [];


public function form(Form $form): Form
{
return $form
->schema(self::getQuestions())
->statePath('data');
}

private static function getQuestions(): array
{
$qs = \App\Models\Exam::get();
$questions = [];

foreach ($qs as $question) {
$questions[] =
Radio::make('question_' . $question->id)
->label(function () use ($question) {
return $question->question;
})
->options(function () use ($question) {
$options = [];
foreach ($question->answers as $key => $answer) {
$options[$key] = $answer;
}

return $options;
})
->required();
}

return $questions;
}

public function submit()
{
$this->form->getState();
}

public function mount()
{
$this->form->fill();
}
}
Thanks
1 Reply
Dennis Koch
Dennis Koch3mo ago
I'd store the order for the current form in the session so you can reproduce the same order for the next request. Delete the session data when saving was successfull and redirect to a different page.
Want results from more Discord servers?
Add your server