Symfony submit form problem

Hello. I have this function in Symfony where I am getting data from the database and building a form. The form builds successful with the data, but when trying to submit the form I get the following error:

     * @Route("/quiz/question")
    public function fetchQuestionAction(Request $request){

        $accessor = PropertyAccess::createPropertyAccessor();

        $question = $this->getDoctrine()
        $questionID = $accessor->getValue($question,'[0].id');

        $answers = $this->getDoctrine()
            ->findBy(array("question" => $questionID));

        $form = $this->createFormBuilder($question[0])
            ->add('id', HiddenType::class)
            ->add('image', TextType::class)
            ->add('answers', ChoiceType::class,
                    'choices'=>  $answers,
                    'choice_label' => 'answer'
            ->add('Submit',SubmitType::class, array('label' => 'Send Answer'))

        if ($form->isSubmitted()) {

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

that means that the object you feed to the form does not have one of the required methods.

although I do wonder why you initialise the form with a different object in the submit handler than in the initial setup.

Do you refer to this parts?
$form = $this->createFormBuilder($question[0])
Here I am just passing the array to build the question.

This part is from the symfony book how form requests are handled.

I more refer to this part:

There I am just shuffling the questions so I don’t get the same question every time. Is not the best approach because I am getting all the records from the DB, shffle them and then at position question[0] I will always have a different record , but now just trying to display some data and handle a submitted form.

and you are aware that you process a completely different question than the one you show the user? because the answer the user chose is a totally different one than the one the form uses.

I get the feeling that you still don’t understand the difference between what you intend to do (answer a quiz) vs. what your code actually does (editing a quiz).

How I was thinking that this works is: data from the database is put in the form fields, and when submitting the form, the actual value of the fields are submited. So when the user submits the form, I need the id which is a hidden field, and the value of the selected radio button, this way I know the question that the user answered and the answer he has given.

but it makes no sense to put the question itself in a form field. putting a value into a form field means that it is basically subject to change. nothing of your database must go into the form fields themselves (maybe except the question id). the answers must only provide the choices (that part is correct). and of course the form processing must use the same question that you gave the user.

and because of that I would even separate the question id from the form, since you must know the id before you (re)build the form for processing (which is impossible, if you don’t know the id field’s full name).

Yes I understand what you are saying, and yesform fields are only used to chamge data for example but i only used them to see if I can display the data somewhere.
I will take a different approach. When getting the question from the database store the id of the question somehow so i know when processing the user answer to which question i am reffering to.
For the image and question I will not use text fields, and for the options I will use the form to submit the user amswer back for processing(this part i am not sure).

there’s nothing difficult. use the answers field as you have it now, omitting all other fields /1/. For the data I’d just feed the form builder an empty array $this->createFormBuilder([]), so that will give you the answer via $form->getData()['answers']. the ID can either be manually inserted into the form (hence you know the full name and can extract it from the request object ($request->request->filter('id')) or use it as URL parameter in the form’s action (via $formBuilder->setAction(), $request->query->filter('id')).

1 - I usually omit buttons in the form component as their only purpose is to make the browser send the form and they don’t carry any information. this also makes the form processing compatible to AJAX.

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