Symfony2 自定义表单选择选项

15

我试图创建一个简单的表单,以添加带有名称和颜色的活动。

所以我想使用颜色数组来制作一个列表,目前它可以运行,我已经拥有了颜色的名称。
我可以向我的选择标签添加任何属性:

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

输出结果将类似于以下内容:

<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> 

但我如何向

6个回答

10

通常情况下,字段的默认数据取决于存储在对象中的值。例如,如果

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

那么具有标签"red"和"green"的条目"1"和"2"将默认显示为已选中。在将该对象传递给表单之前,您也可以将此值存储在对象中:

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

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

最后一种可能性是通过传递"data"选项来覆盖存储在对象中的默认值:

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

3

在此处使用数据选项,如下所述,以进行selected="selected"的选择:

http://symfony.com/doc/current/reference/forms/types/field.html

对于您的情况,可能是这样的:

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

新元素是 数据,将选择数组的编号设置为选定属性。

1

在Symfony中,每个Field都继承自抽象字段类型,其中有一个data选项,您可以在其中传递默认选项。

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


如何使用 data- 属性?我该如何将其传递给 FormBuilder?请详细解释。 - Dariush Jafari
只需将 customattr 重命名为 data-customattr,以符合HTML标准。 - moonwave99
我想在我的option标签中使用customattr,而不是在select标签中使用。我该如何通过FormBuilder实现这一点? - Dariush Jafari
1
你在问题中没有说明这一点 - 尝试覆盖视图[这里有一些提示]。 - moonwave99

1

1
在表单类方法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表单-在选择字段类型中添加属性标签选项


1
为了在选项中添加自定义属性并基于该选项的值,考虑使用ChoiceType字段的choice_attr选项。例如,如果您想将json编码实体传递给选项,可以使用以下代码:
'choice_attr' => function($val, $key) {
   return ['data-object' => json_encode($val)];
},

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接