在FOUserBundle中,我希望能够更改最小长度,最大长度的验证设置,而不是用户名和密码等字段的空白。
我能够通过@Assert
对我的自定义字段进行一些验证,但现在我想知道如何更改FOSUserBundle的用户名验证?
这些字段是自动生成的,因此我无法将它们添加到我的User
实体中...默认情况下,它允许使用{^|
等字符......这些字符看起来不太好
答案 0 :(得分:15)
您可以通过在捆绑包中创建新的验证文件来覆盖默认设置。这是bundle继承。只需复制(不要剪切)。
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配置中使用它们)。
答案 1 :(得分:4)
对我来说最简单的事情是覆盖我的自定义实体中的属性并更改断言的设置:
<?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)。
答案 2 :(得分:3)
在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"