Symfony 2 - 单独的表单逻辑,重定向后显示表单错误

时间:2022-05-22 08:13:12

I want to separate form validation logic:

我想分离表单验证逻辑:

public function contactAction()
{
    $form = $this->createForm(new ContactType());

    $request = $this->get('request');
    if ($request->isMethod('POST')) {
        $form->submit($request);
        if ($form->isValid()) {
            $mailer = $this->get('mailer');
            // .. setup a message and send it

            return $this->redirect($this->generateUrl('_demo'));
        }
    }

    return array('form' => $form->createView());
}

I want to translate into 2 separate actions:

我想翻译成2个单独的动作:

public function contactAction()
{
    $form = $this->createForm(new ContactType());
    return array('form' => $form->createView());
}

public function contactSendAction()
{
    $form = $this->createForm(new ContactType());
    $request = $this->get('request');
    if ($request->isMethod('POST')) {
        $form->submit($request);
        if ($form->isValid()) {
            $mailer = $this->get('mailer');
            // .. setup a message and send it using 

            return $this->redirect($this->generateUrl('_demo'));
        }
    }
    // errors found - go back
    return $this->redirect($this->generateUrl('contact'));
}

The problem is that when errors exist in the form - after form validation and redirect the do NOT showed in the contactAction. (probably they already will be forgotten after redirection - errors context will be lost)

问题是当表单中存在错误时 - 在表单验证和重定向之后,不会在contactAction中显示。 (可能它们在重定向后已经被遗忘 - 错误上下文将丢失)

2 个解决方案

#1


5  

If you check out how the code generated by the CRUD generator handles this you will see that a failed form validation does not return a redirect but instead uses the same view as the GET method. So in your example you would just:

如果您查看CRUD生成器生成的代码如何处理此问题,您将看到失败的表单验证不返回重定向,而是使用与GET方法相同的视图。所以在你的例子中你只会:

return $this->render("YourBundle:Contact:contact.html.twig", array('form' => $form->createView()))

rather than return the redirect. This means you do not lose the form errors as you do in a redirect. Something else the CRUD generator adds is the Method requirement which means you could specify that the ContactSendAction requires the POST method and thus not need the extra if($request->isMethod('POST')){ statement.

而不是返回重定向。这意味着您不会像重定向一样丢失表单错误。 CRUD生成器添加的其他内容是Method要求,这意味着您可以指定ContactSendAction需要POST方法,因此不需要额外的if($ request-> isMethod('POST')){statement。

You can also just return an array if you specify the template elsewhere, for example you could use the @Template annotation and then just

如果在其他地方指定模板,也可以返回一个数组,例如,您可以使用@Template注释,然后只使用

return array('form' => $form->createView())

#2


2  

This seems to work for me in Symfony 2.8:

这似乎在Symfony 2.8中对我有用:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyController extends Controller {
    public function templateAction()
    {
        $form = $this->createForm(new MyFormType(), $myBoundInstance);

        if ($session->has('previousRequest')) {
            $form = $this->createForm(new MyFormType());
            $form->handleRequest($session->get('previousRequest'));
            $session->remove('previousRequest');
        }

        return array(
            'form' => $form->createView(),
        );
    }

    public function processingAction(Request $request)
    {

        $form = $this->createForm(new MyFormType(), $myBoundInstance);
        $form->handleRequest($request);

        if ($form->isValid()) {
            // do some stuff
            // ...

            return redirectToNextPage();
        }

        $session->set('previousRequest', $request);

        // handle errors
        // ...

        return redirectToPreviousPage();
    }
}

Please note that redirectToNextPage and redirectToPreviousPage, as well as MyFormType, are pseudo code. You would have to replace these bits with your own logic.

请注意,redirectToNextPage和redirectToPreviousPage以及MyFormType都是伪代码。您必须使用自己的逻辑替换这些位。

#1


5  

If you check out how the code generated by the CRUD generator handles this you will see that a failed form validation does not return a redirect but instead uses the same view as the GET method. So in your example you would just:

如果您查看CRUD生成器生成的代码如何处理此问题,您将看到失败的表单验证不返回重定向,而是使用与GET方法相同的视图。所以在你的例子中你只会:

return $this->render("YourBundle:Contact:contact.html.twig", array('form' => $form->createView()))

rather than return the redirect. This means you do not lose the form errors as you do in a redirect. Something else the CRUD generator adds is the Method requirement which means you could specify that the ContactSendAction requires the POST method and thus not need the extra if($request->isMethod('POST')){ statement.

而不是返回重定向。这意味着您不会像重定向一样丢失表单错误。 CRUD生成器添加的其他内容是Method要求,这意味着您可以指定ContactSendAction需要POST方法,因此不需要额外的if($ request-> isMethod('POST')){statement。

You can also just return an array if you specify the template elsewhere, for example you could use the @Template annotation and then just

如果在其他地方指定模板,也可以返回一个数组,例如,您可以使用@Template注释,然后只使用

return array('form' => $form->createView())

#2


2  

This seems to work for me in Symfony 2.8:

这似乎在Symfony 2.8中对我有用:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyController extends Controller {
    public function templateAction()
    {
        $form = $this->createForm(new MyFormType(), $myBoundInstance);

        if ($session->has('previousRequest')) {
            $form = $this->createForm(new MyFormType());
            $form->handleRequest($session->get('previousRequest'));
            $session->remove('previousRequest');
        }

        return array(
            'form' => $form->createView(),
        );
    }

    public function processingAction(Request $request)
    {

        $form = $this->createForm(new MyFormType(), $myBoundInstance);
        $form->handleRequest($request);

        if ($form->isValid()) {
            // do some stuff
            // ...

            return redirectToNextPage();
        }

        $session->set('previousRequest', $request);

        // handle errors
        // ...

        return redirectToPreviousPage();
    }
}

Please note that redirectToNextPage and redirectToPreviousPage, as well as MyFormType, are pseudo code. You would have to replace these bits with your own logic.

请注意,redirectToNextPage和redirectToPreviousPage以及MyFormType都是伪代码。您必须使用自己的逻辑替换这些位。