php Symfony2,如何使表单标签类/属性与​​其输入不同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10919619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:21:33  来源:igfitidea点击:

Symfony2, How to make a form label class/attr different than its input?

phpformssymfony

提问by pmoubed

I would like to build a form with label and inputs, but the class of them should be different. Code below creates the label for the input with same attr:

我想用标签和输入构建一个表单,但它们的类应该不同。下面的代码为具有相同属性的输入创建标签:

 public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
                ->add('hours', null ,
                  array('attr'=> 
                             array(
                                 'placeholder'=>'Working Hours',
                                 'class'=>'ui-spinner-box') ) )
    }

In my code above the ui-spinner-boxwill be outputted for both label and input. It will even put placeholder for its label.

在我上面的代码中,ui-spinner-box将输出标签和输入。它甚至会为其标签放置占位符。

So how to make it create attrfor label separately so I can output something like below :

那么如何attr单独为标签创建它,以便我可以输出如下内容:

<label class="MYCLASSFOR_LABEL"   for="input_id">Hours</label>
<input class="MYCLASSFOR_INPUTS"  type="text" id="input_id" name="" value="" >

回答by a.aitboudad

As mentioned in the documentation:

文档中所述:

  • attr : A key-value array that will be rendered as HTML attributes on the field
  • label_attr: A key-value array that will be rendered as HTML attributes on the label
  • attr : 将在字段上呈现为 HTML 属性的键值数组
  • label_attr:一个键值数组,将在标签上呈现为 HTML 属性

You can set those attributes in twig template or in form builder:

您可以在树枝模板或表单构建器中设置这些属性:

Twig template:

树枝模板:

  • for symfony 2.1 and newer use:

    {{ form_label(form.hours, null, {'label_attr': {'class': 'foo'}}) }}
    
  • in the legacy symfony 2.0 it used to be

    {{ form_label(form.hours, { 'label_attr': {'class': 'MYCLASSFOR_LABEL'} }) }}
    {{ form_widget(form.hours, { 'attr': {'class': 'MYCLASSFOR_INPUTS'} }) }}
    
  • 对于 symfony 2.1 和更新的使用:

    {{ form_label(form.hours, null, {'label_attr': {'class': 'foo'}}) }}
    
  • 在旧版 symfony 2.0 中,它曾经是

    {{ form_label(form.hours, { 'label_attr': {'class': 'MYCLASSFOR_LABEL'} }) }}
    {{ form_widget(form.hours, { 'attr': {'class': 'MYCLASSFOR_INPUTS'} }) }}
    

Form builder

表单生成器

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('hours', null, array(
        'label_attr' => array('class' => 'MYCLASSFOR_LABEL'),
        'attr'       => array('class' => 'MYCLASSFOR_INPUTS'),
    ));
}

回答by Jeremy Warne

This may be new, but there's an easy way to do this now:

这可能是新的,但现在有一种简单的方法可以做到这一点:

$builder
    ->add('hours', null , array(
        'attr'=> 
            array(
                'placeholder'=>'Working Hours',
                'class'=>'MYCLASSFOR_INPUTS') 
        ) ,
        'label_attr' => array(
            'class' => 'MYCLASSFOR_LABEL'
        )
    );

The option you're looking for is label_attr.

您正在寻找的选项是label_attr

回答by Onshop

This works for me in Symfony 2.3:

这在 Symfony 2.3 中对我有用:

{{ form_row(form.hours,  {   'label': 'Hours:'
                            ,'label_attr': {'class': 'MYCLASSFOR_LABEL'}
                            ,'attr': {'class': 'MYCLASSFOR_INPUTS'} 
                        }
           )
}}

回答by ornj

The above is no longer correct, at least in the context I was using it. In Symfony 2.1 the solution is:

以上不再正确,至少在我使用它的上下文中。在 Symfony 2.1 中,解决方案是:

{{ form_label(form.item, label|default(null), { 'label_attr': { 'class': 'MYCLASS' } }) }}