How to add custom attributes to option elements in symfony 2 form builder

10,337

Its possible for choice since 2.7. Look at here. With choice_attr.

Share:
10,337
Chase
Author by

Chase

Development has been a passion of mine for a very long time! They say if you love what you do then you never work a day in your life. Id say thats mostly true. I have opinions on almost everything and strong opinions on some things. I really enjoying hearing opposing views and discussing and debating the merits of all sides!

Updated on June 15, 2022

Comments

  • Chase
    Chase almost 2 years

    I am trying to add custom attributes to the option elements using the symfony2 form builder im im not sure that is natively possible. If its not I need to know how i would go about adding the functionality.

    Take the following form as an example:

    class FooForm extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('user','choice',array(
                'choices' => array(
                     'designers'=>'designers',
                     '1'=>'mike',
                     '2'=>'carroll',
                     'developers'=>'developers',
                     '3'=>'chase',
                     '4'=>'brett',
                     '5'=>'jordan',
                 )
            ));
        }
    }
    

    then when rendered i need it to look like:

    <select>
        <option value="" disabled="disabled">designers</option>
        <option value="1">mike</option>
        <option value="2">carroll</option>
        <option value="" disabled="disabled">developers</option>
        <option value="3">chase</option>
        <option value="4">brett</option>
        <option value="5">jordan</option>
    </select>
    

    what i would expect would be something like:

    class FooForm extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('user','choice',array(
                'choices' => array(
                     'designers'=>array(
                          'label'=>'designers',
                          'attr'=>arrry('disabled'=>'disabled')
                     ),
                     '1'=>'mike',
                     '2'=>'carroll',
                     'developers'=>array(
                          'label'=>'developers',
                          'attr'=>arrry('disabled'=>'disabled')
                     ),
                     '3'=>'chase',
                     '4'=>'brett',
                     '5'=>'jordan',
                 )
            ));
        }
    }
    

    But that doesnt work. So any help on this will be greatly appreciated.

  • Chase
    Chase over 9 years
    This does not answer the question, I was looking for a way to add attributes to each of the options in a choice field. Not on the parent field itself.
  • Chase
    Chase over 8 years
    2 years after I needed it they add it. Thanks for updating it