Dariush Jafari
Dariush Jafari

Reputation: 5443

symfony2 customize form select options

I'm trying to do a simple form to add an activity with a name and a color.

So I want to make a list with some an array of color, for now it is working I have the name of the color.
I can add any attribute to my select tag:

$form = $this->createFormBuilder($myclass)
->add('Colors','choice',array('label'=>'select some colors',
            'multiple'=>true,
            'choices'=>array(1=>'red', 2=>'blue', 3=>'green'),
            'attr'=>array('style'=>'width:300px', 'customattr'=>'customdata')
            ));

The output will be something like this:

<select name="select" style="width: 300px;" multiple="multiple" customattr="customdata">
   <option value="1">red</option>
   <option value="2">blue</option>
   <option value="3">green</option>
</select> 

But how can I add selected="selected" and any attribute I want to my select options ? like this:

<select name="select" style="width: 300px;" multiple="multiple" customattr="customdata">
   <option style="background-color: #F00;" value="1" selected="selected">red</option>
   <option style="background-color: #00F;" value="2" selected="selected">blue</option>
   <option style="background-color: #0F0;" value="3">green</option>
</select> 

My question is: how can I add custom attr for option tag (not for select tag) by symfony FormBuilder.
NOTICE: I don't want to use JavaScript. I want to use symfony2 FormBuilder to customize my select options.

Upvotes: 15

Views: 34327

Answers (6)

Pavel Galaton
Pavel Galaton

Reputation: 593

In order to add your custom attribute on the choice, based on the value of that choice, consider using choice_attr option of the ChoiceType field.

For example if you want to pass json encoded entity to the option, you can use something like this:

'choice_attr' => function($val, $key) {
   return ['data-object' => json_encode($val)];
},

Upvotes: 1

Evgeniy Budanov
Evgeniy Budanov

Reputation: 196

Add in form class method Symfony\Component\Form\AbstractType::finishView

public function finishView(FormView $view, FormInterface $form, array $options)
{
    parent::finishView($view, $form, $options);

    $additionalAttributes = array();

    // myMethod - method $choice, returns a value that is to be substituted into the attribute
    foreach ($view->children['orders']->vars['choices'] as $id => $choice) {
        $additionalAttributes[$id] = array(
            'data-cost' => $this->propertyAccessor->getValue($choice->data, 'myMethod'),
            'disabled' => 'disabled',
        );
    }

    foreach ($view->children['orders']->children as $id => $child) {
        $child->vars['attr'] = array_replace(
            isset($child->vars['attr']) ? $child->vars['attr'] : array(),
            $additionalAttributes[$id]
        );
    }
}

Symfony2 Form – add attribute tag option in select field type

Upvotes: 1

Henry
Henry

Reputation: 7891

Looks like Symfony 2.7 will add support to make this easier:

https://github.com/symfony/symfony/pull/14050

Upvotes: 1

fpanini
fpanini

Reputation: 94

use the data option as described here fo selected="selected" : http://symfony.com/doc/current/reference/forms/types/field.html

in ur case could be like this

$form = $this->createFormBuilder($myclass)
->add('Colors','choice',array('label'=>'select some colors',
            'multiple'=>true,
            'choices'=>array(1=>'red', 2=>'blue', 3=>'green'),
            'attr'=>array('style'=>'width:300px', 'customattr'=>'customdata'),
            'data'=> 1
            ));

the new element is data setting the number of the choice array as selected attribute

Upvotes: 3

Bernhard Schussek
Bernhard Schussek

Reputation: 4841

Usually, the default data of a field is determined by the value stored in your object. For example, if

class MyClass
{
    private $Colors = array(1, 2);
}

then the entries "1" and "2" (with the labels "red" and "green") will be displayed as selected by default. You could also store this value in the object before passing it to the form:

$myObject->Colors = array(1, 2);

$form = $this->createFormBuilder($myObject)
    ...

The last possibility is to override the default value stored in the object by passing the "data" option:

$builder->add('Colors', 'choice', array(
    'label' => 'select some colors',
    'multiple' => true,
    'choices' => array(1 => 'red', 2 => 'blue', 3 => 'green'),
    'attr' => array('style' => 'width:300px', 'customattr' => 'customdata'),
    'data' => array(1, 2),
));

Upvotes: 10

moonwave99
moonwave99

Reputation: 22817

Every Field in symfony inherits from abstract field type, which has a data option, in which you can pass default option.

By the way, don't pass style stuff, and for custom attrs use data-* attributes.

Upvotes: 1

Related Questions