doxn43207 2015-10-06 09:05
浏览 39

错误的表单在symfony中验证

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度