Symfony3呈现多个时间相同的表单 [英] Symfony3 Render multiple time same form
问题描述
我希望多次呈现相同的表单以处理两个不同标签的相同操作。
问题是,当我尝试时,只显示第一个选项卡的形式,如果我更改了 id 和 名称 的形式。
我发现这是symfony的预期行为,但我仍然需要它工作。
我发现它可能有效与收集,但不知道它会如何工作。
小枝:
{{form(contactForm,{'attr':{'id':'contactFormId'〜Client.Id},'name':contactFormName〜Client.Id})}}
表格:
$这个> contactForm = $ this-> createFormBuilder($ contact,array('allow_extra_fields'=> true))
- > add('Nom',TextType :: class,array('mapped' ='false'))
- > add('Prenom',TextType :: class,array('mapped'=> false))
- > add('Telephone',TextType: :class,array(
'label'=>'Téléphone'))
- > add('Email',TextType :: class)
- > add('Ajouter' ,SubmitType :: class)
- > getForm();
这是一个较老的问题,但我刚碰到它面临着类似的情况。我想在列表视图中有一个表单对象的多个版本。对我来说,解决方案是将表单对象上的 createView()
调用移到视图上,而不是在控制器中调用它。这是关于分离问题的一种肮脏的解决方案,但是我想要发布它,以便它可以帮助其他人。
我的控制器操作如下所示:
/ **
来自控制器:
* @Route(,name =cart_show)
* @Method( GET)
* /
public function showAction(Request $ request)
{
/ ** @var CartInterface $ cart * /
$ cart = $ this-> get('rodacker.cart');
$ deleteForm = $ this-> createDeleteForm();
$ b $ return $ this-> render(
'AppBundle:Cart:show.html.twig',
['cart'=> $ cart,'deleteForm'= > $ deleteForm]
);
// ...
private function createDeleteForm()
{
return $ this-> createForm(
OrderItemDeleteType :: class,
null,
[
'action'=> $ this-> generateUrl('cart_remove_item'),
'method'=>'DELETE',
]
);
$ / code $ / pre
在视图中,我将<$ c $通过在表单变量(
deleteForm
)上调用createView
函数来传递函数c> form
{%项目%中的%}
{%set form = deleteForm.createView%}
{{form_start(form)}}
{{form_widget(form.item,{'value':item.image.filename})}}
< button type =submit class =btn btn-xs btn-dangertitle =Artikel entfernen>
< i class =fa fa-trash-o>< / i> entfernen
< / button>
{{form_end(form)}}
{%endfor%}
I would like to render the same form multiple times to handle the same action for two different tabs. The problem is that when I try, only the form of the first tab is shown, event if I change the id and name of the form. I found out it's the expected behavior of symfony, but I still need it to work.
I found that it may works with a collection but don't get how it would work.
twig:
{{ form(contactForm, {'attr': {'id': 'contactFormId' ~ Client.Id}, 'name': "contactFormName" ~ Client.Id})}}
Form:
$this->contactForm = $this->createFormBuilder($contact, array('allow_extra_fields' =>true)) ->add('Nom', TextType::class, array('mapped'=>false)) ->add('Prenom', TextType::class, array('mapped'=>false)) ->add('Telephone', TextType::class, array( 'label' => 'Téléphone')) ->add('Email', TextType::class) ->add('Ajouter', SubmitType::class) ->getForm();
解决方案It is an older question, but I just came across it facing a similar situation. I wanted to have multiple versions of one form object in a list view. For me the solution was to move the
createView()
call on the form object to the view instead of calling it in the controller. This is kind of a dirty solution regarding separation of concerns, but I thought to post it so it may help others anyway.My controller action looks like this:
/** * @Route("", name="cart_show") * @Method("GET") */ public function showAction(Request $request) { /** @var CartInterface $cart */ $cart = $this->get('rodacker.cart'); $deleteForm = $this->createDeleteForm(); return $this->render( 'AppBundle:Cart:show.html.twig', ['cart' => $cart, 'deleteForm' => $deleteForm] ); // ... private function createDeleteForm() { return $this->createForm( OrderItemDeleteType::class, null, [ 'action' => $this->generateUrl('cart_remove_item'), 'method' => 'DELETE', ] ); } }
and in the view I set the
form
variable by calling thecreateView
function on the form variable (deleteForm
) passed from the controller:{% for item in items %} {% set form = deleteForm.createView %} {{ form_start(form) }} {{ form_widget(form.item, {'value': item.image.filename}) }} <button type="submit" class="btn btn-xs btn-danger" title="Artikel entfernen"> <i class="fa fa-trash-o"></i> entfernen </button> {{ form_end(form) }} {% endfor %}
这篇关于Symfony3呈现多个时间相同的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!