41

我正在使用 Symfony 的表单构建器,但我找不到不显示标签的方法。此外,我有兴趣为每个输入框实际设置一个占位符。这可能吗?我研究了一下,什么也没发现。

我的表格:

<form action="{{ path('searchPeople') }}" method="post" class="form-inline">
    {{ form_errors(form) }}
    
    {{ form_row(form.first_name) }}
    {{ form_row(form.last_name) }}
    
    {{ form_rest(form) }}
    
    <br />
    <button type="submit" class="btn btn-primary" /><i class="icon-search"></i>Search</button>
</form>
4

12 回答 12

61

我知道它已经得到解答,但如果您不想更改树枝模板中的任何内容,可能会对正在为占位符寻找不同解决方案的人有所帮助:

$builder->add(
    'name',
    'text', 
     array(
        'attr' => array(
             'placeholder' => 'Your name',
        ),
        'label' => false,
     )
);
于 2014-10-07T16:29:15.323 回答
20

如果您使用 form_rest 输出该字段,则必须在表单构建器中将该字段的标签设置为 false ,例如

$builder->add('first_name', 'text', array(
    'label' => false,
));

如果单独输出字段,则可以在 twig 模板中省略该字段的 form_label,或将其设置为空字符串。

{{ form_label(form.first_name, '') }}
于 2013-06-07T22:11:21.033 回答
6

将标签转换为占位符

{% 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 %}
于 2014-08-20T15:42:57.037 回答
3

for other that come across this label-question: you could use form theme to override the form_row tag for every form you want. However I recommend to just set it invisible for page reader optimization. my example with bootstrap:

{% block form_row %}
    {% spaceless %}
            {{ form_label(form, null, {'label_attr': {'class': 'sr-only'}}) }}
            {{ form_errors(form) }}
            {{ form_widget(form) }}
    {% endspaceless %}
{% endblock form_row %}

don't forget to include your formtheme in config.yml and template.

于 2014-07-06T14:24:28.887 回答
3

我最近做了这个!:) 您需要为 form_row 和 form_widget 创建一个新的字段模板。然后删除 form_label 部分,并添加您的占位符。

http://symfony.com/doc/current/cookbook/form/form_customization.html

您可以按字段进行,也可以为所有字段设置。

或者您也可以跳过从 form_row 模板中删除 form_label 的步骤,只需在您当前调用 form_row() 的地方执行 form_widget()

于 2013-06-07T22:11:14.887 回答
3

对于那些不使用的人form_row,您可以在将输入添加到构建器时直接将占位符添加为属性。像这样:

$task = new Task();
$form = $this->createFormBuilder($task)
            ->add('first_name', 'text', array(
                      'required' => true,
                      'trim' => true,
                      'attr' => array('placeholder' => 'Lorem Ipsum')
        )->getForm();
于 2016-03-23T17:51:51.507 回答
3

Symfony 2.8 及更高版本

  1. 删除 form_label

    {% block form_row %}
    <div>
        {{ form_errors(form) }}
        {{ form_widget(form) }}
    </div>
    {% endblock form_row %}
    
  2. 添加占位符属性

    {% block form_widget_simple %}
        {% set type = type|default('text') %}
        <input placeholder="{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}" type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
    {% endblock form_widget_simple %}
    
于 2016-05-18T02:37:41.407 回答
2

扩展 Léo 的答案:

{% use 'form_div_layout.html.twig' %}
{% block widget_attributes %}
{% spaceless %}
    {% set attr = attr|merge({'placeholder': label}) %}
    {{ parent() }}
{% endspaceless %}
{% endblock widget_attributes %}

trans过滤器已被删除,因为它包含在 parent 中

于 2014-09-15T10:08:14.640 回答
1

您必须手动呈现表单。

这是一个例子:

<form id="form-message" action="{{ path('home') }}" method="post" {{ form_enctype(form) }}>
  {{ form_label(form.name) }}
  {% if form_errors(form.name) %}
    <div class="alert alert-error">
      {{ form_errors(form.name) }}
    </div>
  {% endif %}
  {{ form_widget(form.name) }}
  {{ form_row(form._token) }}
  <input type="submit" class="btn" value="Submit">
</form>

相关文档

于 2013-06-07T22:13:22.990 回答
1

总结一下:

  • 蒂蒂的回答是最简单的;

  • MickLéo & Quolonel的答案是最有效的,但不完整(对于 symfony > 2.6):

如果您使用 中的label_format选项*Type::configureOptions,他们的解决方案将不起作用。您需要添加form_label块的内容来处理所有可能的标签。完整且最有效的答案(使用 symfony 3.3 的代码):

  1. 删除 form_label

    {% block form_row %}
    <div>
        {{ form_errors(form) }}
        {{ form_widget(form) }}
    </div>
    {% endblock form_row %}
    
  2. 编辑widget_attribute

    {% block widget_attributes %}
        {% spaceless %}
            {% if label is not same as(false) -%}
                {% if label is empty -%}
                    {%- if label_format is not empty -%}
                        {% set label = label_format|replace({
                            '%name%': name,
                            '%id%': id,
                        }) %}
                    {%- else -%}
                        {% set label = name|humanize %}
                    {%- endif -%}
                {%- endif -%}
    
                {% set attr = attr|merge({'placeholder': label}) %}
            {%- endif -%}
    
            {{ parent() }}
        {% endspaceless %}
    {% endblock widget_attributes %}
    

备注

  • 不要将标签翻译到widget_attributes块中,否则它们将显示为缺少翻译。

  • 该解决方案不适用于复选框或单选按钮,您需要添加以下内容:

    {%- block checkbox_widget -%}
        {{ parent() }}
        {{- form_label(form) -}}
    {%- endblock checkbox_widget -%}
    
于 2017-06-29T19:23:50.383 回答
1

引导表单

在我的情况下,最好是混合@Cethy 和@Quolonel 的问题

{% form_theme form _self %}
{% use 'bootstrap_4_layout.html.twig' %}

{% block widget_attributes %}          {#   set placeholder   #}
    {% spaceless %}
        {% set attr = attr|merge({'placeholder': label}) %}
        {{ parent() }}
    {% endspaceless %}
{% endblock widget_attributes %}

{% block form_row %}                   {#   remove label   #}
    <div class="form-group">
        {{ form_errors(form) }}
        {{ form_widget(form) }}
    </div>
{% endblock form_row %}

它看起来如下

在此处输入图像描述

它适用于翻译

于 2019-01-03T09:14:39.313 回答
0

您还可以在呈现表单之前将标签复制到占位符属性中:

$formView = $form->createView();

foreach($formView->getIterator() as $item) {
    /** @var $item FormView */
    if ($item->vars['label']) {
        $item->vars['attr']['placeholder'] =$item->vars['label'];
    }
}
于 2018-07-12T16:44:03.087 回答