0

I have a controller action with a couple of forms. One form is for adding a question entity and the other form is for editing an existing question. This is a part of the action

    /**
         * @Route("/edit/{form}")
         * @Template()
         * @ParamConverter("form", class="AppBundle:Form")
         */
        public function editAction(Request $request, $form)
        {
            $questionForm = new Question();
            $addQuestionForm = $this->createForm(new AddQuestionType(), $questionForm);

            $addQuestionForm->handleRequest($request);

            if ($addQuestionForm->isValid()) {

                dump('Wrong form');
                die();

                $em->persist($questionForm);
                $em->flush();

                return $this->redirectToRoute('app_form_edit', array('form' => $form_id));
            }

            $editQuestion = new Question();
            $editAjaxQuestionForm = $this->createForm(new AddQuestionType(), $editQuestion);

            $editAjaxQuestionForm->handleRequest($request);

            if ($editAjaxQuestionForm->isValid()) {

            dump('Correct form');
            die();

            $em->persist($editQuestion);
            $em->flush();

            return $this->redirectToRoute('app_form_edit', array('form' => $form_id));
        }

I added the dump() and die() for debugging because the editAjaxQuestionForm was not working properly. Then I noticed that when I submit the editAjaxQuestionForm, the dump('Wrong form') is shown. So the form goes through the wrong validation.

The forms are created after an Ajax call. This is the code

/**
     * @Route("/AjaxAddQuestionForm/{section}")
     * @Template
     * @ParamConverter("section", class="AppBundle:Section")
     */
    public function ajaxAddQuestionFormAction(Request $request, $section)
    {
        $question = new Question();
        $question->setSection($section);
        $addQuestionForm = $this->createForm(new AddQuestionType(), $question);

        return array(
            'section' => $section,
            'addAjaxQuestionForm' => $addQuestionForm->createView(),
        );
    }

    /**
     * @Route("/AjaxEditQuestionForm/{question}")
     * @Template
     * @ParamConverter("question", class="AppBundle:Question")
     */
    public function ajaxEditQuestionFormAction(Request $request, $question)
    {
        $editQuestionForm = $this->createForm(new AddQuestionType(), $question);

        return array(
            'question' => $question,
            'editAjaxQuestionForm' => $editQuestionForm->createView(),
        );
    }

I think I've tried everything but I can't figure out what is wrong.

2
  • 4
    This is because both forms have the same name. Related to: Multiple forms of same type - Symfony 2 Commented Oct 6, 2015 at 9:59
  • Works perfectly now. Thanks a lot! Commented Oct 6, 2015 at 10:10

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.