2

I am trying to add some CSS classes to specific options of my select.

It is no problem to add it to the select itself with the attr:

'attr'=>array('style'=>'width:300px', 'class'=>'class'),

But I want to add the class to the options itself:

<select name="select">
   <option class="x" value="1">red</option>
   <option value="2">blue</option>
   <option class="y" value="3">green</option>
</select> 

I haven't found any way, yet. I investigated already the ChoiceType and the ChoiceList implementations.

My favorite way would be to add them on server side.

Thanks for any hints.

CSchulz
  • 10,882
  • 11
  • 60
  • 114

3 Answers3

3

Finally the solution I am waiting for will be implemented in 2.7 (source).

choice_attr

Returns the additional HTML attributes for choices. Can be an array, a callable (like for "choice_label") or a property path. If an array, the key of the "choices" array must be used as keys.

// array
$builder->add('attending', 'choice', array(
    'choices' => array(
        'Yes' => true,
        'No' => false,
        'Maybe' => null,
    ),
    'flip_choices' => true,
    'choice_attr' => array(
        'Maybe' => array('class' => 'greyed-out'),
    ),
));
CSchulz
  • 10,882
  • 11
  • 60
  • 114
  • In Symfony 3, this still works, but it is interesting how it's given in the Docs: "This can be an array of attributes (**if they are the same for each choice**)"... so it seems like you could use one and the same entry in this array, for setting up a value for all choices. But then, 1 line lower it commands that "the keys of the choices array **must be used as keys**". I'd expect that - if you always want the same class for all choices - you could just use *'choice_attr' => ['class' => 'sameclass']* – userfuser Sep 28 '16 at 08:46
1

I am not sure what you are looking for but as I understood via Florian Peschka's comment that you wanted to add custom class to specific(particular) option then here is what I tried..

JQuery

$('select>option:eq(1)').addClass("custom_class");

Here eq() method : Reduce the set of matched elements to the one at the specified index. Just like Array it's index starts from 0 so if you wanted to select second option then write eq(1).

In above code I have added custom_class to second option..

HTML:

<select name="select">
   <option class="x" value="1">red</option>
   <option value="2">blue</option>  # suppose you wanted to add custom class in this option
   <option class="y" value="3">green</option>
</select> 

Which will now be looks like

<option class="custom_class" value="2">blue</option>

Working Demo

To check in demo you can right click on drop-down and select Inspect element with firebug. enter image description here

If this is not what you are looking for then please update your question with your Expected output..with description

Community
  • 1
  • 1
Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
0

You will have to override the layout of specific widgets in your form . see the example How can I add css classes to specific symfony2 form choices?

Community
  • 1
  • 1
Ajeet Varma
  • 726
  • 4
  • 20