How to handle formWizard with random fields in LiveWire component?

Hello, This might be off topic, but I really struggling with this. I am generating a form Wizard with 10 random questions, but the questions (fields) got changed each time when i click next step. I am generating Form Schema with this,
public static function getTestFormSchema($randomQuestions){
$testquestionsschema = [];
foreach ($randomQuestions as $question) {
$testquestionsschema[] = Wizard\Step::make($question->id)->hiddenLabel()->schema([self::getQuestionFieldFor($question)]);
}
return Wizard::make($testquestionsschema)->skippable()->extraAttributes(['class' => 'test']);
}

public static function getQuestionFieldFor($field){
$fieldName = "field_" . $field->id;
switch($field->question_type) {
case 'MC':
case 'TF':
$options = array_column($field->choices,'choice_text');
$options = array_combine($options, $options);
$ffield = Radio::make($fieldName)->label($field->question_text)->options($options);
break;
}
return $ffield;
}
public static function getTestFormSchema($randomQuestions){
$testquestionsschema = [];
foreach ($randomQuestions as $question) {
$testquestionsschema[] = Wizard\Step::make($question->id)->hiddenLabel()->schema([self::getQuestionFieldFor($question)]);
}
return Wizard::make($testquestionsschema)->skippable()->extraAttributes(['class' => 'test']);
}

public static function getQuestionFieldFor($field){
$fieldName = "field_" . $field->id;
switch($field->question_type) {
case 'MC':
case 'TF':
$options = array_column($field->choices,'choice_text');
$options = array_combine($options, $options);
$ffield = Radio::make($fieldName)->label($field->question_text)->options($options);
break;
}
return $ffield;
}
and loading that in my Livewire component
public function mount()
{
$this->questions = $this->test->questions()->inRandomOrder()->limit(10)->get();
$this->form->fill();
}

public function form(Form $form): Form
{
return $form
->schema([BasicHelper::getTestFormSchema($this->questions)])
->statePath('data');
}
public function mount()
{
$this->questions = $this->test->questions()->inRandomOrder()->limit(10)->get();
$this->form->fill();
}

public function form(Form $form): Form
{
return $form
->schema([BasicHelper::getTestFormSchema($this->questions)])
->statePath('data');
}
Livewire loading random records from the model with each next action request. Could anyone please help me with this? is there any better way to avoid this or load random entries with first load or page refresh only? Thank you. πŸ™‚
0 Replies
No replies yetBe the first to reply to this messageJoin