在Symfony FormBuilder中添加类来选择选项


Add class to select option in Symfony FormBuilder

在我的Symfony 2应用程序中,我使用FormBuilder创建一个用于选择生成文档中包含的数据的表单。

$typeChoices = [
    '001' => 'first factor',
    '002' => 'another factor',
    '003' => 'some surcharge',
    '004' => 'custom discount',
    '005' => 'another surcharge'
];
$formDownload = $this->createFormBuilder(array())
    ->add('category', 'entity', array(
        'class' => 'MyApp'CategoryBundle'Entity'Category',
        'choice_label' => 'name'
    ))
    ->add('type', 'choice', array(
        'choices' => $typeChoices,
        'multiple' => true
    ))
    ->add('download', 'submit', array(
        'attr' => array(
            'class' => 'btn-primary'
        ),
    ))
    ->setAction($this->generateUrl('myapp_data_download'))
    ->getForm();

$typeChoices数据是从EntityRepository加载的-我只是简化了这个演示的代码。

这样,生成的选择框如下:

<select multiple="multiple" class="form-control" required="required" name="form[type][]" id="form_type">
    <option value="001">first factor</option>
    <option value="002">another factor</option>
    <option value="003">some surcharge</option>
    <option value="004">custom discount</option>
    <option value="005">another surcharge</option>
</select>

如何将class属性添加到每个option ?它必须基于来自EntityRepository的原始数据的属性来创建。到目前为止,当使用FormBuilder时,我无法将class属性添加到option s,我想避免手动创建表单标记。

Symfony 2.7新增功能:选择表单类型重构

在Symfony 2.7中,这个表单类型被完全重构为支持动态生成标签、值、索引和属性。多亏了新的选项choice_label, choice_name choice_value , choice_attr , group_by choices_as_values .

例如可以生成动态选择标签

例子:

$builder->add('attending', 'choice', array(
    'choices' => array(
        'yes' => true,
        'no' => false,
        'maybe' => null,
    ),
    'choices_as_values' => true,
    'choice_label' => function ($allChoices, $currentChoiceKey) {
        return 'form.choice.'.$currentChoiceKey;
    },
));

在您的示例中,您希望操作每个选项的属性类

例子:

$builder->add('attending', 'choice', array(
    'choices' => array(
        'Yes' => true,
        'No' => false,
        'Maybe' => null,
    ),
    'choices_as_values' => true,
    'choice_attr' => function ($allChoices, $currentChoiceKey) {
        if (null === $currentChoiceKey) {
            return array('class' => 'text-muted');
        }
    },
));

在Form class中添加Symfony'Component'Form'AbstractType::finishView

/**
 * @param FormView $view
 * @param FormInterface $form
 * @param array $options
 */
public function finishView(FormView $view, FormInterface $form, array $options)
{
    parent::finishView($view, $form, $options);
    $additionalAttributes = array();
    // myMethod - object method $choice, returns a value that must be replaced by attribute
    foreach ($view->children['type']->vars['choices'] as $id => $choice) {
        $additionalAttributes[$id] = array(
            'class' => $this->propertyAccessor->getValue($choice->data, 'myMethod'),
        );
    }
    foreach ($view->children['type']->children as $id => $child) {
        $child->vars['attr'] = array_replace(
            isset($child->vars['attr']) ? $child->vars['attr'] : array(),
            $additionalAttributes[$id]
        );
    }
}

Symfony2 Form -在选择类型

中为标签选项添加属性