Symfony 3:FosUserBundle使用自定义html类自定义注册表单

时间:2017-04-11 16:07:41

标签: php symfony twig fosuserbundle

当我尝试自定义我的symfony 3项目的FosUSerBundle登录表单时,因此我查看了FosUSerBundle的默认树枝模板以生成一个想法,然后我注意到提供了树枝模板通过供应商(vendor/friendsofsymfony/user-bundle/Resources/views/Registration/register_content.html.twig)具有以下值:

{% trans_default_domain 'FOSUserBundle' %}

{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register'}}) }}
    {{ form_widget(form) }}
    <div>
        <input type="submit" value="{{ 'registration.submit'|trans }}" />
    </div>
{{ form_end(form) }}

我注意到它调用{{ form_widget(form) }}来呈现表单。所以我想知道如何调用此方法以及如何自定义视图。基本上我想要表格和表格的html类看起来像注册管理员AdminLte的一个:https://almsaeedstudio.com/themes/AdminLTE/pages/examples/register.html

现在我所做的就是创建这个模板app/Resources/FOSUSerBundle/views/Registration/register.html.twig

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

{% set classes='hold-transition register-page'%}

{% block fos_user_content %}

  {% trans_default_domain 'FOSUserBundle' %}

  <div class="register-box">
    <div class="register-logo">
        <h1>PhotoShare!</h1>
    </div>
    <div class="register-box-body">
      <p class="login-box-msg">Register a new membership</p>
      {{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register')}) }}
        {{ form_widget(form) }}
        <div class="row">
          <div class="col-xs-4">
            <input type="submit" class="btn btn-primary btn-block btn-flat" value="{{ 'registration.submit'|trans }}" />
          </div>
        </div>
      {{ form_end(form) }}
  </div>

{% endblock fos_user_content %}

扩展app/Resources/FOSUSerBundle/views/layout.html.twig具有以下内容:

{% extends '::base.html.twig' %}

{% block title %}Photoshare!!{% endblock %}

{% set classes=''%}

{% block body %}
  {% block fos_user_content %}
  {% endblock fos_user_content %}
{% endblock body %}

扩展了app/Resourses/views/base.html.twig模板:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>{% block title %}Welcome!{% endblock %}</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        {% block stylesheets %}
          <link rel="stylesheet" type="text/css" href="{{asset('assets/vendor/bootstrap/css/bootstrap.css')}}" >
          <link rel="stylesheet" type="text/css" href="{{asset('assets/vendor/adminlte/adminlte.css')}}" >
          <link rel="stylesheet" type="text/css" href="{{asset('assets/vendor/adminlte/skin-blue.css')}}" >
        {% endblock %}
        <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />

        {% block javascriptsHeader %}

        {% endblock %}

    </head>
    <body class="{{ classes }}">
      {% block body %}
      {% endblock body %}

      {% block javascriptsFooter %}

      {% endblock javascriptsFooter %}
   </body>
</html>

我想呈现的字段是默认的,与通过默认的FosUserBundle管理员表单提供的字段完全相同。我想搞乱html类,以便从上面提到的模板中获得相同的外观。

1 个答案:

答案 0 :(得分:1)

您需要form theme

您可以创建表单主题,仅在注册表单等特定模板中使用它:

config.yml

或者您可以在# Twig Configuration twig: # ... form_themes: - 'form-theme.html.twig' 中设置全局表单主题:

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

然后,您的表单主题应该扩展symfony提供的默认div布局:

{# app/Resources/views/form-theme.html.twig #}
{% extends 'form_div_layout.html.twig' %}

{%- block form_start -%}
    {% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' custom classes')|trim }) %}
    {{ parent() }}
{%- endblock form_start -%}

{%- block form_row -%}
    <div class="custom classes">
        {{- form_label(form) -}}
        {{- form_widget(form) -}}
        {{- form_errors(form) -}}
    </div>
{%- endblock form_row -%}

{%- block form_widget_simple -%}
    {% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' custom classes')|trim }) %}
    {{ parent() }}
{%- endblock form_widget_simple -%}

然后您可以覆盖此模板中的块:

aes()