app/forms/Registration.php: change Uniqueness validator to Callback + cleanup username validators abit.
This commit is contained in:
parent
c71416908a
commit
8237a71f52
1 changed files with 16 additions and 13 deletions
|
|
@ -28,7 +28,6 @@ use Phalcon\Forms\Element\Text,
|
|||
* Validators
|
||||
*/
|
||||
use Phalcon\Validation\Validator\Callback as CallbackValidator,
|
||||
Phalcon\Validation\Validator\Uniqueness as UniquenessValidator,
|
||||
Phalcon\Validation\Validator\Alnum as AlnumValidator,
|
||||
Phalcon\Validation\Validator\Email as EmailValidator,
|
||||
Phalcon\Validation\Validator\StringLength as StringLengthValidator,
|
||||
|
|
@ -50,15 +49,20 @@ class Registration extends FormBase
|
|||
|
||||
$username->setLabel('Username');
|
||||
|
||||
$username->addValidator(new AlnumValidator());
|
||||
|
||||
$validator = new UniquenessValidator(array(
|
||||
'model' => new UserModel(),
|
||||
'message' => 'The username already exists.',
|
||||
'attribute' => 'username',
|
||||
));
|
||||
|
||||
$username->addValidator($validator);
|
||||
$username->addValidators([
|
||||
new AlnumValidator([
|
||||
'message' => 'Username must contain only letters and numbers.'
|
||||
]),
|
||||
new StringLengthValidator([
|
||||
'min' => 2,
|
||||
'messageMinimum' => 'Username must be at least :min characters long.',
|
||||
]),
|
||||
new CallbackValidator([
|
||||
'callback' => function($data) { return User::findFirstByUsername($data['username']) === false; },
|
||||
'message' => 'The username already exists.',
|
||||
'attribute' => 'username',
|
||||
])
|
||||
]);
|
||||
|
||||
$this->add($username);
|
||||
|
||||
|
|
@ -90,10 +94,9 @@ class Registration extends FormBase
|
|||
new IdenticalValidator([
|
||||
'accepted' => $this->getEntity()->getEmail(),
|
||||
]),
|
||||
new UniquenessValidator([
|
||||
'model' => new UserModel(),
|
||||
new CallbackValidator([
|
||||
'callback' => function($data) { return User::findFirstByEmail($data['email']) === false; },
|
||||
'message' => 'This email already exist.',
|
||||
'attribute' => 'email',
|
||||
])
|
||||
]);
|
||||
|
||||
|
|
|
|||
Reference in a new issue