Symfony2 - FormBuilder - 添加一个类到字段和输入 [英] Symfony2 - FormBuilder - add a class to the field and input

查看:361
本文介绍了Symfony2 - FormBuilder - 添加一个类到字段和输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我可以在Twig中的表单中执行类似操作:

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

但我必须为每种形式设置模板。我必须把它分解到最小的可能输出电平。我实际上想使用:

  {{form_widget(form)}} 

所以,我在想,如何为l添加一个css类:

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



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



那么如何做到这一点,或者我想错了方法。 / p>

任何帮助都很棒,



非常感谢,
Philipp

解决方案

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



中创建一个 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%}

如果您只想自定义某个字段自定义



形式的 formName 字段 fieldName

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

EDIT 字段:

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

然后在所有表单模板中,添加:

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

这在食谱


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

I can do something like this in my form within 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:

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

解决方案

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:

{% 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:

{% 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

这篇关于Symfony2 - FormBuilder - 添加一个类到字段和输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆