symfony2 customize form select options

不想你离开。 提交于 2019-12-04 10:32:23

问题


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.


回答1:


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),
));



回答2:


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




回答3:


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.




回答4:


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

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




回答5:


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




回答6:


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)];
},


来源:https://stackoverflow.com/questions/12217431/symfony2-customize-form-select-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!