Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

I'm confused about the new Validator in 2.1

From 2.1.x blog post:

Should the line return $this->validate(); be instead return $validator? I'm not seeing how anything is being assigned permanently here.

<?php

namespace Invo\Models;

use Phalcon\Mvc\Model;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Email as EmailValidator;
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;

class Users extends Model
{
    public function validation()
    {
        $validator = new Validation();

        $validator->add(
            'email',
            new EmailValidator()
        );

        $validator->add(
            'username',
            new UniquenessValidator(
                [
                    'message' => 'Sorry, That username is already taken',
                ]
            )
        );

        return $this->validate();
    }
}


33.6k
Accepted
answer

You have to pass the validator:

return $this->validate($validator);


46.8k

Ok cool. So just small bug in the blog post only?

Will fix the article, thanks