Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii Model: Username rule allow a-z_-

Tags:

php

yii

How can I make a rule at Yii Framework to allow only characters a-z, underline and dash in username field?

Thanks

like image 651
Luciano Nascimento Avatar asked Nov 01 '12 04:11

Luciano Nascimento


1 Answers

You should be able to use the match/pattern rule outlined in the manual:

public function rules() {
    return array(
        array('username', 'required'),
        array(
            'username',
            'match', 'not' => true, 'pattern' => '/[^a-zA-Z_-]/',
            'message' => 'Invalid characters in username.',
        ),
    );
}

This, untested code, will require the username field to contain data and then validate that it doesn't contain a character not in the accepted-character list (a-z, underscore, and dash).

like image 73
newfurniturey Avatar answered Nov 14 '22 23:11

newfurniturey