在表单中重复相同表单字段的最佳方法是什么?
我希望用户提交多个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
}
}
理想情况下,我希望用户输入他想要的多个字段......
1-有没有办法避免在这里重复代码?
2-如何将这些名称/电话存储在基础对象中?
3-我可以将其存储为数组,但仍然应用一些验证规则吗?
答案 0 :(得分:2)
尝试使用:
$builder->add('phones', 'collection', array('type' => new PhoneType()));
并在表单构建器中'allow_add' => true
。