如何从 twig Symfony 5.2 覆盖 form_errors()?
How to override form_errors() from twig Symfony 5.2?
如何覆盖 Symfony 5.2 中的表单字段验证错误模板?
我在 twig 中有这样的表格
{{ form_start(registrationForm) }}
name:<br />
{{ form_row(registrationForm.name) }}
family_name:<br />
{{ form_row(registrationForm.family_name) }}
Email:<br />
{{ form_row(registrationForm.email) }}
password:<br />
{{ form_row(registrationForm.password) }}
<br />
<div class="text-center"><button type="submit" class="site-btn">registration</button></div>
{{ form_end(registrationForm) }}
如果电子邮件字段验证出错,这是布局
<div>
<ul>
<li>There is already an account with this email</li>
</ul>
<input type="text" id="registration_form_email" name="registration_form[email]" required="required" maxlength="180" class="register-form-input" autocomplete="off" value="asd">
</div>
问题是表单验证错误的布局
<ul>
<li>There is already an account with this email</li>
</ul>
取自标准模板 symfony,但我想在表单字段验证失败时使用我自己的模板。
我转向文档进行说明this and this但老实说,这让我更加困惑,我只是意识到可以在模板中以某种方式对这种形式进行风格化,但我不明白。
如何制作自己的模板来显示表单的字段验证错误?
结果想得到这样的布局:
<div>
<b>There is already an account with this email</b> <br>
<input type="text" id="registration_form_email" name="registration_form[email]" required="required" maxlength="180" class="register-form-input" autocomplete="off" value="asd">
</div>
使用 Symfony 表单时,您可以使用表单错误功能并使用 HTML 根据您的需要设置样式:
树枝:
{# render only the error messages related to this field #}
{{ form_errors(form.name) }}
{# render any "global" errors not associated to any form field #}
{{ form_errors(form) }}
你想知道的都可以找到here
foundation_5_layout.html.twig
extends form_div_layout.html.twig
, 我没看到 form_errors
也在 form_div_layout
里面,但是原理是一样的。
不要复制,通过示例创建一个模板“errors.html.twig”,用您想要的代码创建一个块form_errors
,然后在您的表单模板中,用[=调用它16=]
{% form_theme form 'path/to/errors.html.twig' %}
是不是更清楚了:)?它只是对树枝块的覆盖
如何覆盖 Symfony 5.2 中的表单字段验证错误模板?
我在 twig 中有这样的表格
{{ form_start(registrationForm) }}
name:<br />
{{ form_row(registrationForm.name) }}
family_name:<br />
{{ form_row(registrationForm.family_name) }}
Email:<br />
{{ form_row(registrationForm.email) }}
password:<br />
{{ form_row(registrationForm.password) }}
<br />
<div class="text-center"><button type="submit" class="site-btn">registration</button></div>
{{ form_end(registrationForm) }}
如果电子邮件字段验证出错,这是布局
<div>
<ul>
<li>There is already an account with this email</li>
</ul>
<input type="text" id="registration_form_email" name="registration_form[email]" required="required" maxlength="180" class="register-form-input" autocomplete="off" value="asd">
</div>
问题是表单验证错误的布局
<ul>
<li>There is already an account with this email</li>
</ul>
取自标准模板 symfony,但我想在表单字段验证失败时使用我自己的模板。
我转向文档进行说明this and this但老实说,这让我更加困惑,我只是意识到可以在模板中以某种方式对这种形式进行风格化,但我不明白。
如何制作自己的模板来显示表单的字段验证错误?
结果想得到这样的布局:
<div>
<b>There is already an account with this email</b> <br>
<input type="text" id="registration_form_email" name="registration_form[email]" required="required" maxlength="180" class="register-form-input" autocomplete="off" value="asd">
</div>
使用 Symfony 表单时,您可以使用表单错误功能并使用 HTML 根据您的需要设置样式:
树枝:
{# render only the error messages related to this field #}
{{ form_errors(form.name) }}
{# render any "global" errors not associated to any form field #}
{{ form_errors(form) }}
你想知道的都可以找到here
foundation_5_layout.html.twig
extends form_div_layout.html.twig
, 我没看到 form_errors
也在 form_div_layout
里面,但是原理是一样的。
不要复制,通过示例创建一个模板“errors.html.twig”,用您想要的代码创建一个块form_errors
,然后在您的表单模板中,用[=调用它16=]
{% form_theme form 'path/to/errors.html.twig' %}
是不是更清楚了:)?它只是对树枝块的覆盖