简体   繁体   English

如何更改Symfony 2表单模板中显示的标签

[英]How to change the label appearing in symfony 2 form templates

I have this in template 我在模板中有这个

<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{ form_widget(form) }}

The form is appearing as 该表格显示为

fos_user_registration_form_username  --input box
fos_user_registration_form_email
fos_user_registration_form_plainPassword_first
fos_user_registration_form_plainPassword_second

But it want to have simple labels like Username , Email etc. How to do that 但是它希望具有简单的标签,例如Username,Email等。

You can parse the individual form fields instead of the form as a whole. 您可以解析单个表单字段,而不是整个表单。

{{ form_widget(form.fos_user_registration_form_username) }}

In this way, you can parse a single form element. 这样,您可以解析单个表单元素。 Make sure to end with 确保以

{{ form_rest(form) }} 

to output any not yet parsed fields (such as the csrf protection token). 输出任何尚未解析的字段(例如csrf保护令牌)。

using the above approach you can add your own labels to the fields. 使用上述方法,您可以将自己的标签添加到字段中。

FOSUserBundle uses the translator system. FOSUserBundle使用翻译器系统。 You need to as seen in the docs here: 您需要在此处的文档中看到:

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.2.0/Resources/doc/index.md https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.2.0/Resources/doc/index.md

Add this: 添加:

# app/config/config.yml
framework:
    translator: ~

To your config file ( app/config/config.yml ). 到您的配置文件( app/config/config.yml )。

This will tell symfony to replace all the label values with the values found in the translator file ( FOSUserBundle.en.yml ). 这将告诉symfony将所有标签值替换为在翻译器文件( FOSUserBundle.en.yml )中找到的值。 Then the form will always print "Username" instead of " fos_user_registration_form_username ". 然后,该表格将始终打印“用户名”,而不是“ fos_user_registration_form_username ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM