Create array input field with form builder symfony2

27,237

Solution 1

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.

Solution 2

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]',
))

Solution 3

Also you can ovveride field in TWIG. Example:

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

Where step is your index.

Share:
27,237
markoub
Author by

markoub

Updated on July 09, 2022

Comments

  • markoub
    markoub almost 2 years

    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?

  • markoub
    markoub over 11 years
    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.
  • Mats Rietdijk
    Mats Rietdijk over 11 years
    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.
  • markoub
    markoub over 11 years
    I added explanation in original post... :-)
  • Mats Rietdijk
    Mats Rietdijk over 11 years
    I don't think that you can just skip indexes like that. But the rest it is just what the collection type does.
  • markoub
    markoub over 11 years
    Thanks, I started using collections.