What is the best way of repeating the same form fields within a form?
I’d like the user to submit multiple Name / Phone number rows.
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
}
}
Ideally, I would like the user to enter as many fields as he wants…
1- Is there a way to avoid repeating the code here?
2- How should I store these name/phone in the underlying object?
3- Can I store it as an array, but still apply some validation rules?
Try using:
And
'allow_add' => truein your Form Builder.Take a look at the How to Embed a Collection of Forms Cookbook page.