有没有一种方法可以在同一页面上嵌入两个表单(数据位于2个不相关的实体上),并仅通过一个提交按钮提交表单?
这个想法是仅在一个控制器动作中验证两个提交的表单。
Entity1和Entity2绝对没有共同点。
|--------------------------
| Form 1 (Entity 1) |
|--------------------------
--------------- |
| Main Form |------------------
--------------- |
|--------------------------
| Form 2 (Entity 2) |
|--------------------------
有谁知道这是否可行?
非常感谢。
这是可能的。这样的事情应该起作用:
$entity1 = new Entity1();
$entity2 = new Entity2();
$form = $this->createMainForm();
$form->setData(array(
'entity1' => $entity1,
'entity2' => $entity2,
));
if ($request->isMethod('POST')) {
$form->bindRequest($request);
if ($form->isValid()) {
// $entity1 and $entity2 should contain the post data
// and can be persisted or whatever it is you want to do
// ...
您还可以创建一个同时包含两个实体的模型,并为其创建一个表单。使用$mainEntity->getEntity1();
中检索的封装实体。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句