Symfony2 - FormBuilder - 在字段和输入中添加一个类

时间:2022-10-16 15:46:44

I want to add a class to certain input or label fields from within symfony2.

我想在symfony2中为某些输入或标签字段添加一个类。

I can do something like this in my form within Twig:

我可以在Twig中以我的形式做这样的事情:

<div class="row">
    {{ form_label(form.subject) }}
    {{ form_widget(form.subject, { 'attr': {'class': 'c4'} }) }}
</div>

Which works fine. But I have to setup the template for every form. And I have to break it down to the smallest possible output level. I actually want to use:

哪个工作正常。但我必须为每个表单设置模板。我必须将其分解为可能的最小输出水平。我其实想用:

 {{ form_widget(form) }}

So, I was thinking, how could I add a css class for the l somewhere in:

所以,我在想,我怎么能在某处添加一个css类:

class SystemNotificationType extends AbstractType {
    public function buildForm(FormBuilder $builder, array $options) {
        $builder    ->add('subject', 'text', array( 'label'  => 'Subject' ) )
–

I thought this might be more useful, as I only have to make changes on one place.

我认为这可能更有用,因为我只需要在一个地方进行更改。

So how could this be done, or maybe I'm thinking the wrong way.

那怎么可能这样做,或者我想错了路。

Any help would be great,

任何帮助都会很棒,

Many thanks, Philipp

非常感谢,Philipp

3 个解决方案

#1


26  

The class of a field is part of the presentation layer of your application, so it is best to create a twig theme for your forms:

字段的类是应用程序的表示层的一部分,因此最好为表单创建一个树枝主题:

Create a file fields.html.twig in Resources/views/Form of your Bundle and define how your form row will be formated, for example:

在Bundle的Resources / views / Form中创建一个文件fields.html.twig,并定义如何格式化表单行,例如:

{% block field_row %}
<div class="row">
    {{ form_errors(form) }}
    {{ form_label(form) }}
    {{ form_widget(form, { 'attr': {'class': 'c4'} }) }}
</div>
{% endblock field_row %}

If you want to customize only certain field, for example the field fieldName of the form formName, customize the row:

如果只想自定义某个字段,例如formName形式的字段fieldName,请自定义该行:

{% block _formName_fieldName_row %}
<div class="row">
    {{ form_label(form) }}
    {{ form_errors(form) }}
    {{ form_widget(form, { 'attr': {'class': 'c4'} }) }}
</div>
{% endblock %}

EDIT: customize only the field:

编辑:仅自定义字段:

{% block _formName_fieldName_widget %}
    {% set type = type|default('text') %}
    <input type="{{ type }}" {{ block('widget_attributes') }} value="{{ value }}" class="c4" />
{% endblock %}

Then in all the form templates you want it use this theme add:

然后在您想要的所有表单模板中使用此主题添加:

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

This is explained in depth in the cookbook

这在食谱中有详细解释

#2


42  

You can do it like this:

你可以这样做:

class SystemNotificationType extends AbstractType {
    public function buildForm(FormBuilder $builder, array $options) {
        $builder->add('subject', 'text', array( 
            'label'  => 'Subject',
            'attr'   =>  array(
                'class'   => 'c4')
            )
        );
    }
}

#3


6  

Form Theming

形式主题


Every part of how a form is rendered can be customized. You're free to change how each form "row" renders, change the markup used to render errors, or even customize how a textarea tag should be rendered. Nothing is off-limits, and different customizations can be used in different places.

可以自定义表单呈现方式的每个部分。您可以*更改每个表单“行”的呈现方式,更改用于呈现错误的标记,甚至可以自定义textarea标记的呈现方式。没有什么是禁止的,并且可以在不同的地方使用不同的自定义。

Symfony uses templates to render each and every part of a form, such as label tags, input tags, error messages and everything else.

Symfony使用模板来呈现表单的每个部分,例如标签标签,输入标签,错误消息和其他所有内容。

In Twig, each form "fragment" is represented by a Twig block. To customize any part of how a form renders, you just need to override the appropriate block.

在Twig中,每个表单“片段”由Twig块表示。要自定义表单呈现方式的任何部分,您只需要覆盖相应的块。

In PHP, each form "fragment" is rendered via an individual template file. To customize any part of how a form renders, you just need to override the existing template by creating a new one.

在PHP中,每个表单“片段”都通过单独的模板文件呈现。要自定义表单呈现方式的任何部分,您只需通过创建新模板来覆盖现有模板。

To understand how this works, customize the form_row fragment and add a class attribute to the div element that surrounds each row. To do this, create a new template file that will store the new markup:

要了解其工作原理,请自定义form_row片段并将class属性添加到围绕每行的div元素。为此,请创建一个用于存储新标记的新模板文件:

{# src/Acme/TaskBundle/Resources/views/Form/fields.html.twig #}
{% block form_row %}
{% spaceless %}
<div class="form_row">
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</div>
{% endspaceless %}
{% endblock form_row %}

The form_row form fragment is used when rendering most fields via the form_row function. To tell the form component to use your new form_row fragment defined above, add the following to the top of the template that renders the form:

通过form_row函数渲染大多数字段时使用form_row表单片段。要告诉表单组件使用上面定义的新form_row片段,请将以下内容添加到呈现表单的模板顶部:

{# src/Acme/TaskBundle/Resources/views/Default/new.html.twig #}
{% form_theme form 'AcmeTaskBundle:Form:fields.html.twig' %}
{% form_theme form 'AcmeTaskBundle:Form:fields.html.twig'
'AcmeTaskBundle:Form:fields2.html.twig' %}
{{ form(form) }}

The form_theme tag (in Twig) "imports" the fragments defined in the given template and uses them when rendering the form. In other words, when the form_row function is called later in this template, it will use the form_row block from your custom theme (instead of the default form_row block that ships with Symfony).

form_theme标记(在Twig中)“导入”给定模板中定义的片段,并在呈现表单时使用它们。换句话说,当稍后在此模板中调用form_row函数时,它将使用自定义主题中的form_row块(而不是Symfony附带的默认form_row块)。

Your custom theme does not have to override all the blocks. When rendering a block which is not overridden in your custom theme, the theming engine will fall back to the global theme (defined at the bundle level).

您的自定义主题不必覆盖所有块。渲染未在自定义主题中覆盖的块时,主题引擎将回退到全局主题(在捆绑级别定义)。

If several custom themes are provided they will be searched in the listed order before falling back to the global theme.

如果提供了多个自定义主题,则会在回退到全局主题之前按列出的顺序进行搜索。

To customize any portion of a form, you just need to override the appropriate fragment. Knowing exactly which block or file to override is the subject of the next section.

要自定义表单的任何部分,您只需要覆盖相应的片段。确切地知道要覆盖哪个块或文件是下一节的主题。

{# src/Acme/TaskBundle/Resources/views/Default/new.html.twig #}

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

{% form_theme form with ['AcmeTaskBundle:Form:fields.html.twig',
'AcmeTaskBundle:Form:fields2.html.twig'] %}

For more information see the How to customize rendering in Symfony cookbook

有关更多信息,请参阅如何在Symfony cookbook中自定义渲染

Global Form Theming

全球形式主题


In the above example, you used the form_theme helper (in Twig) to "import" the custom form fragments into just that form. You can also tell Symfony to import form customizations across your entire project.

在上面的示例中,您使用form_theme帮助程序(在Twig中)将自定义表单片段“导入”到该表单中。您还可以告诉Symfony在整个项目中导入表单自定义。

Twig

枝条

To automatically include the customized blocks from the fields.html.twig template created earlier in all templates, modify your application configuration file:

要自动包含先前在所有模板中创建的fields.html.twig模板中的自定义块,请修改应用程序配置文件:

# app/config/config.yml
  twig:
    form:
      resources:
        - 'AcmeTaskBundle:Form:fields.html.twig'

Any blocks inside the fields.html.twig template are now used globally to define form output.

现在,fields.html.twig模板中的任何块都可以全局用于定义表单输出。

#1


26  

The class of a field is part of the presentation layer of your application, so it is best to create a twig theme for your forms:

字段的类是应用程序的表示层的一部分,因此最好为表单创建一个树枝主题:

Create a file fields.html.twig in Resources/views/Form of your Bundle and define how your form row will be formated, for example:

在Bundle的Resources / views / Form中创建一个文件fields.html.twig,并定义如何格式化表单行,例如:

{% block field_row %}
<div class="row">
    {{ form_errors(form) }}
    {{ form_label(form) }}
    {{ form_widget(form, { 'attr': {'class': 'c4'} }) }}
</div>
{% endblock field_row %}

If you want to customize only certain field, for example the field fieldName of the form formName, customize the row:

如果只想自定义某个字段,例如formName形式的字段fieldName,请自定义该行:

{% block _formName_fieldName_row %}
<div class="row">
    {{ form_label(form) }}
    {{ form_errors(form) }}
    {{ form_widget(form, { 'attr': {'class': 'c4'} }) }}
</div>
{% endblock %}

EDIT: customize only the field:

编辑:仅自定义字段:

{% block _formName_fieldName_widget %}
    {% set type = type|default('text') %}
    <input type="{{ type }}" {{ block('widget_attributes') }} value="{{ value }}" class="c4" />
{% endblock %}

Then in all the form templates you want it use this theme add:

然后在您想要的所有表单模板中使用此主题添加:

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

This is explained in depth in the cookbook

这在食谱中有详细解释

#2


42  

You can do it like this:

你可以这样做:

class SystemNotificationType extends AbstractType {
    public function buildForm(FormBuilder $builder, array $options) {
        $builder->add('subject', 'text', array( 
            'label'  => 'Subject',
            'attr'   =>  array(
                'class'   => 'c4')
            )
        );
    }
}

#3


6  

Form Theming

形式主题


Every part of how a form is rendered can be customized. You're free to change how each form "row" renders, change the markup used to render errors, or even customize how a textarea tag should be rendered. Nothing is off-limits, and different customizations can be used in different places.

可以自定义表单呈现方式的每个部分。您可以*更改每个表单“行”的呈现方式,更改用于呈现错误的标记,甚至可以自定义textarea标记的呈现方式。没有什么是禁止的,并且可以在不同的地方使用不同的自定义。

Symfony uses templates to render each and every part of a form, such as label tags, input tags, error messages and everything else.

Symfony使用模板来呈现表单的每个部分,例如标签标签,输入标签,错误消息和其他所有内容。

In Twig, each form "fragment" is represented by a Twig block. To customize any part of how a form renders, you just need to override the appropriate block.

在Twig中,每个表单“片段”由Twig块表示。要自定义表单呈现方式的任何部分,您只需要覆盖相应的块。

In PHP, each form "fragment" is rendered via an individual template file. To customize any part of how a form renders, you just need to override the existing template by creating a new one.

在PHP中,每个表单“片段”都通过单独的模板文件呈现。要自定义表单呈现方式的任何部分,您只需通过创建新模板来覆盖现有模板。

To understand how this works, customize the form_row fragment and add a class attribute to the div element that surrounds each row. To do this, create a new template file that will store the new markup:

要了解其工作原理,请自定义form_row片段并将class属性添加到围绕每行的div元素。为此,请创建一个用于存储新标记的新模板文件:

{# src/Acme/TaskBundle/Resources/views/Form/fields.html.twig #}
{% block form_row %}
{% spaceless %}
<div class="form_row">
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</div>
{% endspaceless %}
{% endblock form_row %}

The form_row form fragment is used when rendering most fields via the form_row function. To tell the form component to use your new form_row fragment defined above, add the following to the top of the template that renders the form:

通过form_row函数渲染大多数字段时使用form_row表单片段。要告诉表单组件使用上面定义的新form_row片段,请将以下内容添加到呈现表单的模板顶部:

{# src/Acme/TaskBundle/Resources/views/Default/new.html.twig #}
{% form_theme form 'AcmeTaskBundle:Form:fields.html.twig' %}
{% form_theme form 'AcmeTaskBundle:Form:fields.html.twig'
'AcmeTaskBundle:Form:fields2.html.twig' %}
{{ form(form) }}

The form_theme tag (in Twig) "imports" the fragments defined in the given template and uses them when rendering the form. In other words, when the form_row function is called later in this template, it will use the form_row block from your custom theme (instead of the default form_row block that ships with Symfony).

form_theme标记(在Twig中)“导入”给定模板中定义的片段,并在呈现表单时使用它们。换句话说,当稍后在此模板中调用form_row函数时,它将使用自定义主题中的form_row块(而不是Symfony附带的默认form_row块)。

Your custom theme does not have to override all the blocks. When rendering a block which is not overridden in your custom theme, the theming engine will fall back to the global theme (defined at the bundle level).

您的自定义主题不必覆盖所有块。渲染未在自定义主题中覆盖的块时,主题引擎将回退到全局主题(在捆绑级别定义)。

If several custom themes are provided they will be searched in the listed order before falling back to the global theme.

如果提供了多个自定义主题,则会在回退到全局主题之前按列出的顺序进行搜索。

To customize any portion of a form, you just need to override the appropriate fragment. Knowing exactly which block or file to override is the subject of the next section.

要自定义表单的任何部分,您只需要覆盖相应的片段。确切地知道要覆盖哪个块或文件是下一节的主题。

{# src/Acme/TaskBundle/Resources/views/Default/new.html.twig #}

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

{% form_theme form with ['AcmeTaskBundle:Form:fields.html.twig',
'AcmeTaskBundle:Form:fields2.html.twig'] %}

For more information see the How to customize rendering in Symfony cookbook

有关更多信息,请参阅如何在Symfony cookbook中自定义渲染

Global Form Theming

全球形式主题


In the above example, you used the form_theme helper (in Twig) to "import" the custom form fragments into just that form. You can also tell Symfony to import form customizations across your entire project.

在上面的示例中,您使用form_theme帮助程序(在Twig中)将自定义表单片段“导入”到该表单中。您还可以告诉Symfony在整个项目中导入表单自定义。

Twig

枝条

To automatically include the customized blocks from the fields.html.twig template created earlier in all templates, modify your application configuration file:

要自动包含先前在所有模板中创建的fields.html.twig模板中的自定义块,请修改应用程序配置文件:

# app/config/config.yml
  twig:
    form:
      resources:
        - 'AcmeTaskBundle:Form:fields.html.twig'

Any blocks inside the fields.html.twig template are now used globally to define form output.

现在,fields.html.twig模板中的任何块都可以全局用于定义表单输出。