在表单中重复相同表单字段的最佳方法是什么?
我希望用户提交多个Name / Phone number
行。
class contactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name1', 'text')
->add('phone1', 'text');
->add('name2', 'text')
->add('phone2', 'text');
->add('name3', 'text')
->add('phone3', 'text');
....etc
}
}
理想情况下,我希望用户输入他想要的字段。
这里有避免重复代码的方法吗?
2-如何将这些名称/电话存储在底层对象中?
3-我可以将其存储为数组,但仍然应用一些验证规则吗?
发布于 2012-10-29 19:59:39
试着使用:
$builder->add('phones', 'collection', array('type' => new PhoneType()));
以及表单生成器中的'allow_add' => true
。
看一看如何嵌入表单集合烹饪手册页面。
https://stackoverflow.com/questions/13132338
复制相似问题