0

In Symfony I have this part of my code where I am building a view with some data and a form with some radio buttons. When submitting the form I am doing a dump in the view to check which data has been submitted, but the data does not match with the one the form was build. Can someone help? Thanks.

public function playAction(Request $request){
    $data = $this->getDbQuestion();
    $questionData = $data[0];
    dump($questionData);
    $answerData = $data[1];
    dump($answerData);

    $form = $this->createFormBuilder($answerData)
    ->add('answers', ChoiceType::class,
         array(
             'choices'=>  $answerData,
             'multiple'=>false,'expanded'=>true,
             'choice_label' => 'answer',
    ))
    ->add('Submit',SubmitType::class, array('label' => 'Send Answer'))
    ->getForm();

    $form->handleRequest($request);
    if($form->isSubmitted()) {
         $formData = $form->getData();
         return $this->render('QuizViews/correctAnswer.html.twig', array(
                'ss' => $formData
        ));
    }
    return $this->render('QuizViews/playQuiz.html.twig', array(
        'form' => $form->createView(),
        'question' => $questionData
    ));
}

Twig

<a href="/quiz/question">
    <input type="button" value="Start Quiz" />
</a>
<br>
FormData Correct {{ dump(ss) }}

Form data

Submitted form Data

2 Answers 2

0

After chatting, this might be a better solution for the answer section:

->add('answers', EntityType::class, array(
    'class' => 'AppBundle:Answer',
    'query_builder' => function (EntityRepository $er) {
        return $er->createQueryBuilder('a')
                ->where('a.question_id->getId() = :qID')
                ->setParameter('qID', 1);
    },
     'multiple'=>false,
     'expanded'=>true,
    'choice_label' => 'answer',
))

Try it!

Sign up to request clarification or add additional context in comments.

Comments

0

Your call to get the data after verifying the form isSubmitted is incorrect. You need to call like so:

$formData = $form->get('answers')->getData();

That just gets the 'answers' only.

Edit #2 You might also want to change this:

->add('answers', ChoiceType::class,
    array(
        'choices'=>  $answerData,
        'multiple'=>false,
        'expanded'=>true,
        'choice_label' => 'answer',
        'choice_value' => $answerData,
))

Which sets the 'choice_value', what is actually selected and returned from the getData().

Can you post your twig answers file please? Edit your post and so I can see.

11 Comments

Yes, you are right. This is like in my second screenshot "answers" => Answer. But the data is still incorrect.
Is the answer you are seeing is "rasp11,rasp22,rasp33,rasp44", but you are expecting "rasp111,rasp222,rasp333,rasp444"?
yes something like that. Like in the screenshots above when I select "rasp22" I get in my view "rasp222" or "rasp2" or "rasp2222". This are all the answers for other questions.
Actually, how do you create $data[1]? Or how to create $data? Can you show me the code? Looks like that is where the problem is.
Also for example if for a question I select 'rasp1' then the wrong answers will always be rasp11, or rasp111, or rasp1111. Never rasp2, rasp22, rasp333 etc.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.