使用form_row设置重复的字段属性

时间:2014-04-03 22:09:43

标签: symfony twig

我已经为form_row创建了自己的主题并将其设置为在应用程序上全局使用,它看起来像这样:

fields.html.twig

{% block form_row %}
{% spaceless %}

  {% set class="form-row" %}
  {% if attr.class is defined %}
    {% set class = class ~ ' ' ~ attr.class %}
  {% endif %}
  {% if not form.vars.valid %}
    {% set class = class ~ ' error' %}
  {% endif %}

  <div class="{{ class }}" >
    {% if (label is defined) and (label is same as(false)) %}
      {{ form_widget(form) }}
    {% else %}
      {{ form_label(form) }}
      {{ form_widget(form) }}
    {% endif %}
    {{ form_errors(form) }}
  </div>
{% endspaceless %}
{% endblock form_row %}

然后我覆盖了FOSUserBundle注册表:

register_content.html

{% trans_default_domain 'FOSUserBundle' %}

<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
  {{ form_start(form) }}
    {{ form_errors(form) }}
    {{ form_row(form.email, { 'attr': {'placeholder': 'form.placeholder.email'|trans}, 'label': false }) }}
    {{ form_row(form.username, { 'attr': {'placeholder': 'form.placeholder.username'|trans}, 'label': false }) }}

    {{ form_row(form.plainPassword.first, { 'attr': {'placeholder': 'form.placeholder.password'|trans}, 'label': false }) }}
    {{ form_row(form.plainPassword.second, { 'attr': {'placeholder': 'form.placeholder.password_confirmation'|trans}, 'label': false }) }}
    <div>
      <ul class="no-bullet">
        <li><input type="submit" value="{{ 'registration.submit'|trans }}" class="button radius expand" /></li>
        <li><a href="#" class="facebook button radius expand">Sign Up with Facebook</a></li>
        <li><a href="#" class="twitter button radius expand">Sign Up with Twitter</a></li>
      </ul>
    </div>
  {{ form_end(form) }}
  <p class="text-center">Already signed up? <a href="{{ path('fos_user_security_login') }}">{{ 'layout.login'|trans({}, 'FOSUserBundle') }}</a></p>
</form>

我无法做的是将属性传递给form.plainPassword但只使用一次form_row()调用,这可能吗?

我正在寻找类似的东西:

{{ form_row(form.plainPassword, {
    'first': { 'attr': {'placeholder': 'form.placeholder.password'|trans}, 'label': false },
    'second': { 'attr': {'placeholder': 'form.placeholder.password_confirmation'|trans}, 'label': false }
})}}

1 个答案:

答案 0 :(得分:0)

据我所知,没有办法做到这一点,但对你的用例来说似乎没有必要。只需创建第二个unmapped字段,并将data attribute设置为与映射的密码字段相同。