A
A
Anton Medvedev2012-12-14 13:56:20
symfony
Anton Medvedev, 2012-12-14 13:56:20

How to add custom validation rules to FOSUserBundle username?

On the site for registration I use FOSUserBundle, only now it allows using any characters for username.
How to add a validator for username allowing only [a-b0-9] latin characters?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
stnw, 2012-12-14
@Elfet

You can add the loadValidatorMetadata method to the User class.
My example of a nickname on a project (regex needs to be replaced with the required one):
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint(
'nickname',
new Regex(array(
'pattern' => '/^[a-yayoa -z0-9_\- ]+$/ui',
'groups' => array('Profile', 'forum'),
'message' => 'Nick must not contain %, !, ~, #, &, No.'
))
);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question