Create an array input field with symfony2 form builder

I'm having trouble using Form builder in Symfony2. More precisely, I need an input field, which is an html array, but I cannot create it using createFormBuilder-> add. Here is what I tried:

$attributesForm = $this->createFormBuilder() ->add('attribute[0]', 'text') ... 

And so on, but I get the following exception:

The name "attribute [0]" contains invalid characters. Names must begin with a letter,> numbers or underscores and contain only letters, numbers, numbers, underscores ("_"), hyphens> ("-"), and colons (":").

Is there any good solution or do I need to create fields manually?

Thanks in advance!

EDIT: clarify this further ... I want to create something like this:

 <div id="msoft_adminbundle_offertype"> <div>Name <input type="text" name="name"></div> <div>...</div> <div>Attribute 0 <input type="text" name="attribute[0]"></div> <div>Attribute 1 <input type="text" name="attribute[1]"></div> <div>Attribute 3 <input type="text" name="attribute[3]"></div> <ul> </ul> <p> <button type="submit">Edit</button> </p> 

reference

+6
source share
3 answers

You can create an array of input fields using the type 'collection' -field.

The documentation on how to use it can be found here:

Prefabricated Documentation

If this is not clear enough or you still have questions, I will be happy to help you with them.

+5
source

As in previous answers, use a collection type or nested form, where each field corresponds to one array entry. And in cases where you cannot / do not want to do this, you can do the following:

 ->add('attribute_0', 'text', array( 'property_path' => 'attribute[0]', )) 
+14
source

You can also use ovveride in TWIG. Example:

  {{ form_row(form[field_name],{ 'full_name': 'attribute[' ~ step ~ ']' })}} 

Where is the step of your index.

+1
source

All Articles