symfony2 customize form select options

≯℡__Kan透↙ 提交于 2019-12-03 05:32:43

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

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

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.

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

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

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

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