Symfony表单标签呈现

时间:2014-03-12 15:54:46

标签: symfony twig symfony-forms

我想将我的所有标签转换为该输入的占位符:

例如:

这是标准的:

<div>
  <label>FooBar</label>
  <input type="text"/>
</div>
<div>
  <label>FooBar2</label>
  <input type="checkbox"/>
</div>

这就是我想要的:

<div>
  <input type="text" placeholder="FooBar"/>
</div>
<div>
  <label>FooBar2</label>
  <input type="checkbox"/>
</div>

我试图通过覆盖twig表单模板的部分来实现这一点,但是没有成功。这是一次尝试:

{% block form_widget_simple %}
    {% set type = type|default('text') %}

        {% if type == 'text' %}
                    {% block form_label %}{% endblock %}
        {% endif %}

    <input placeholder="{{ label|trans({}, translation_domain) }}" data-test="formtest" type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{% endblock form_widget_simple %}

谢谢!

7 个答案:

答案 0 :(得分:1)

离开这个坐了一会儿,学习更多树枝后,我终于回到了它并以正确的方式做到了。

国际海事组织,这是应该如何做的:

{% extends 'form_div_layout.html.twig' %}


{# override label creation, remove all labels from inputs and text areas #}

{% block form_row %}
{% spaceless %}
    <div>
    {% if form.vars.block_prefixes[1] != 'text' %}
        {{ form_label(form) }}
    {% endif %}
    {{ form_errors(form) }}
    {{ form_widget(form) }}
    </div>
{% endspaceless %}
{% endblock form_row %}


{# widgets #}
{% block form_widget_simple %}
    {% set type = type|default('text') %}
    <input placeholder="{{ label|trans({}, translation_domain) }}" type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{% endblock form_widget_simple %}

{% block textarea_widget %}
{% spaceless %}
    <textarea placeholder="{{ label|trans({}, translation_domain) }}" {{ block('widget_attributes') }}>{{ value }}</textarea>
{% endspaceless %}
{% endblock textarea_widget %}

像魅力一样!

答案 1 :(得分:0)

这样的事情会起作用吗?

{{ form_widget(form.yourFormField, {'attr': {'placeholder': twigVariableYouWantToUseAsPlaceHolder}}) }}

我通常使用form_widget函数来完全控制Symfony Forms创建的HTML元素。

这里有一个更完整的例子: http://symfony.com/doc/current/cookbook/form/form_customization.html

答案 2 :(得分:0)

您是否告诉Twig使用您的自定义模板来主题表单? 您可以通过添加以下行在包含表单的每个页面中执行此操作:

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

或者对config.yml文件中的所有表单执行一次操作:

twig:
    form:
        resources:
            - 'AcmeCoreBundle::fields.html.twig'

答案 3 :(得分:0)

也许在widget_attributes区块中:

{% block widget_attributes %}
  {% spaceless %}

    {% set type = type|default('text') %}

    {% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %} 

    {% if type == 'text' %}
        placeholder="{{ label|trans }}"
    {% endif %}

  {% endspaceless %}
{% endblock widget_attributes %}

答案 4 :(得分:0)

我创建了一个具有a dedicated option for that的Bootstrap包。您可以在the form theme内搜索labels_to_placeholders字词,了解其实施方式。

或者,如果你自己使用Bootstrap,只需使用我的捆绑包。 ;)

答案 5 :(得分:0)

更短的方式并保留原始的区块代码(仍然有更新)。

{% use 'form_div_layout.html.twig' with widget_attributes as base_widget_attributes %}
{% block widget_attributes %}
    {% set attr = {'placeholder': label|trans({}, translation_domain)} %}
    {{- block('base_widget_attributes') -}}
{% endblock widget_attributes %}

答案 6 :(得分:0)

如果您使用新的引导程序主题:

{% extends 'bootstrap_3_layout.html.twig' %}

{% block form_row -%}
    <div class="form-group{% if (not compound or force_error|default(false)) and not valid %} has-error{% endif %}">
        {% if form.vars.block_prefixes[1] != 'text' %}
            {{ form_label(form) }}
        {% endif %}
        {{ form_widget(form) }}
        {{ form_errors(form) }}
    </div>
{%- endblock form_row %}

{% block form_widget_simple -%}
    {% if attr.placeholder is not defined or attr.placeholder == '' %}
        {% set attr = attr|merge({placeholder: label|trans({}, translation_domain)|trim(':')|trim}) %}
    {% endif %}

    {% if type is not defined or 'file' != type %}
        {% set attr = attr|merge({class: (attr.class|default('') ~ ' form-control')|trim}) %}
    {% endif %}
    {{- parent() -}}
{%- endblock form_widget_simple %}

{% block textarea_widget -%}

    {% if attr.placeholder is not defined or attr.placeholder == '' %}
        {% set attr = attr|merge({placeholder: label|trans({}, translation_domain)|trim(':')|trim}) %}
    {% endif %}

    {% set attr = attr|merge({class: (attr.class|default('') ~ ' form-control')|trim}) %}
    {{- parent() -}}
{%- endblock textarea_widget %}

注意:我添加了|trim(':')以从FOSUserBundle生成的标签末尾删除冒号。

同样对于我的项目我想要大输入,所以我在这里添加了input-lg(简单和textarea):

{% set attr = attr|merge({class: (attr.class|default('') ~ ' input-lg form-control')|trim}) %}
相关问题