php Symfony2 Form Builder - 删除标签,使其占位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16993684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony2 Form Builder - Remove label, make it placeholder
提问by Josh Wa
I am playing with Symfony's form builder, and I can't find a way to not display a label. Further, I am interested in actually setting a placeholder for each input box. Is this possible? I have researched a bit and found nothing.
我正在使用 Symfony 的表单构建器,但找不到不显示标签的方法。此外,我有兴趣为每个输入框实际设置一个占位符。这可能吗?我研究了一下,什么也没发现。
My form:
我的表格:
<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>
EDIT: Solved!All of the solutions below helped, but I gave the answer to the primary helpful comment. I appreciate all of the help. For anyone else that comes across this, this is my final working code:
编辑:解决了!下面的所有解决方案都有帮助,但我给出了主要有用评论的答案。我感谢所有的帮助。对于遇到此问题的任何其他人,这是我的最终工作代码:
<form action="{{ path('searchPeople') }}" method="post" class="form-inline">
{{ form_errors(form) }}
{{ form_errors(form.first_name) }}
{{ form_widget(form.first_name, {'attr': {'placeholder': 'First Name'} }) }}
{{ form_errors(form.last_name) }}
{{ form_widget(form.last_name, {'attr': {'placeholder': 'Last Name'} }) }}
{{ form_rest(form) }}
<br />
<button type="submit" class="btn btn-primary" /><i class="icon-search icon-white"></i>Search</button>
</form>
采纳答案by Fo.
If you're outputting the field with form_rest you'll have to set the label for the the field to false in the form builder with something like
如果您使用 form_rest 输出该字段,则必须在表单构建器中将该字段的标签设置为 false,例如
$builder->add('first_name', 'text', array(
'label' => false,
));
If you output the fields individually, you can omit the form_label for that field in the twig template, or set it to an empty string.
如果单独输出字段,则可以在树枝模板中省略该字段的 form_label,或将其设置为空字符串。
{{ form_label(form.first_name, '') }}
回答by Titi
I know it's already answered, but might help somebody who is looking for a different solution for placeholders, if you don't want to change anything in your twig template:
我知道它已经得到了回答,但如果您不想更改树枝模板中的任何内容,可能会对正在寻找不同占位符解决方案的人有所帮助:
$builder->add(
'name',
'text',
array(
'attr' => array(
'placeholder' => 'Your name',
),
'label' => false,
)
);
回答by Léo Benoist
Convert label to placeholder
将标签转换为占位符
{% 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 %}
回答by user3557131
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:
对于遇到此标签问题的其他人:您可以使用表单主题来覆盖您想要的每个表单的 form_row 标记。但是,我建议将其设置为不可见以进行页面阅读器优化。我的引导程序示例:
{% 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.
不要忘记在 config.yml 和模板中包含您的 formtheme。
回答by Jessica
I did this recently! :) You'll want to create a new fields template, for form_row and one for form_widget. Then remove the form_label part, and add your placeholder.
我最近做了这个!:) 您需要为form_row 和form_widget 创建一个新的字段模板。然后删除 form_label 部分,并添加您的占位符。
http://symfony.com/doc/current/cookbook/form/form_customization.html
http://symfony.com/doc/current/cookbook/form/form_customization.html
You can do it per field, or set it for all of them.
您可以按字段执行此操作,也可以为所有字段进行设置。
Or you can also skip the removing the form_label from the form_row template, and just do form_widget() where you're currently calling form_row()
或者您也可以跳过从 form_row 模板中删除 form_label 的步骤,只需在您当前调用 form_row() 的地方执行 form_widget()
回答by Nat Naydenova
For those NOT using form_row
, you can always add the placeholder as an attribute directly when adding the input to the builder. Like so:
对于那些不使用form_row
,您始终可以在将输入添加到构建器时直接将占位符添加为属性。像这样:
$task = new Task();
$form = $this->createFormBuilder($task)
->add('first_name', 'text', array(
'required' => true,
'trim' => true,
'attr' => array('placeholder' => 'Lorem Ipsum')
)->getForm();
回答by Mick
Symfony 2.8 & above
Symfony 2.8 及以上
Remove form_label
{% block form_row %} <div> {{ form_errors(form) }} {{ form_widget(form) }} </div> {% endblock form_row %}
Add placeholder attribute
{% 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 %}
删除 form_label
{% block form_row %} <div> {{ form_errors(form) }} {{ form_widget(form) }} </div> {% endblock form_row %}
添加占位符属性
{% 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 %}
回答by Quolonel Questions
Expanding on Léo's answer:
扩展 Léo 的回答:
{% use 'form_div_layout.html.twig' %}
{% block widget_attributes %}
{% spaceless %}
{% set attr = attr|merge({'placeholder': label}) %}
{{ parent() }}
{% endspaceless %}
{% endblock widget_attributes %}
trans
filter has been removed because it is included in the parent.
trans
过滤器已被删除,因为它包含在 parent 中。
回答by Daniel
Bootstrap Forms
Bootstrap 表单
In my case best is mix aswers of @Cethy and @Quolonel Questions
就我而言,最好是混合 @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 %}
It looks the following
它看起来如下
It works with translations
它适用于翻译
回答by Cethy
To sums it up:
总结一下:
Titi's answer is the most simple ;
Mick, Léo& Quolonel's answers are the most effective but are incomplete (for symfony > 2.6) :
If you use the label_format
option in your *Type::configureOptions
, their solution does not work. You need to add the content of the form_label
block to handle all the label possibilities.
The full & most effective answer (code used w/ symfony 3.3) :
如果您在 中使用该label_format
选项*Type::configureOptions
,他们的解决方案将不起作用。您需要添加form_label
块的内容来处理所有标签的可能性。完整和最有效的答案(使用 symfony 3.3 的代码):
Remove form_label
{% block form_row %} <div> {{ form_errors(form) }} {{ form_widget(form) }} </div> {% endblock form_row %}
Edit the
widget_attribute
block{% 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 %}
删除 form_label
{% block form_row %} <div> {{ form_errors(form) }} {{ form_widget(form) }} </div> {% endblock form_row %}
编辑
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 %}
Notes:
注意事项:
Do not translate the labels into the
widget_attributes
block, otherwise they will appear as missing translations.The solution does not work for checkboxes or radio buttons, you'll want to add something like :
{%- block checkbox_widget -%} {{ parent() }} {{- form_label(form) -}} {%- endblock checkbox_widget -%}
不要将标签翻译到
widget_attributes
块中,否则它们将显示为缺少翻译。该解决方案不适用于复选框或单选按钮,您需要添加如下内容:
{%- block checkbox_widget -%} {{ parent() }} {{- form_label(form) -}} {%- endblock checkbox_widget -%}