Create a multicolor selectbox on Symfony2

Posted by Ninsuo on Stack Overflow See other posts from Stack Overflow or by Ninsuo
Published on 2012-12-06T16:28:28Z Indexed on 2012/12/06 17:04 UTC
Read the original article Hit count: 236

Filed under:
|
|
|

I try to create the following field :

<select name="test">
    <option value="1">a</option>
    <option value="2">b</option>
    <option value="3" style="font-weight: bold; color: red;">c</option>
    <option value="4">d</option>
    <option value="5">e</option>
</select>

This creates a 2-color selectbox :

enter image description here

But in Symfony2, I do not know how to apply a class to a single option.

If in my view I do :

{{
    form_widget(myForm.test, {
        'attr': {
            'class': 'red',
        }
    }) 
}}

Or if in my form I do :

    $builder->add('test', 'choice', array(
            'required' => true,
            'choices' => array('a', 'b', 'c', 'd', 'e'),
            'attr' => array('class' => 'red'),
    ));

The attribute is stored into the <select> tag and apply to the whole selectable values.

How do I apply a class to an <option> in Symfony2 ?

© Stack Overflow or respective owner

Related posts about php

Related posts about css