FOSUserBundle - Validation for username, password

2019-01-11 14:23发布

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.

3条回答
三岁会撩人
2楼-- · 2019-01-11 14:33

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

查看更多
女痞
3楼-- · 2019-01-11 14:36

In YML you would do it like this:

# 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"
查看更多
我只想做你的唯一
4楼-- · 2019-01-11 14:44

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.

查看更多
登录 后发表回答