duanpang5583 2015-10-24 21:01
浏览 44

在无效表单提交上使用不同的路由

I'm building a page which will contain multiple forms. Now when i enter a password mismatch for example i go to the wrong route, namely where the form lives and not the page where i render it.

How do i go at this correctly?

html structure, omitted the 'management' page this is all loaded on

#app/Resources/views/forms/user.html.twig
{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit" class="btn btn-primary pull-right">Submit</button>
{{ form_end(form) }}

#app/Resources/views/components/management/user/user.html.twig
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6">
           {% include 'components/management/user/list.html.twig' %}
        </div>
        <div class="col-md-6">
            {% include 'components/management/user/add.html.twig' %}
        </div>
    </div>
</div>

#app/Resources/views/components/management/user/add.html.twig
<table class="table">
    <thead id="add-user-head" class="pointer">
    <tr>
        <th>Add user <span id="caret-add-user" class="caret"></span></th>
    </tr>
    </thead>

    <tbody id="add-user-body">
    <tr>
        <td>
            {{ render(controller('AppBundle:Manage:user')) }}
        </td>
    </tr>
    </tbody>
</table>

Controller, omitted the form builder

#@AppBundle/Controller/ManageController.php
public function viewAction()
    {
        return $this->render('pages/management.html.twig', array('users' => $this->listUsers()));
    }

    public function userAction(Request $request)
    {
        // 1) build the form
        $user = new User();
        $form = $this->createForm(new UserType(), $user, array(
            'action' => $this->generateUrl('manage_user'),
            'method' => 'GET'
        ));

        // 2) handle the submit (will only happen on POST)
        $form->handleRequest($request);

        if ($form->isValid() && $form->isSubmitted()) {
            // 3) Encode the password (you could also do this via Doctrine listener)
            $password = $this->get('security.password_encoder')
                ->encodePassword($user, $user->getPlainPassword());
            $user->setPassword($password);

            // 4) save the User!
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();

            // ... do any other work - like send them an email, etc
            // maybe set a "flash" success message for the user

            return $this->redirectToRoute('manage');
        }

        return $this->render('forms/user.html.twig', array('form' => $form->createView()));
    }

To clarify a bit more: Invalid input will bring me to the forms/user.html.twig page because of the form action.

  • 写回答

1条回答 默认 最新

  • dongzhang1987 2015-10-24 23:16
    关注

    I think you just need another else statement, though Im not sure where exactly you want to redirect to:

    if ($form->isValid() && $form->isSubmitted()) {
            // same stuff
        } elseif ($form->isSubmitted()) {
            return $this->redirectToRoute('invalidSubmission');
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题