Php Symfony2在twig中更改表单日期输入类

Php Symfony2在twig中更改表单日期输入类,php,forms,symfony,Php,Forms,Symfony,我对如何覆盖表单模板的代码以满足我的需要感到困惑。。例如,当我创建一个日期输入字段时,它非常长,我想用我的类来更改它。我正在阅读此文档,但我不知道如何根据自己的需要使用此示例:这是我已经做过的: 我在config.yml中添加了水平主题: # Twig Configuration twig: debug: "%kernel.debug%" strict_variables: "%kernel.debug%" form: resourc

我对如何覆盖表单模板的代码以满足我的需要感到困惑。。例如,当我创建一个日期输入字段时,它非常长,我想用我的类来更改它。我正在阅读此文档,但我不知道如何根据自己的需要使用此示例:这是我已经做过的:

我在config.yml中添加了水平主题:

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    form:
        resources: 
            - 'bootstrap_3_horizontal_layout.html.twig' 
这就是我如何输入数据字段的方法:

>add('DateOfBirth', 'date', array(
    'label' => 'Date Of Birth * ',
    'widget' => 'choice',
    'attr' => array('placeholder' => 'Date of Birth')))
它呈现此模板:

<div id="form_DateOfBirth" placeholder="Date of Birth" class="form-inline">
    <select id="form_DateOfBirth_month" name="form[DateOfBirth][month]" class="form-control"></select>
我该怎么做?从文档中,我知道我需要做的唯一一件事是:

{% form_theme form _self %}

{% block date_widget %}

{% endblock date_widget %}


 {{ form(form) }}
然后呢/

我正在尝试这样做:

{% block date_widget -%}
            {% set attr = attr|merge({class: (attr.class|default('') ~ ' span1')|trim}) -%}

    {%- endblock date_widget %}
但是我得到了未定义的变量attr/

更新

代码:
{form_小部件(form.DateOfBirth,{'attr':{'class':'span1'}}}}
不是我需要的

这将更改整个div的类。我需要更改字段
select
的类,该字段具有类
表单控件

这就是我得到的(
class=“span1表单内联”
):


像这样覆盖整个小部件意味着更改将影响所有其他表单。如果您需要其他模板长输入,该怎么办?您可以简单地将额外的类单独添加到输入中。例如这样的事情:

{% block date_widget -%}
            {% set attr = attr|merge({class: (attr.class|default('') ~ ' span1')|trim}) -%}

    {%- endblock date_widget %}
那会在你的FormType课上

例如,这将出现在您的细枝模板中

您还可以对表单的特定字段应用自定义设置。关于这个问题,你可以读更多

-更新-

看看这个

试试:

{{ form_widget(form.DateOfBirth, {'attr': {'class': 'span1'}}) }}
表单是从控制器传递到视图的表单视图

见文件:

编辑:

这将更改整个div的类。我需要更改select字段的类,它有一个class表单控件

就我而言,这就是它的工作方式,我使用相同的语法(我同时使用Symfony 2和bootstrap),它只更改输入/选择元素的类,而不更改周围的dir(我也只使用form_widget()方法)


你能把你使用这个代码时能看到的打印出来吗?

你好:)当然可以,我可以,你也可以检查我的吗?@Dominykas55添加了额外的信息链接。看起来它可以工作,然而,我在widget和date_模式上得到了未定义的变量……请记住:一个好的答案总是会对所做的事情以及为什么会以这种方式进行解释,不仅是为了OP,也是为了未来的访问者。
You can give class by separate to the date both year month and day for example if the date is by name DateOfBirth would look like this


{{form_widget(form.DateOfBirth.day , {'attr': {'class': 'form-control'}}) }}

{{ form_widget(form.DateOfBirth.month , {'attr': {'class': 'form-control'}}) }}

{{ form_widget(form.DateOfBirth.year , {'attr': {'class': 'form-control'}}) }}
{{ form_widget(form.birthdate, { 'attr': {'class': 'your_class'} }) }}
{{ form_widget(form.DateOfBirth, {'attr': {'class': 'span1'}}) }}
You can give class by separate to the date both year month and day for example if the date is by name DateOfBirth would look like this


{{form_widget(form.DateOfBirth.day , {'attr': {'class': 'form-control'}}) }}

{{ form_widget(form.DateOfBirth.month , {'attr': {'class': 'form-control'}}) }}

{{ form_widget(form.DateOfBirth.year , {'attr': {'class': 'form-control'}}) }}