php 如何将类属性设置为 Symfony2 表单输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6734821/
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-26 01:06:59  来源:igfitidea点击:

How to set a class attribute to a Symfony2 form input

phpformssymfonyinputformbuilder

提问by Jean-Philippe Bond

How can I set the HTMLclassattribute to a form <input>using the FormBuilderin Symfony2?

如何使用in将HTMLclass属性设置为表单?<input>FormBuilderSymfony2

Something like this:

像这样的东西:

->add('birthdate', 'date',array(
      'input' => 'datetime',
      'widget' => 'single_text',
      'attr' => array(
          'class' => 'calendar'
      )
 ))

 {{ form_widget(form.birthdate) }}

I want this inputfield with the attribute classset to calendar

我希望此input字段的属性class设置为日历

回答by Problematic

You can do this from the twig template:

您可以从树枝模板执行此操作:

{{ form_widget(form.birthdate, { 'attr': {'class': 'calendar'} }) }}

From http://symfony.com/doc/current/book/forms.html#rendering-each-field-by-hand

来自http://symfony.com/doc/current/book/forms.html#rendering-each-field-by-hand

回答by Acyra

You can do it with FormBuilder. Add this to the array in your FormBuilder:

您可以使用 FormBuilder 来完成。将此添加到 FormBuilder 中的数组中:

'attr'=> array('class'=>'span2')

回答by JeanValjean

The answer by Acyra lead the right way if you want to set attributes inside the controller, but has many inaccuracies.

如果您想在控制器内部设置属性,Acyra 的答案是正确的,但有很多不准确之处。

Yes, you can do it directly with the FormBuilder by using the attrattribute (introduced herefor the 2.1 version and here for the 2.0) to the array of options as follows:

是的,您可以通过对选项数组使用attr属性(此处为 2.1 版本和此处为 2.0引入)直接使用 FormBuilder 执行此操作,如下所示:

->add('birthdate', 'date',array(
      'input' => 'datetime',
      'widget' => 'single_text',
      'attr' => array('class'=>'calendar')
 ))

It is not true that the "functionality is broken". It works very well!

“功能被破坏”是不正确的。它运作良好!

It is not true that Symfony2 applies the HTML classattribute to both the label and the input (at least from the 2.1 version).

Symfony2 不对class标签和输入都应用 HTML属性(至少从 2.1 版本开始)。

Moreover, since the attrattribute is an array itself, you can pass any HTML attribute you want to render for the field. It is very helpful if you wanna pass the HTML5 data-attributes.

此外,由于attr属性本身是一个数组,因此您可以传递要为该字段呈现的任何 HTML 属性。如果您想传递 HTML5data-属性,这将非常有用。

回答by segli

You can add it in the options of your form class:

您可以将其添加到表单类的选项中:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\MyEntity',
        'attr' => array(
            'class' => 'form-horizontal'
        )
    ));
}

回答by Kiran

{{ form_widget(form.content, { 'attr': {'class': 'tinyMCE', 'data-theme': 'advanced'} })  }}

回答by Alexandre Velo

Like this:

像这样:

{{ form_widget(form.description, { 'attr': {'class': 'form-control', 'rows': '5', 'style': 'resize:none;'} }) }}

回答by Kim

You can to this in Twig or the FormClass as shown in the examples above. But you might want to decide in the controller which class your form should get. Just keep in mind to not have much logic in the controller in general!

您可以在 Twig 或 FormClass 中执行此操作,如上面的示例所示。但是您可能希望在控制器中决定您的表单应该获得哪个类。请记住,通常控制器中没有太多逻辑!

    $form = $this->createForm(ContactForm::class, null, [
        'attr' => [
            'class' => 'my_contact_form'
        ]
    ]);

回答by Javier Morillo

Renders the HTML widget of a given field. If you apply this to an entire form or collection of fields, each underlying form row will be rendered.

呈现给定字段的 HTML 小部件。如果您将此应用于整个表单或字段集合,则将呈现每个底层表单行。

{# render a field row, but display a label with text "foo" #} {{ form_row(form.name, {'label': 'foo'}) }}

{# render a field row, but display a label with text "foo" #} {{ form_row(form.name, {'label': 'foo'}) }}

The second argument to form_row() is an array of variables. The templates provided in Symfony only allow to override the label as shown in the example above.

form_row() 的第二个参数是一个变量数组。Symfony 中提供的模板只允许覆盖如上例所示的标签。

See "More about Form Variables" to learn about the variables argument.

请参阅“有关表单变量的更多信息”以了解有关 variables 参数的信息。