6

在 Symfony 2.7 之前,字段的attrchoice只应用于字段本身,<select>即被渲染的元素。我用它来为这个元素应用类来设置它的样式。

在 Symfony 2.7 中,这种行为发生了改变。现在,元素的所有<option><select>元素也获得相同的属性(更改的提交)和类。


为了澄清,让这成为代码:

<?php echo $view['form']->widget($form['myField'], ['attr' => ['class' => "text ui-widget-content ui-corner-all"]]); ?>

那么这是 Symfony <=2.6 的输出:

<select class="text ui-widget-content ui-corner-all" name="myField">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

这是 Symfony >= 2.7 的输出:

<select class="text ui-widget-content ui-corner-all" name="myField">
    <option value="1" class="text ui-widget-content ui-corner-all">Option 1</option>
    <option value="2" class="text ui-widget-content ui-corner-all">Option 2</option>
</select>

我应用的类不适合<option>元素,因为它们为实际字段定义了边框等。请注意,这些是由 jQuery UI 定义的类,因此我不能轻易更改它们的定义。

避免将这些类应用于字段的所有<option>元素choice同时仍将其应用于元素的最简单方法是什么<select>

4

2 回答 2

5

感谢choice_attr@user2268997 的评论,我找到了相关的博客文章Symfony 2.7 中的新功能:选择表单类型重构,其中详细介绍了(截至目前未记录的)choice_attr选项的使用。

似乎 Symfony在渲染字段时将属性choice_attr与in 合并。attr这意味着我们需要class覆盖choice_attr.

我尝试在我定义的代码旁边的代码中执行此操作,attr但没有运气。看来您需要在表单类型定义中执行此操作。choice_attr这是添加选项后我的表单的摘录:

namespace MyBundle\Form;

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
        ->add('roles',
            'entity',
            [
                'class' => 'MyBundle:Role',
                'choice_label' => 'name',
                'multiple' => true,
                'choice_attr' => function () { return ["class" => ""]; }
            ]);
}

结果如我所愿。我可能还会将其重构为我自己的自定义表单类型,因此我不需要在我的包中重复它。


我现在决定创建一个choice具有上述所需行为的自定义类型,并在整个应用程序中使用该类型。

这是我的选择类型:

use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ChoiceNoOptAttrType extends ChoiceType {
    public function configureOptions(OptionsResolver $resolver) {
        parent::configureOptions($resolver);

        $resolver->setDefault("choice_attr", function () { return ["class" => ""]; });
    }
}

我不想重构所有现有的表单来使用这种新类型,所以我选择用我的替换 Symfony 提供的选择类型。这可以通过修改choice表单类型的服务配置来实现。为此,我为我的包创建了一个编译器通行证。

进一步阅读:创建编译器通行证

namespace MyBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class MyCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $definition = $container->getDefinition("form.type.choice");
        $definition->setClass('MyBundle\Form\ChoiceNoOptAttrType');
    }
}

现在剩下要做的就是在包中注册编译器传递。

进一步阅读:如何在 Bundles 中使用编译器通道

namespace MyBundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use MyBundle\DependencyInjection\Compiler\MyCompilerPass;

class MyBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        $container->addCompilerPass(new MyCompilerPass());
    }
}

就是这样。现在我的所有choice字段都在使用我的自定义类,它确保设置的 CSS 类attr不会传播到我的<option>元素。

于 2015-08-17T14:33:13.220 回答
1

可能有一个更简单的解决方案,但您可能想看看Form Themes。覆盖choice_widget_options 的模板,以便类不会应用于选项标签。

{%- block choice_widget_options -%}
    {% for group_label, choice in options %}
        {%- if choice is iterable -%}
            <optgroup label="{{ choice_translation_domain is sameas(false) ? group_label : group_label|trans({}, choice_translation_domain) }}">
                {% set options = choice %}
                {{- block('choice_widget_options') -}}
            </optgroup>
        {%- else -%}
            {% set attr = choice.attr %}
            <option value="{{ choice.value }}" {# DELETE THIS PART: {{ block('attributes') }}#}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is sameas(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}</option>
        {%- endif -%}
    {% endfor %}
{%- endblock choice_widget_options -%}
于 2015-08-17T13:57:20.207 回答