Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOSUserBundle - Validation for username, password or email fields

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.

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?

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.

like image 597
M. Winterinho Avatar asked May 22 '13 22:05

M. Winterinho


2 Answers

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\.

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).

like image 79
gblock Avatar answered Oct 09 '22 09:10

gblock


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.

like image 30
Nicolai Fröhlich Avatar answered Oct 09 '22 08:10

Nicolai Fröhlich