如果提交的表单无效,具有@Method(" post")的控制器应该如何处理请求? [英] How should a controller with @Method("post") handle the request if submitted form is not valid?

查看:107
本文介绍了如果提交的表单无效,具有@Method(" post")的控制器应该如何处理请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的symfony2中有一个控制器,如果用户表单有效,它将重定向到其他一些链接,但如果有一些错误,它将保留在同一页面并显示错误。只是一般情况下,客户端验证被禁用,只有服务器端验证检查错误。

  / ** 
*创建一个新的用户实体。
*
* @Route(/ create,name =admin_user_create)
* @Method(post)
* @Template(UserBundle:User:new .html.twig)
* /
public function createAction()
{
$ entity = new User();
$ request = $ this-> getRequest();
$ form = $ this-> createForm(new UserType(),$ entity);

$ form-> bindRequest($ request);
if($ form-> isValid())
{
// DO SOMETHING ...
return $ this-> redirect($ this-> generateUrl( 'some_link',array('user_id'=> $ entity-> getId()));
}

//如果表单无效,它将呈现模板并显示错误。
return array(
'entity'=> $ entity,
'form'=> $ form-> createView()
);
}

情景如下:


  1. 用户为表单输入一些无效数据
  2. 用户提交表单

  3. 控制器检查if该表单是有效的

  4. 由于它无效, @Template(UserBundle:User:new.html.twig)


  5. 浏览器中的路由将是 / create

  6. 如果用户点击在浏览器链接上,它不是 post 会得到一个错误

我该如何解决这个问题 ?我必须重新导向吗?由于该方法是 post 是否可以重定向?

解决方案

不要指定@Method(POST)并在方法中执行此操作:

  if($ request-> getMethod()=='POST')
{
$ form-> bindRequest $要求提供);
if($ form-> isValid())
{
// DO SOMETHING ...
return $ this-> redirect($ this-> generateUrl( 'some_link',array('user_id'=> $ entity-> getId()));
}
}


I have a controller in symfony2 like below, if the user form is valid it will redirect to some other link but if there is some error it will remain in the same page and shows the errors. simply general scenario when the client side validation is disabled and only sever side validation is checking for the errors.

/**
 * Creates a new User entity.
 *
 * @Route("/create", name="admin_user_create")
 * @Method("post")
 * @Template("UserBundle:User:new.html.twig")
 */
public function createAction()
{
    $entity  = new User();
    $request = $this->getRequest();
    $form    = $this->createForm(new UserType() , $entity);

    $form->bindRequest($request);
    if($form->isValid())
    {
        // DO SOMETHING ... 
        return $this->redirect($this->generateUrl('some_link' , array( 'user_id' => $entity->getId() )));
    }

    // If the form is NOT valid, it will render the template and shows the errors.   
    return array(
        'entity' => $entity ,
        'form'   => $form->createView()
    );
}

Scenario would be something like below:

  1. User input some invalid data for the form
  2. User submit the form
  3. Controller check if the form is valid
  4. Since it is not valid it will render the template, in this case

    @Template("UserBundle:User:new.html.twig")
    

  5. The route in the browser will be /create
  6. If the user click on browser link and it not post will get an error

How can I fix this ? Do I have to redirect again? Since the method is post is it possible to redirect ?

解决方案

Don't specify the @Method("POST") and do this in the method:

if ($request->getMethod() == 'POST')
{
    $form->bindRequest($request);
    if($form->isValid())
    {
        // DO SOMETHING ... 
        return $this->redirect($this->generateUrl('some_link' , array( 'user_id' => $entity->getId() )));
    }
}

这篇关于如果提交的表单无效,具有@Method(" post")的控制器应该如何处理请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆