7

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.

8
  • 1
    Do you need to put attributes on options or you want to group your options? It seems that you are trying to group them. Commented Oct 16, 2013 at 3:10
  • Both, i found some grouping stuff in the source not sure how to do it. But i do also need to add some "data-id" attributes to the options as well Commented Oct 16, 2013 at 3:17
  • 2
    Attributes on option are not supported.. If you build a nested array of values you can create group of options (the key being the name of group). If you really need a way to add "data-id" you should probably create a new FormType that extends the Entity form type with a custom template. Commented Oct 16, 2013 at 3:45
  • 1
    You tried the "group by" option? Basically you provide the field name that can be used to group entities together Commented Oct 16, 2013 at 4:21
  • 1
    This documentation should help you out, not saying it's easy but it's worth learning how it works. Commented Oct 16, 2013 at 4:49

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

2 years after I needed it they add it. Thanks for updating it
0

You might need to create own widget, extended from 'choice' widget.

So you'll be able to use custom render on your options, and you'll be able to setup whatever you want in attributes to each option.

NB: I was looking the same, but 've used simple 'choice', and in template I've added JS var with extra options (array with values from the 'select', and specified extra attributes). I apply each extra attributes based on values of he option on page load event.

Comments

-1

Please use this below code for attribute set in symfony2

->add('birthdate', 'date',array(
      'input' => 'datetime',
      'widget' => 'single_text',
      'attr' => array('class'=>'calendar')
 ))

1 Comment

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.