在 Symfony 中创建一个带有 for 的表单
Create a form with a for in Symfony
我在 Symfony 中创建了一个这样的表单:
$form = $this->createFormBuilder($template)
->add('product1', 'text')
->add('product2', 'text')
->add('save', 'submit')
->getForm();
现在这是我的树枝:
{{ form_start(form) }}
{% for i in 1..2 %}
<div class="col-md-3">
<div class="product">
<div class="name">
{{ form_label(form.product{{ i }} ) }}
{{ form_errors(form.product{{ i }} ) }}
{{ form_widget(form.product{{ i }} ) }}
</div>
</div>
</div>
{% endfor %}
{{ form_end(form) }
主要思想是遍历 for 并获得一个新的 form.product<X>
每个循环。
我不能让它工作,我什至不知道它是否可以通过这种方式完成。有什么想法吗?
为此,我建议您使用 Collection 类型。但如果你想按照自己的方式去做,你应该这样做:
{{ form_start(form) }}
{% for i in 1..2 %}
<div class="col-md-3">
<div class="product">
<div class="name">
{{ form_label( attribute(form, 'product' ~ i) ) }}
{{ form_errors( attribute(form, 'product' ~ i) ) }}
{{ form_widget( attribute(form, 'product' ~ i) ) }}
</div>
</div>
</div>
{% endfor %}
{{ form_end(form) }
你是对的,它可能行不通。有关信息,Twig 中的连接符号是“~”。
在您的情况下,如果您的实体应该有 2 个或更多 "products",您应该使用集合而不是手动创建每个产品。
在您的实体中,您会有类似
的东西
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
protected $products;
并且在产品实体上,您将拥有
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
然后在您的第一个实体 __constructor 或您的控制器中迭代创建任意数量的产品并将它们添加到实体中。
在您的表单中,您只需添加:
$builder->add('products', 'collection');
并且您可以在 Twig 中对其进行迭代。
希望这对您有所帮助
您应该从一个与 "product" 实体只有多对一关系的实体开始。假设我们称这个实体为 "ProductContainer".
然后您为 ProductContainer 创建一个表单,其中只有一个类型为 'collection' 的字段,这将为您创建一个产品列表。
您可以按照本教程进行操作:http://symfony.com/doc/current/cookbook/form/form_collections.html
我在 Symfony 中创建了一个这样的表单:
$form = $this->createFormBuilder($template)
->add('product1', 'text')
->add('product2', 'text')
->add('save', 'submit')
->getForm();
现在这是我的树枝:
{{ form_start(form) }}
{% for i in 1..2 %}
<div class="col-md-3">
<div class="product">
<div class="name">
{{ form_label(form.product{{ i }} ) }}
{{ form_errors(form.product{{ i }} ) }}
{{ form_widget(form.product{{ i }} ) }}
</div>
</div>
</div>
{% endfor %}
{{ form_end(form) }
主要思想是遍历 for 并获得一个新的 form.product<X>
每个循环。
我不能让它工作,我什至不知道它是否可以通过这种方式完成。有什么想法吗?
为此,我建议您使用 Collection 类型。但如果你想按照自己的方式去做,你应该这样做:
{{ form_start(form) }}
{% for i in 1..2 %}
<div class="col-md-3">
<div class="product">
<div class="name">
{{ form_label( attribute(form, 'product' ~ i) ) }}
{{ form_errors( attribute(form, 'product' ~ i) ) }}
{{ form_widget( attribute(form, 'product' ~ i) ) }}
</div>
</div>
</div>
{% endfor %}
{{ form_end(form) }
你是对的,它可能行不通。有关信息,Twig 中的连接符号是“~”。 在您的情况下,如果您的实体应该有 2 个或更多 "products",您应该使用集合而不是手动创建每个产品。 在您的实体中,您会有类似
的东西/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
protected $products;
并且在产品实体上,您将拥有
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
然后在您的第一个实体 __constructor 或您的控制器中迭代创建任意数量的产品并将它们添加到实体中。
在您的表单中,您只需添加:
$builder->add('products', 'collection');
并且您可以在 Twig 中对其进行迭代。
希望这对您有所帮助
您应该从一个与 "product" 实体只有多对一关系的实体开始。假设我们称这个实体为 "ProductContainer".
然后您为 ProductContainer 创建一个表单,其中只有一个类型为 'collection' 的字段,这将为您创建一个产品列表。
您可以按照本教程进行操作:http://symfony.com/doc/current/cookbook/form/form_collections.html