In the FOUserBundle, I would like to be able to change the validation settings for minimum length, maximum length and not blank on fields such as username and password.
在FOUserBundle中,我希望能够更改验证设置为最小长度、最大长度,而不是用户名和密码等字段的空白。
I was able to put some validation via @Assert
on my custom fields, but now I am wondering how I could change the username validation for the FOSUserBundle?
我可以在我的自定义字段上通过@Assert进行一些验证,但是现在我想知道如何更改FOSUserBundle的用户名验证?
These fields are generated automatically, so I can't add them to my User
entity... and by default, it allows characters like {^|
etc... which don't look good.
这些字段是自动生成的,所以我不能将它们添加到我的用户实体中…默认情况下,它允许字符如{ ^ |等等……它不好看。
3 个解决方案
#1
14
You can overwrite the default settings by creating a new validation file in your bundle. This is bundle inheritance. Just copy (not cut).
您可以通过在包中创建一个新的验证文件来覆盖默认设置。这是包继承。复制(不)。
FOSUserBundle/Resources/config/validation/orm.xml
to YourBundle/Resources/config/validation/orm.xml
.
FOSUserBundle /资源/ config /验证/ orm。xml YourBundle /资源/配置/验证/ xml文件。
(couchdb.xml
, mongodb.xml
, propel.xml
respectively)
(couchdb。xml,mongodb。xml,推动。xml分别)
and adjust it to your needs. Change the class name, then add your constraints:
并根据你的需要进行调整。更改类名,然后添加约束:
<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>
Read more about which constraints are available (and how to use them with xml configuration ) in the Validation Constraints Reference.
阅读验证约束引用中哪些约束可用(以及如何在xml配置中使用它们)的更多信息。
#2
4
The easiest thing for me was to overwrite the property in my custom entity and change the settings for the assertion:
对于我来说,最简单的事情是在我的自定义实体中重写属性,并更改断言的设置:
<?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;
// ...
}
Don't forget the validation groups.
不要忘记验证组。
Heads up: user.password.short
translation is in the validators
Domain of your bundle in YourBundle\Resources\translations\
.
小心:user.password。短翻译是在您的bundle资源\翻译中的验证器域。
Example:
例子:
# validators.en.yml
user:
password:
short: Password must be at least 8 characters long.
Don't know if this is version specific; I'm using symfony v2.8.3 and fosuser ~2.0@dev (a39d000).
不知道这是否是特定版本;我使用的是symfony v2.8.3和fosuser ~2.0@dev (a39d000)。
#3
3
In YML you would do it like this:
在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"
#1
14
You can overwrite the default settings by creating a new validation file in your bundle. This is bundle inheritance. Just copy (not cut).
您可以通过在包中创建一个新的验证文件来覆盖默认设置。这是包继承。复制(不)。
FOSUserBundle/Resources/config/validation/orm.xml
to YourBundle/Resources/config/validation/orm.xml
.
FOSUserBundle /资源/ config /验证/ orm。xml YourBundle /资源/配置/验证/ xml文件。
(couchdb.xml
, mongodb.xml
, propel.xml
respectively)
(couchdb。xml,mongodb。xml,推动。xml分别)
and adjust it to your needs. Change the class name, then add your constraints:
并根据你的需要进行调整。更改类名,然后添加约束:
<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>
Read more about which constraints are available (and how to use them with xml configuration ) in the Validation Constraints Reference.
阅读验证约束引用中哪些约束可用(以及如何在xml配置中使用它们)的更多信息。
#2
4
The easiest thing for me was to overwrite the property in my custom entity and change the settings for the assertion:
对于我来说,最简单的事情是在我的自定义实体中重写属性,并更改断言的设置:
<?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;
// ...
}
Don't forget the validation groups.
不要忘记验证组。
Heads up: user.password.short
translation is in the validators
Domain of your bundle in YourBundle\Resources\translations\
.
小心:user.password。短翻译是在您的bundle资源\翻译中的验证器域。
Example:
例子:
# validators.en.yml
user:
password:
short: Password must be at least 8 characters long.
Don't know if this is version specific; I'm using symfony v2.8.3 and fosuser ~2.0@dev (a39d000).
不知道这是否是特定版本;我使用的是symfony v2.8.3和fosuser ~2.0@dev (a39d000)。
#3
3
In YML you would do it like this:
在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"