如何更改 Symfony 2 / Twig 中的 form_row 行为

发布于 2024-12-20 00:09:04 字数 614 浏览 1 评论 0原文

默认情况下,form_row(form.name) 呈现为类似以下内容:

<div><label for="form_name" class=" required">Name</label><input type="text" id="form_name" name="form[name]" required="required" maxlength="45" value=""></div>

如何/在哪里可以将 form_row() 的行为更改为例如:

<div class="someClassName"><label for="form_name" class=" required">Name</label></div><div class="someOtherClassName"><input type="text" id="form_name" name="form[name]" required="required" maxlength="45" value=""></div>

By default a form_row(form.name) is rendered as something like:

<div><label for="form_name" class=" required">Name</label><input type="text" id="form_name" name="form[name]" required="required" maxlength="45" value=""></div>

How/where can I change the behaviour of form_row() to for example:

<div class="someClassName"><label for="form_name" class=" required">Name</label></div><div class="someOtherClassName"><input type="text" id="form_name" name="form[name]" required="required" maxlength="45" value=""></div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杯敬自由 2024-12-27 00:09:04

您可以检查此网址: http://symfony .com/doc/2.0/cookbook/form/form_customization.html#cookbook-form-theming-methods

有一段关于自定义 form_row() 的内容。

这是一个简单的例子。默认情况下, form_row() 将创建一个简单的 html 结构,如下所示:

TWIG:

{{ form_row(form.email, { 'label' : 'Your email address' }) }}

HTML:

<div>
    <label for="register_email" class=" required">Your email address</label>
    <input type="email" id="register_email" name="register[email]" required="required" />
</div>

因此,根据文档,您可以创建一个新的 twig 模板,并将 class="form_row" 添加到字段和标签周围。将其放在 YourBundle/views/Form/fields.html.twig 中,然后将以下代码放入其中:

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

在模板文件中,添加以下行:

{% form_theme form 'YourBundle:Form:fields.html.twig' %}

现在,将使用您创建的该文件中的 form_row 模板,并将返回以下 HTML 代码:

<div class="form_row">
    <label for="register_email" class=" required">Email</label>        
    <input type="email" id="register_email" name="register[email]" required="required" />
</div>

希望有帮助。

You can check this URL: http://symfony.com/doc/2.0/cookbook/form/form_customization.html#cookbook-form-theming-methods

There's a paragraph about customizing form_row().

Here's a simple example. By default, form_row() would create a simple html structure like this:

TWIG:

{{ form_row(form.email, { 'label' : 'Your email address' }) }}

HTML:

<div>
    <label for="register_email" class=" required">Your email address</label>
    <input type="email" id="register_email" name="register[email]" required="required" />
</div>

So, according to the docs, you can create a new twig template, and add class="form_row" to surrounding the field and label. Place it in YourBundle/views/Form/fields.html.twig and put the following code in there:

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

In your template file, add the following line:

{% form_theme form 'YourBundle:Form:fields.html.twig' %}

Now, the form_row template from that file you created will be used, and will return the following HTML code:

<div class="form_row">
    <label for="register_email" class=" required">Email</label>        
    <input type="email" id="register_email" name="register[email]" required="required" />
</div>

Hope that it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文