1

我在 symfony2 中有一个控制器,如下所示,如果用户表单有效,它将重定向到其他链接,但如果出现错误,它将保留在同一页面中并显示错误。当客户端验证被禁用并且只有服务器端验证正在检查错误时,这是​​一个简单的一般场景。

/**
 * 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()
    );
}

场景将如下所示:

  1. 用户为表单输入了一些无效数据
  2. 用户提交表单
  3. 控制器检查表单是否有效
  4. 由于它无效,它将呈现模板,在这种情况下

    @Template("UserBundle:User:new.html.twig")
    
  5. 浏览器中的路线将是/create
  6. 如果用户click在浏览器链接上而不是它post会得到一个错误

我怎样才能解决这个问题 ?我必须再次重定向吗?既然方法是post可以重定向吗?

4

3 回答 3

2

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

if ($request->getMethod() == 'POST')
{
    $form->bindRequest($request);
    if($form->isValid())
    {
        // DO SOMETHING ... 
        return $this->redirect($this->generateUrl('some_link' , array( 'user_id' => $entity->getId() )));
    }
}
于 2012-07-18T17:42:15.300 回答
0

您可以接受GETPOST做某事链接:

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

    // Is this POST? Bind the form...
    if('POST' == $request->getMethod()) $form->bindRequest($request);

    // GET or from not valid? Return the view...
    if('GET' == $request->getMethod() || !$form->isValid()) :
        return array(
            'entity' => $entity ,
            'form'   => $form->createView()
        );
    endif;

    // Success, then persist the entity and redirect the user
    return $this->redirect($this->generateUrl('some_link',
        array('user_id' => $entity->getId()))
    );
}
于 2012-07-19T09:12:14.437 回答
0
/**
 * Creates a new User entity.
 *
 * @Route("/create", name="admin_user_create")
 * @Method("GET|POST")
 * @Template("Use`enter code here`rBundle:User:new.html.twig")
 */
public function createAction()
{
    $entity  = new User();
    $request = $this->getRequest();
    $form    = $this->createForm(new UserType() , $entity);
// Is this POST? Bind the form...
if('POST' == $request->getMethod()) $form->bindRequest($request);

// GET or from not valid? Return the view...
if('GET' == $request->getMethod() || !$form->isValid()) :
    return array(
        'entity' => $entity ,
        'form'   => $form->createView()
    );
endif;

// Success, then persist the entity and redirect the user
return $this->redirect($this->generateUrl('some_link',
    array('user_id' => $entity->getId()))
);

}

于 2014-03-06T03:13:22.190 回答