FOSUserBundle:重写Twig模板:添加HTML元素[英] FOSUserBundle : Overriding a Twig template : adding HTML elements

本文是小编为大家收集整理的关于FOSUserBundle:重写Twig模板:添加HTML元素的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用 fosuserbundle ,我想在注册表格中添加一些HTML元素.实际上,我做到了,我可以在我的表单中看到对用户类的添加属性.问题是我希望这些字段(名字,姓氏,出生日期等)能够获得我的CSS模板(Bootstrap)的外观. 我成功地通过覆盖该页面来完成此操作,因为明确声明了HTML.我想在寄存器页面上做同样的事情,但是这似乎对我感到困惑,因为这是:

的内容
  • register.html.twig

    {%扩展" fosuserbundle :: layout.html.twig"%}

    {%block fos_user_content%} {%包含" fosuserbundle:registration:register_content.html.twig"%} {%endblock fos_user_content%}

  • register_content.html.twig

    {%trans_default_domain'fosuserbundle'%}

    {{form_widget(form)}}

如何访问此代码中我在页面中看到的元素?

推荐答案

尝试此

<div class="form-group {% if form.plainPassword.first.vars.errors %}has-error{% endif %}">
  <label class="col-lg-2 control-label">Password:</label>
  <div class="col-lg-5">
    {{ form_widget(form.plainPassword.first, {'attr': {'class': 'form-control input-lg', 'placeholder': 'Enter password', 'required': 'required'}}) }}
    {% for errorItem in form.plainPassword.first.vars.errors %}
      <label class="control-label has-error" for="{{ form.plainPassword.vars.id }}">{{ errorItem.message }}</label>
    {% endfor %}
  </div>
  <div class="col-lg-5">
    {{ form_widget(form.plainPassword.second, {'attr': {'class': 'form-control input-lg', 'placeholder': 'Enter password again', 'required': 'required'}}) }}
  </div>
</div>

它对我有用.

其他推荐答案

请在此处查看官方文档:" 覆盖表单".您将需要创建自定义注册表格类型类,将其声明为服务,并告诉FosuserBundle使用它.

要自定义模板,请参见"

其他推荐答案

替换form_widget(form)用以下内容:

form_widget(form.username)
form_widget(form.email)
form_widget(form.plainPassword)
form_widget(form.myField)
form_rest(form)

在您的自定义RegistrationFormType类中,您可以在用户名字段中添加一个类:

    $builder
        ->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle', 'attr' => array('class'=>'myClass')))

还请参见手工渲染字段上的表单文档: http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template

本文地址:https://www.itbaoku.cn/post/2090771.html

问题描述

I'm using FOSUserBundle, and I want to add a few HTML elements to the registration form. Actually, I did and I can see the added properties to the User class in my form. The issue is that I want those fields (first name, last name, date of birth, etc..) to get the look and feel of my CSS template (bootstrap). I succeeded to do that for the login page by overriding it, since the HTML are explicitly declared. I want to do the same for the register page, however it seems confused to me, because here's the content of :

  • register.html.twig

    {% extends "FOSUserBundle::layout.html.twig" %}

    {% block fos_user_content %} {% include "FOSUserBundle:Registration:register_content.html.twig" %} {% endblock fos_user_content %}

  • register_content.html.twig

    {% trans_default_domain 'FOSUserBundle' %}

    {{ form_widget(form) }}

How can I access to the elements that I see in the page from this code ?

推荐答案

Try this

<div class="form-group {% if form.plainPassword.first.vars.errors %}has-error{% endif %}">
  <label class="col-lg-2 control-label">Password:</label>
  <div class="col-lg-5">
    {{ form_widget(form.plainPassword.first, {'attr': {'class': 'form-control input-lg', 'placeholder': 'Enter password', 'required': 'required'}}) }}
    {% for errorItem in form.plainPassword.first.vars.errors %}
      <label class="control-label has-error" for="{{ form.plainPassword.vars.id }}">{{ errorItem.message }}</label>
    {% endfor %}
  </div>
  <div class="col-lg-5">
    {{ form_widget(form.plainPassword.second, {'attr': {'class': 'form-control input-lg', 'placeholder': 'Enter password again', 'required': 'required'}}) }}
  </div>
</div>

It works for me.

其他推荐答案

Please see the official documentation here: "Overriding Forms". You will need to create a custom registration form type class, declare it as a service, and tell FOSUserBundle to use it.

To customize the template, see "Overriding Templates". In your case, you could create app/Resources/FOSUserBundle/views/Registration/register.html.twig.

其他推荐答案

replace form_widget(form) with something like:

form_widget(form.username)
form_widget(form.email)
form_widget(form.plainPassword)
form_widget(form.myField)
form_rest(form)

in your custom RegistrationFormType class, you could add a class to the username field with:

    $builder
        ->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle', 'attr' => array('class'=>'myClass')))

also see form docs on rendering fields by hand: http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template