【问题标题】:Symfony2 forms how to set class for choice input fields?Symfony2表单如何为选择输入字段设置类?
【发布时间】:2014-01-04 20:11:27
【问题描述】:

如上如何设置输入字段的类?

到目前为止我得到了什么:表单构建器类:

    $builder->add('rate', 'choice', array(
                'choices' => array(
                    1 => 1,
                    2 => 2,
                    3 => 3,
                    4 => 4,
                    5 => 5,
                ),
                'multiple' => false,
                'expanded' => true,
                'attr' => array('disabled' => 'disabled', 'class' => 'star'),
            ))
            ->add('save', 'submit');

所以这里是:

'attr' => array('disabled' => 'disabled', 'class' => 'star'),

不为每个输入比率按钮设置潜水类

这里是生成的html:

<form action="/app_dev.php/pl/applications/vote/Google%20Earth%20DirectX" method="post">

    <div id="vote_rate"     disabled="disabled" class="star"><label class="radio"><input type="radio" id="vote_rate_0" name="vote[rate]" required="required"    class="" value="1" />
            1
        </label><label class="radio"><input type="radio" id="vote_rate_1" name="vote[rate]" required="required"    class="" value="2" />
            2
        </label><label class="radio"><input type="radio" id="vote_rate_2" name="vote[rate]" required="required"    class="" value="3" checked="checked" />
            3
        </label><label class="radio"><input type="radio" id="vote_rate_3" name="vote[rate]" required="required"    class="" value="4" />
            4
        </label><label class="radio"><input type="radio" id="vote_rate_4" name="vote[rate]" required="required"    class="" value="5" />
            5
        </label></div>
  <div><button type="submit" id="vote_save" name="vote[save]">Save</button></div><input type="hidden" id="vote__token" name="vote[_token]"    class=" not-removable form-control" value="35bc6584f16d20796946cac15f9e18e62dec3cab" />
</form>

我们可以看到我的构建器正在为 div vote_rate 设置类,我的输入字段有 class="",我希望它是 class="star",我的问题是如何在构建器或其他方式中设置它?

【问题讨论】:

  • 一种方法,但不建议使用 jquery 遍历所有收音机并添加一个类
  • symfony2 没办法?
  • 非常感谢。它确实帮助了我!我使用了 radio_widget,效果很好!
  • 如果将您的问题的解决方案发布为答案,将对未来的访问者有所帮助

标签: php forms class symfony


【解决方案1】:

choice_attr 选项将允许您为每个选项(在您的示例中为单选)添加属性(包括类)。

        'choice_attr' => function($val, $key, $index) {
            return ['disabled' => 'disabled', 'class' => 'star'];
        },

您可以使用$val$key$index 来确定要返回的内容。

【讨论】:

    【解决方案2】:

    如果要设置输入字段类,我会覆盖无线电小部件

    在我的表单模板中我添加了:

    {% form_theme form 'ApplicationsBundle:Form:fields.html.twig' %}
    

    这是我的 fields.html.twig:

    {% block radio_widget %}
    {% spaceless %}
    <input type="radio" class="star" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} disabled="disabled"/>
    {% endspaceless %}
    {% endblock radio_widget %}
    

    为了使其正常工作,您需要将表单称为:

    {{ form_widget(form) }}
    

    【讨论】:

      猜你喜欢
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多