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.