在 FOUserBundle 中,我希望能够在用户名和密码等字段上更改最小长度、最大长度和非空白的验证设置。
我能够通过@Assert
我的自定义字段进行一些验证,但现在我想知道如何更改 FOSUserBundle 的用户名验证?
这些字段是自动生成的,所以我无法将它们添加到我的User
实体中......默认情况下,它允许诸如此类的字符{^|
......看起来不太好。
在 FOUserBundle 中,我希望能够在用户名和密码等字段上更改最小长度、最大长度和非空白的验证设置。
我能够通过@Assert
我的自定义字段进行一些验证,但现在我想知道如何更改 FOSUserBundle 的用户名验证?
这些字段是自动生成的,所以我无法将它们添加到我的User
实体中......默认情况下,它允许诸如此类的字符{^|
......看起来不太好。
您可以通过在捆绑包中创建新的验证文件来覆盖默认设置。这是捆绑继承。只是复制(而不是剪切)。
FOSUserBundle/Resources/config/validation/orm.xml
到YourBundle/Resources/config/validation/orm.xml
.
( couchdb.xml
, mongodb.xml
,propel.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>
在Validation Constraints Reference中阅读有关哪些约束可用(以及如何将它们与 xml 配置一起使用)的更多信息。
对我来说最简单的事情是覆盖我的自定义实体中的属性并更改断言的设置:
<?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;
// ...
}
不要忘记验证组。
注意:user.password.short
翻译位于validators
您的捆绑包的域中YourBundle\Resources\translations\
。
例子:
# validators.en.yml
user:
password:
short: Password must be at least 8 characters long.
不知道这是否是特定于版本的;我正在使用 symfony v2.8.3 和 fosuser ~2.0@dev (a39d000)。
在 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"