“重复"的自定义渲染来自 Twig 中 Symfony 2 的字段 [英] Custom rendering of a "repeated" field from Symfony 2 in Twig

查看:9
本文介绍了“重复"的自定义渲染来自 Twig 中 Symfony 2 的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用 Twig,我正在尝试构建一个注册表单.要添加密码/重新输入密码字段,我使用重复"文件类型:

I just started using Twig and I'm trying to build a registration form. To add a password/re-enter password field I use the "repeated" filetype:

->add('password', 'repeated', array(
    'type' => 'password',
    'invalid_message' => 'Passwords have to be equal.',
    'first_name'      => 'Password',
    'second_name'     => 'Re-enter password',
));

按预期工作.然而,我遇到的问题是我想在我的表单中添加一些自定义类等.所以我的模板看起来像这样:

which works as intended. The problem I have however is that I want to add some custom classes etc. to my form. So my template looks like this:

<form action="{{ path('register') }}" method="post" {{ form_enctype(form) }}>
    {{ form_errors(form) }}
    {{ form_errors(form.username) }}
    <div class="form-field">
        {{ form_label(form.username, null, { 'attr': {'class': 'form-label'} }) }}
        {{ form_widget(form.username, { 'attr': {'class': 'form-input'} }) }}
    </div>
    {{ form_errors(form.email) }}
    <div class="form-field">
        {{ form_label(form.email, null, { 'attr': {'class': 'form-label'} }) }}
        {{ form_widget(form.email, { 'attr': {'class': 'form-input'} }) }}
    </div>
    {{ form_errors(form.password) }}
    <div class="form-field">
        {{ form_label(form.password, null, { 'attr': {'class': 'form-label'} }) }}
        {{ form_widget(form.password, { 'attr': {'class': 'form-input'} }) }}
    </div>

    {{ form_rest(form) }}

    <input type="submit" class="contact-submit" />
</form>

这对除密码部分之外的所有内容都适用.我想在那里分别渲染两个字段,现在它们都在同一个 div 中渲染.

this works fine for everything except for the password part. I want to render both fields seperately there, now they are just both rendered in the same div.

我该如何解决这个问题?有没有办法在 Twig 中选择单独的字段?还是我只是做错了什么,因为我首先遇到了这个问题.

How do I fix this? Is there a way to select the seperate fields in Twig? Or am I just doing something wrong because I encounter this problem in the first place.

推荐答案

经过随机猜测,我解决了自己的问题.我会把它贴在这里,以便其他可能通过搜索找到这个问题的人也知道答案:

After a random guess I solved my own problem. I'll post it here so others who might come to this question by searching also know the answer:

{% for passwordField in form.password %}
    <div class="form-field">
        {{ form_label(passwordField, null, { 'attr': {'class': 'form-label'} }) }}
        {{ form_widget(passwordField, { 'attr': {'class': 'form-input'} }) }}
    </div>
{% endfor %}

这篇关于“重复"的自定义渲染来自 Twig 中 Symfony 2 的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆