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
|
* Validators
|
||||||
*/
|
*/
|
||||||
use Phalcon\Validation\Validator\Callback as CallbackValidator,
|
use Phalcon\Validation\Validator\Callback as CallbackValidator,
|
||||||
Phalcon\Validation\Validator\Uniqueness as UniquenessValidator,
|
|
||||||
Phalcon\Validation\Validator\Alnum as AlnumValidator,
|
Phalcon\Validation\Validator\Alnum as AlnumValidator,
|
||||||
Phalcon\Validation\Validator\Email as EmailValidator,
|
Phalcon\Validation\Validator\Email as EmailValidator,
|
||||||
Phalcon\Validation\Validator\StringLength as StringLengthValidator,
|
Phalcon\Validation\Validator\StringLength as StringLengthValidator,
|
||||||
|
|
@ -50,15 +49,20 @@ class Registration extends FormBase
|
||||||
|
|
||||||
$username->setLabel('Username');
|
$username->setLabel('Username');
|
||||||
|
|
||||||
$username->addValidator(new AlnumValidator());
|
$username->addValidators([
|
||||||
|
new AlnumValidator([
|
||||||
$validator = new UniquenessValidator(array(
|
'message' => 'Username must contain only letters and numbers.'
|
||||||
'model' => new UserModel(),
|
]),
|
||||||
'message' => 'The username already exists.',
|
new StringLengthValidator([
|
||||||
'attribute' => 'username',
|
'min' => 2,
|
||||||
));
|
'messageMinimum' => 'Username must be at least :min characters long.',
|
||||||
|
]),
|
||||||
$username->addValidator($validator);
|
new CallbackValidator([
|
||||||
|
'callback' => function($data) { return User::findFirstByUsername($data['username']) === false; },
|
||||||
|
'message' => 'The username already exists.',
|
||||||
|
'attribute' => 'username',
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
|
||||||
$this->add($username);
|
$this->add($username);
|
||||||
|
|
||||||
|
|
@ -90,10 +94,9 @@ class Registration extends FormBase
|
||||||
new IdenticalValidator([
|
new IdenticalValidator([
|
||||||
'accepted' => $this->getEntity()->getEmail(),
|
'accepted' => $this->getEntity()->getEmail(),
|
||||||
]),
|
]),
|
||||||
new UniquenessValidator([
|
new CallbackValidator([
|
||||||
'model' => new UserModel(),
|
'callback' => function($data) { return User::findFirstByEmail($data['email']) === false; },
|
||||||
'message' => 'This email already exist.',
|
'message' => 'This email already exist.',
|
||||||
'attribute' => 'email',
|
|
||||||
])
|
])
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue