php symfony2 自定义表单选择选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12217431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:03:50  来源:igfitidea点击:

symfony2 customize form select options

phpsymfonysymfony-forms

提问by Dariush Jafari

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:

但是我怎样才能selected="selected"在我的选择选项中添加我想要的任何属性?像这样:

<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 optiontag (not for selecttag) by symfony FormBuilder.
NOTICE:I don't want to use JavaScript. I want to use symfony2 FormBuilder to customize my select options.

我的问题是:如何通过 symfony FormBuilder为option标签(不是为select标签)添加自定义属性。
注意:我不想使用 JavaScript。我想使用 symfony2 FormBuilder 来自定义我的选择选项。

采纳答案by Bernhard Schussek

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:

然后条目“1”和“2”(带有标签“红色”和“绿色”)将默认显示为选中状态。您还可以在将其传递给表单之前将此值存储在对象中:

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

回答by fpanini

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

使用此处描述的数据选项 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 datasetting the number of the choice array as selected attribute

新元素是数据设定的选择阵列的数量作为选择的属性

回答by Henry

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

看起来 Symfony 2.7 将添加支持以使其更容易:

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

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

回答by Evgeniy Budanov

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

添加表单类方法 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

Symfony2 Form – 在选择字段类型中添加属性标签选项

回答by Pavel Galaton

In order to add your custom attribute on the choice, based on the value of that choice, consider using choice_attroption of the ChoiceTypefield.

为了在选择上添加自定义属性,基于该选择的值,请考虑使用ChoiceType字段的choice_attr选项。

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

例如,如果您想将 json 编码的实体传递给选项,您可以使用以下内容:

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

回答by moonwave99

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

Fieldsymfony 中的每个都继承自抽象字段 type,它有一个data选项,您可以在其中传递默认选项。

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

顺便说一句,不要传递style东西,对于自定义data-*属性,请使用属性。