Php 当验证失败时,如何使用以前的值重新显示窗体?

Php 当验证失败时,如何使用以前的值重新显示窗体?,php,symfony,symfony-2.2,Php,Symfony,Symfony 2.2,我有一个表单,它必须通过一些其他的验证,而不是不寻常的验证(大约有4个字段相互依赖)。问题是,当它失败时,我将用户重定向回,但是表单会丢失它的值,我不想要它。我知道这可以通过会话完成,但可能有一种“更理智”的方式。代码通常是: public function printAction() { if ($this->getRequest()->getMethod() == "POST") { $form->bindRequest($this->

我有一个表单,它必须通过一些其他的验证,而不是不寻常的验证(大约有4个字段相互依赖)。问题是,当它失败时,我将用户重定向回,但是表单会丢失它的值,我不想要它。我知道这可以通过会话完成,但可能有一种“更理智”的方式。代码通常是:

public function printAction()
{
    if ($this->getRequest()->getMethod() == "POST")
    {
        $form->bindRequest($this->getRequest());
        if ($form->isValid())
        {
             .... more validation.... Failed!
             return $this->redirect($this->generateUrl("SiteHomePeltexStockStockHistory_print"));
             // and this is when I lose the values.... I dont want it
        }
    }
}

您可以对与表单相关的
GET
POST
请求使用相同的操作。如果验证失败,只需不重定向,同一表单将重新显示,并显示输入的值和验证错误消息:

/**
 * @Template
 */
public function addAction(Request $request)
{
    $form = /* ... */;        

    if ($request->isMethod('POST')) {
        $form->bind($request);

        if ($form->isValid()) {
            // do something and redirect
        }

        // the form is not valid, so do nothing and the form will be redisplayed
    }

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

您可以对与表单相关的
GET
POST
请求使用相同的操作。如果验证失败,只需不重定向,同一表单将重新显示,并显示输入的值和验证错误消息:

/**
 * @Template
 */
public function addAction(Request $request)
{
    $form = /* ... */;        

    if ($request->isMethod('POST')) {
        $form->bind($request);

        if ($form->isValid()) {
            // do something and redirect
        }

        // the form is not valid, so do nothing and the form will be redisplayed
    }

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

进行新重定向时,可以将参数传递到新页面:

$this->redirect($this->generateUrl('SiteHomePeltexStockStockHistory_print', array('name1' => 'input1', 'name2' => 'input2', 'name3' => $input3, ....)));
或直接传递一个post值数组:

$this->redirect($this->generateUrl('SiteHomePeltexStockStockHistory_print', array('values' => $values_array)));

进行新重定向时,可以将参数传递到新页面:

$this->redirect($this->generateUrl('SiteHomePeltexStockStockHistory_print', array('name1' => 'input1', 'name2' => 'input2', 'name3' => $input3, ....)));
或直接传递一个post值数组:

$this->redirect($this->generateUrl('SiteHomePeltexStockStockHistory_print', array('values' => $values_array)));

你可能想做这样的事情

class FooController extends Controller
{
    /**
     * @Route("/new")
     * @Method({"GET"})
     */
    public function newAction()
    {
        // This view would send the form content to /create
        return $this->render('YourBundle:form:create.html.twig', array('form' => $form));
    }

    /**
     * @Route("/create")
     * @Method({"POST"})
     */
    public function createAction(Request $request)
    {
        // ... Code
        if ($form->isValid()) {
            if (/* Still valid */) {
                // Whatever you do when validation passed
            }
        }

        // Validation failed, just pass the form
        return $this->render('YourBundle:form:create.html.twig', array('form' => $form));
    }
}

你可能想做这样的事情

class FooController extends Controller
{
    /**
     * @Route("/new")
     * @Method({"GET"})
     */
    public function newAction()
    {
        // This view would send the form content to /create
        return $this->render('YourBundle:form:create.html.twig', array('form' => $form));
    }

    /**
     * @Route("/create")
     * @Method({"POST"})
     */
    public function createAction(Request $request)
    {
        // ... Code
        if ($form->isValid()) {
            if (/* Still valid */) {
                // Whatever you do when validation passed
            }
        }

        // Validation failed, just pass the form
        return $this->render('YourBundle:form:create.html.twig', array('form' => $form));
    }
}

有点离题,你能在这里编辑你的答案吗,我不小心投了反对票,想更改投票,谢谢。有点离题,你能在这里编辑你的答案吗,我不小心投了反对票,想更改投票,谢谢。