Symfony3中的一个页面上有多个表单 [英] Multiple Forms on one page in Symfony3

查看:112
本文介绍了Symfony3中的一个页面上有多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有解决方案来处理Symfony 3中的一个页面上的多个表单?

它的基本工作原理是提交一个表单以空值结束这些表单似乎有相同的名称(表单)。



我在页面上有多个handleRequests,每个表单都有一个

  $ form-> handleRequest($ request); 

...

$ secondform-> handleRequest($ request);

我想我需要检查哪个表单被提交,但是

  $ request-> request-> get($ form-> getName())
pre>

为这两种形式提供了相同的名称,所以这是行不通的。



是否有解决方案处理多个表单或可能更改每个表单的名称?

解决方案

处理多个动态生成的表单时遇到此问题在一页上。 isSubmitted()表现不够健全,因为我相信它会根据表单名称进行检查,并且所有表单都具有相同的名称。因此,用一个表单提交一个表单更新了表单的所有表单



我使用Form Factory的 createNamed 方法。



这已经在Symfony v3.2.1上测试过了

<$ p $ ($ aObjects as $ oObject)
{
$ sUniqueFormName ='Form'。 $ oObject->的getId(); //使用一些独特的数据来生成表单名称。
$ oForm = $ this-> get('form.factory') - > createNamed($ sUniqueFormName,CustomFormType :: class,$ oObject);
$ aForms [$ sUniqueFormName] = $ oForm;
}

foreach($ aForms as $ sFormName => $ oForm)
{
$ oForm-> handleRequest($ oRequest);
if($ oForm-> isSubmitted()&& $ oForm-> isValid())
{
//做东西
}
/ /在调用handleRequest之后创建视图*,因此数据更新为表单,即使存在验证错误。
$ aFormViews [$ sFormName] = $ oForm-> createView();
}

该模板非常明了,只是遍历表单视图和呈现他们通常。确保你为每个表单显式地输出一个 form_end(),但是当我使用 form_rest()


Is there a solution to handle multiple forms on one Page in Symfony 3?

Its basically working but submitting one form end in null values for the other form since all the forms seem to have the same name (form).

I have multiple handleRequests on the page, each for one form

$form->handleRequest($request);

...

$secondform->handleRequest($request);

I think I would need a possibility to check which form was submitted but

$request->request->get($form->getName())

gives the same name for both forms so this doesn’t work.

Is there a solution to handle multiple forms or maybe to change the name of each form?

解决方案

I ran into this problem when dealing with multiple dynamically generated forms on one page. isSubmitted() was not behaving sanely, as I believe it checks based on form names, and all the forms had the same name. As such, submitting one form updated all the forms with that form's data.

I got around this using the Form Factory's createNamed method.

This was tested on Symfony v3.2.1

foreach ($aObjects as $oObject) 
{
    $sUniqueFormName = 'Form' . $oObject->getId(); //Use some unique data to generate the form name.
    $oForm = $this->get('form.factory')->createNamed($sUniqueFormName, CustomFormType::class, $oObject);
    $aForms[$sUniqueFormName] = $oForm;
}

foreach ($aForms as $sFormName => $oForm)
{
    $oForm->handleRequest($oRequest);
    if ($oForm->isSubmitted() && $oForm->isValid())
    {
        //Do stuff
    }
    //Create the view *after* calling handleRequest, so data is updated to the form, even if, say, there are validation errors.
    $aFormViews[$sFormName] = $oForm->createView(); 
}

The template is pretty self-explanatory, just iterate over the form views and render them normally. Make sure you explicitly output a form_end() for each form, though, I experienced weirdness when just using form_rest().

这篇关于Symfony3中的一个页面上有多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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