FOSUserBundle - Validation for username, password or email fields
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
.
(couchdb.xml
, mongodb.xml
, propel.xml
respectively)
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.
The easiest thing for me was to overwrite the property in my custom entity and change the settings for the assertion:
<?phpnamespace 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\
.
Example:
# validators.en.ymluser: 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).
In YML you would do it like this:
# src/Acme/ProjectBundle/Resources/config/validation.ymlAcme\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"