user3810730
user3810730

Reputation:

Symfony submitted form data is NULL

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) }}

The form data

Null in twig

User answer in twig

Adding $form->isValid()I get this in twig when the answer is not submitted.

Adding validation to form, in twig I get this

Upvotes: 1

Views: 4011

Answers (2)

Bart Bartoman
Bart Bartoman

Reputation: 766

Did you tryed to check if your form was valid?

if($form->isSubmitted() && $form->isValid()) {

Also, set and display form errors may help a lot, to check what is wrong, and which part of your form isn't correct.

Upvotes: 1

Alvin Bunk
Alvin Bunk

Reputation: 7764

Glad you got further. I believe that the "$formData" is coming back as an answer object, and all you have to do in twig is call something like:

{{ ss.getAnswer }}

Where getAnswer is a method for the Entity Answer (I don't remember your code). The form data correct you show above looks exactly like a dump of an "object". Remember you need to think in terms of objects.

Let me know if that doesn't work.

Edit #2.

Try this change:

->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_value' => 'answer',
        'choice_label' => 'answer',
))

Where I've added 'choice_value' => 'answer', in this case 'answer' should be the answer value stored in the Answer Entity.

Edit #3. This is weird. I'm not sure why it's not working. You shouldn't need to pass in the $answerData into the builder. Try leaving it blank, and let's change it to a drop-down list (default):

$form = $this->createFormBuilder()
    ->add('answers', EntityType::class, array(
         'class' => 'QuizBundle:Answer',
         'label' => 'Select an Answer',
         'choice_value' => 'answer',
         'choice_label' => 'answer',
    ))

If that doesn't work, something else is definitely wrong, possibly your Entities. This returns the Entity value in the db for me.

Upvotes: 1

Related Questions