表单验证 - 我收到两次标签 [英] Validation of a form - I'm getting the labels two times
问题描述
我有一个注册表 - 来自 FOSUserBundle,它在模板中:
{% form_theme form 'AcmeMyBundle:Form:errors.html.twig' %}<form class="big-spacer" action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">{{ form_widget(form) }}<input class="btn little-spacer" type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}"/></div></表格>这里是errors.html.twig
:
{% block field_errors %}{% 无空间 %}{% 如果错误|长度 >0%}<ul class="little-spacer nav text-error">{% for error in errors %}<li>{{loop.index}}.{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</li>{% endfor %}</ul>{% 万一 %}{% endspaceless %}{% endblock field_errors %}
我刚刚从 TwitterBootstrap 添加了一些 css 类.
问题是我收到了两次验证的一些消息.
我的表单有 4 个字段 - 用户名、电子邮件、密码、确认密码
我试图打破尽可能多的验证规则,输出如下:
对于用户名
:
- 此用户名已被使用.请选择另一个.
- 用户名太短 - 请输入至少 2 个符号.
- 用户名太短 - 请输入至少 2 个符号.
对于电子邮件
:
- 请输入有效的电子邮件.
- 请输入有效的电子邮件.
如果我输入已使用的电子邮件,错误只会显示一次:
- 此电子邮件已被使用.
对于密码
:
如果它们很短:
- 密码太短 - 请输入至少 6 个符号.
- 密码太短 - 请输入至少 6 个符号.
如果它们不匹配:
- 输入的密码不匹配.
另外一个奇怪的是,当我重新提交表单时,仍然无效,密码长度的通知只有一个:
- 密码太短 - 请输入至少 6 个符号.
在重新提交之前,他们是两个.
您知道为什么某些错误会显示两次以及如何解决此问题吗?非常感谢您!:)
<小时>更新
这是 C:xampphtdocsProjectsrcAcmeMyBundleEntityUser.php
命名空间 AcmeMyBundleEntity;使用 FOSUserBundleEntityUser 作为 BaseUser;使用 DoctrineORMMapping 作为 ORM;/*** @ORM实体* @ORMTable(name="fos_user")*/类用户扩展 BaseUser{/*** @ORMID* @ORMColumn(type="整数")* @ORMGeneratedValue(strategy="AUTO")*/受保护的$id;公共函数 __construct(){父::__construct();//你自己的逻辑}}
在 C:xampphtdocsProjectappResourcesFOSUserBundle ranslations
我复制了文件 validators.en.yml
并在其中删除[-Inf, Inf]
部分并稍微更改了消息.
我还覆盖了验证文件 - 我在这里复制了它:
C:xampphtdocsProjectsrcAcmeMyBundleResourcesconfigvalidation.xml
我只更改了密码的最小长度.其他一切都与原始文件中的相同.
我的包扩展了 FOSUserBundle:
C:xampphtdocsProjectsrcAcmeMyBundleAcmeMyBundle.php
该文件包含以下内容:
解决方案 好的,这是一个已知问题.
让我们看看这里:https://github.com/symfony/symfony/issues/2605
解决方案是:为要覆盖的属性验证规则创建自己的验证组.在你的validation.xml中,只放你想要一些不同规则的属性,并在一个新的验证组上定义验证.
因此,对于validation.xml,只修改实体的plainPassword 验证规则(AcmeMyBundleEntityUser):
<?xml version="1.0" ?><约束映射 xmlns="http://symfony.com/schema/dic/constraint-mapping"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mappinghttp://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"><类名="AcmeMyBundleEntityUser"><属性名称="plainPassword"><约束名称="NotBlank"><option name="message">fos_user.password.blank</option><option name="groups">注册</option></约束><约束名称="长度"><option name="min">6</option><option name="minMessage">fos_user.password.short</option><选项名称="组"><value>RegistrationAcme</value><value>ProfileAcme</value></选项></约束></属性></类></约束映射>
现在您必须声明您对受影响的表单(注册表和个人资料)使用不同的验证组.幸运的是,FOSUserBundle 是一个很好的实践示例,它允许您在 config.yml 中覆盖它们:
fos_user:登记:形式:验证组:[默认,RegistrationAcme]轮廓:形式:验证组:[默认,ProfileAcme]
I have a registration form - from FOSUserBundle this is in the template:
{% form_theme form 'AcmeMyBundle:Form:errors.html.twig' %}
<form class="big-spacer" action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{ form_widget(form) }}
<div>
<input class="btn little-spacer" type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}" />
</div>
</form>
Here is errors.html.twig
:
{% block field_errors %}
{% spaceless %}
{% if errors|length > 0 %}
<ul class="little-spacer nav text-error">
{% for error in errors %}
<li>{{loop.index}}. {{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</li>
{% endfor %}
</ul>
{% endif %}
{% endspaceless %}
{% endblock field_errors %}
I have just added some css classes from TwitterBootstrap.
The problem is that I get some of the messages for the validation twice.
My form has 4 fields - Username, Email, Password, Confirm Password
I tried to break as many validation rules I can and here is the output:
For Username
:
- This username is already used. Please choose another one.
- The username is too short - please enter at least 2 symbols.
- The username is too short - please enter at least 2 symbols.
For email
:
- Please enter a valid email.
- Please enter a valid email.
and if I enter already used email, the error is shown only once:
- This email is already used.
And for the passwords
:
If they are short:
- The password is too short - please enter at least 6 symbols.
- The password is too short - please enter at least 6 symbols.
And if they don't match:
- The entered passwords don't match.
Another strange thing is that when I resubmit the form, but it's still not valid, the notice for the length of the password is only one:
- The password is too short - please enter at least 6 symbols.
and before resubmitting, they were two.
Do you have any ideas why some of the errors are displayed twice and how to fix this? Thank you very much in advance! :)
UPDATE
This is C:xampphtdocsProjectsrcAcmeMyBundleEntityUser.php
namespace AcmeMyBundleEntity;
use FOSUserBundleEntityUser as BaseUser;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
* @ORMTable(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
And in C:xampphtdocsProjectappResourcesFOSUserBundle ranslations
I copied the file validators.en.yml
and in it and removed the [-Inf, Inf]
part and changed the messages a bit.
I also overrode the validation file - I copied it here:
C:xampphtdocsProjectsrcAcmeMyBundleResourcesconfigvalidation.xml
I changed only the minimum length of the password. Everything else is the same as in the original file.
My bundle extends FOSUserBundle:
C:xampphtdocsProjectsrcAcmeMyBundleAcmeMyBundle.php
this file contains the following:
<?php
namespace AcmeBudgetTrackerBundle;
use SymfonyComponentHttpKernelBundleBundle;
class AcmeBudgetTrackerBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
解决方案 Ok, this is a known issue.
Let's have a look here : https://github.com/symfony/symfony/issues/2605
The solution is : create your own validation group for properties' validation rules you want to override. In your validation.xml, put only properties you want some different rules, and define validation on a new validation group.
So, for validation.xml, to modify only plainPassword validation rules for your entity (AcmeMyBundleEntityUser) :
<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="AcmeMyBundleEntityUser">
<property name="plainPassword">
<constraint name="NotBlank">
<option name="message">fos_user.password.blank</option>
<option name="groups">Registration</option>
</constraint>
<constraint name="Length">
<option name="min">6</option>
<option name="minMessage">fos_user.password.short</option>
<option name="groups">
<value>RegistrationAcme</value>
<value>ProfileAcme</value>
</option>
</constraint>
</property>
</class>
</constraint-mapping>
And now you have to state that you use different validation groups for the impacted forms (registration and profile). Fortunately, FOSUserBundle is a good practices' example and allows you to override them in your config.yml :
fos_user:
registration:
form:
validation_groups: [Default, RegistrationAcme]
profile:
form:
validation_groups: [Default, ProfileAcme]
这篇关于表单验证 - 我收到两次标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文