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

时间:2015-07-24 10:32:52

标签: php 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的原始数据的属性创建它。到目前为止,我在使用class时无法向option添加FormBuilder属性,我希望避免手动创建表单标记。

2 个答案:

答案 0 :(得分:2)

Symfony 2.7中的新内容:Choice form type refactorization

  

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

希望它会有所帮助。

答案 1 :(得分:1)

在Form类

中添加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 - adding an attribute to the tag option in the select type