Symfony form data is not always submited

Hi guys. I have this Symfony code where I am building a form with radio buttons. My problem is that when the user submits it’s choice this is not always passed to the twig template. Sometimes the data is passed sometimes is not. Why I am getting this inconsistency? Can someone help? Thanks

Here is my code:

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,

                'choice_label' => 'answer',
            ))
        ->add('Submit',SubmitType::class, array('label' => 'Send Answer'))
        ->getForm();

        $form->handleRequest($request);
        if($form->isSubmitted()) {
            $formData = $form->get('answers')->getData();

            return $this->render('QuizViews/correctAnswer.html.twig', array('ss' => $formData ));
        }

        return $this->render('QuizViews/playQuiz.html.twig', array('form' => $form->createView(),'question' => $questionData));
    }

The dump is just to show the data I am getting from DB.

Sometimes data is not submited…

And sometimes it is…

OK. Adding if($form->isSubmitted()&& $form->isValid()) doesn’t submit the answer. Instead when submitting the answer the question is rebuilt again. This means that the form is not valid. How to make my form valid?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.