In Symfony I am building a form with four possible choices as radio buttons. When the user submits the form with a choice, I am taking the submitted form data (the user choice) and display it in twig. The problem is that sometimes the submitted form data is null even if the user selected a radio, and sometimes the selected choice is passed and displayed in twig. Also sometimes after reciving null in twig and refreshing the page the data is shown, but this doesn't always happen. Why this inconsistency? How can I solve this? Thanks
public function playAction(Request $request){
$data = $this->getDbQuestion();
$questionData = $data[0];
$questionID = $questionData->getId();
dump($questionData);
$answerData = $data[1];
dump($answerData);
$form = $this->createFormBuilder($answerData)
->add('answers', EntityType::class, array(
'class' => 'QuizBundle:Answer',
'query_builder' => function (EntityRepository $er) use ($questionID) {
return $er->createQueryBuilder('a')
->where('a.question = :qID')
->setParameter('qID', $questionID);
},
'multiple'=>false,
'expanded'=>true,
'error_bubbling' => true,
'choice_label' => 'answer',
))
->add('Submit',SubmitType::class, array('label' => 'Send Answer'))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$formData = $form->get('answers')->getData();
$errors = $form->getErrors();
return $this->render('QuizViews/correctAnswer.html.twig', array('ss' => $formData, 'errors' => $errors ));
}
return $this->render('QuizViews/playQuiz.html.twig', array('form' => $form->createView(),'question' => $questionData));
}
Dumping the submitted form data in twig
<a href="/quiz/question">
<input type="button" value="Start Quiz" />
</a>
<br>
FormData Correct {{ dump(ss) }}
Form Errors {{ dump(errors) }}
Adding
$form->isValid()I get this in twig when the answer is not submitted.



