How should a controller with @Method(“post”) hand

2019-08-11 10:00发布

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 ?

标签: forms symfony
3条回答
来,给爷笑一个
2楼-- · 2019-08-11 10:21
/**
 * 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()))
);

}

查看更多
不美不萌又怎样
3楼-- · 2019-08-11 10:30

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() )));
    }
}
查看更多
太酷不给撩
4楼-- · 2019-08-11 10:31

You can accept GET or POST and do something link:

/**
 * 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()))
    );
}
查看更多
登录 后发表回答