8

I'm having a trouble with using Form builder in Symfony2. To be exact, I need input field that is html array, but I can't create it with 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 illegal characters. Names should start with a letter, >digit or underscore and only contain letters, digits, numbers, underscores ("_"), hyphens >("-") and colons (":").

Is there any nice solution or I have to create fields manually?

Thanks in advance!

EDIT: to clarify this further... I want something like this to be generated:

<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>

Help?

3 Answers 3

17

As the previous answer states, use the collection type or a nested form, where each field corresponds to one entry of the array. And in cases where you can't/don't want to do that, you can do the following:

->add('attribute_0', 'text', array(
    'property_path' => 'attribute[0]',
))
1
  • Thanks, I started using collections.
    – markoub
    Nov 9, 2012 at 10:43
7

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

Documentation about how to use it can be found here:

Collection documentation

If that isn't clear enough or you still have questions I will gladly help you with them.

4
  • Sorry, additional notes... :-/ It turns out I need something simpler... I have very specific list of attributes user can set for each offer, and I just want them listed as "attribute[0]", "attribute[1]", and so on. Actually, I also need to use different array indexes, but it is not that important. So, to sum up, this is more complicated, it allows me to add user option to create new forms inside of forms, with set of fields. I just want to get instead of names "attribute_1", "attribute_2" and so on, attribute array.
    – markoub
    Nov 7, 2012 at 8:47
  • It's not clear to me what you actually want now. if you could provide sample code (html) of the form as you want it to be generated I might be able to help you further. Nov 7, 2012 at 10:36
  • I added explanation in original post... :-)
    – markoub
    Nov 7, 2012 at 15:37
  • I don't think that you can just skip indexes like that. But the rest it is just what the collection type does. Nov 7, 2012 at 16:13
2

Also you can ovveride field in TWIG. Example:

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

Where step is your index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.