Validation FOSUserBundle-用户名、密码或电子邮件字段的验证
在FOUserBundle中,我希望能够更改最小长度、最大长度以及用户名和密码等字段的验证设置 我可以通过Validation FOSUserBundle-用户名、密码或电子邮件字段的验证,validation,symfony,fosuserbundle,Validation,Symfony,Fosuserbundle,在FOUserBundle中,我希望能够更改最小长度、最大长度以及用户名和密码等字段的验证设置 我可以通过@Assert在自定义字段上进行一些验证,但现在我想知道如何更改FOSUserBundle的用户名验证 这些字段是自动生成的,因此我无法将它们添加到我的用户实体中。。。默认情况下,它允许像{^ |等看起来不好的字符。您可以通过在捆绑包中创建新的验证文件来覆盖默认设置。这是捆绑包继承。只需复制(而不是剪切) 到YourBundle/Resources/config/validation/orm
@Assert
在自定义字段上进行一些验证,但现在我想知道如何更改FOSUserBundle的用户名验证
这些字段是自动生成的,因此我无法将它们添加到我的
用户
实体中。。。默认情况下,它允许像{^ |
等看起来不好的字符。您可以通过在捆绑包中创建新的验证文件来覆盖默认设置。这是捆绑包继承。只需复制(而不是剪切)
到YourBundle/Resources/config/validation/orm.xml
(couchdb.xml
,mongodb.xml
,spreep.xml
)
并根据需要进行调整。更改类名,然后添加约束:
<class name="Vendor\YourBundle\Model\User">
<property name="username">
<!-- minimum length for username -->
<constraint name="MinLength">
<option name="limit">3</option>
<option name="message">Your name must have at least {{ limit }} characters.</option>
</constraint>
<!-- custom constraint -->
<constraint name="Acme\DemoBundle\Validator\Constraints\ContainsAlphanumeric" />
</property>
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">usernameCanonical</option>
<option name="errorPath">username</option>
<option name="message">fos_user.username.already_used</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint>
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">emailCanonical</option>
<option name="errorPath">email</option>
<option name="message">fos_user.email.already_used</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint>
</class>
3.
您的姓名必须至少包含{{limit}}个字符。
用户名规范
用户名
fos\u user.username.已使用
登记处
轮廓
电子邮件规范
电子邮件
fos_user.email.已使用
登记处
轮廓
阅读有关哪些约束可用(以及如何将它们与xml配置一起使用)的更多信息。在YML中,您可以这样做:
# src/Acme/ProjectBundle/Resources/config/validation.yml
Acme\ProjectBundle\Entity\User:
properties:
email:
- Length:
min: 5
minMessage: "Your email must have at least {{ limit }} characters."
max: 255
maxMessage: "Your email is too long."
- NotBlank:
message: "Please enter an email"
username:
- Length:
min: 6
minMessage: "Your username must have at least {{ limit }} characters."
max: 255
maxMessage: "Your username is too long."
- NotBlank:
message: "Please enter an username"
plainPassword:
- Length:
min: 8
minMessage: "Your password must have at least {{ limit }} characters."
max: 255
maxMessage: "Your password is too long."
- NotBlank:
message: "Please enter a password"
Acme\ProjectBundle\Form\Model\ChangePassword:
properties:
new:
- Length:
min: 8
minMessage: "Your password must have at least {{ limit }} characters."
max: 255
maxMessage: "Your password is too long."
- NotBlank:
message: "Please enter a password"
Acme\ProjectBundle\Form\Model\ResetPassword:
properties:
new:
- Length:
min: 8
minMessage: "Your password must have at least {{ limit }} characters."
max: 255
maxMessage: "Your password is too long."
- NotBlank:
message: "Please enter a password"
对我来说,最简单的方法是覆盖自定义实体中的属性并更改断言的设置:
<?php
namespace YourBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class User extends \FOS\UserBundle\Model\User
{
// ...
/**
* @Assert\Length(
* min=8,
* max=4096,
* minMessage="user.password.short",
* groups={"Profile", "ResetPassword", "Registration", "ChangePassword"}
* )
*/
protected $plainPassword;
// ...
}
不知道这是否是特定于版本的;我使用的是symfony v2.8.3和fosuser~2。0@dev(a39d000).好的,非常感谢!我甚至不必添加orm.xml文件,在正确阅读了Validation Constraints参考之后,我发现我可以将Validation.yml添加到我的UserBundle的配置中,并将其添加到那里:DThats是第二种可能的解决方案,但一般来说,使用FOSUserBundle中的xml文件并将其复制粘贴为它已经提供了一个skeleten:)@M.Winterinho您能不能也添加您的解决方案?这似乎不起作用,因为字段验证是在不同的文件中定义的:如果我试图覆盖此文件,验证会重复,而不是重复overwritten@nifr是的,如前所述,我正在谈论一个话题,因为这是属性验证的文件定义了ons。您提到的文件用于实体验证,而不是属性验证,因此从技术上讲,在您建议的解决方案中,您混合了这两个不正确的选项。注意,您忘记了在电子邮件中重新添加“唯一”约束和“电子邮件”约束
# validators.en.yml
user:
password:
short: Password must be at least 8 characters long.