Validator interface

I just write something like this:

use Phalcon\Validation\Validator\Between;

class Users extends \Phalcon\Mvc\Model {
    public function validation() {
        $this->validate(new Between(array(
            'minimum' => 3,
            'maximum' => 21,
            'field' => 'nickname',
            'message' => 'reg_error_nick_len'
        )));
    }
}

And when I try to save model I have this exception trace (uniquness and email work normal):

Unexpected value type: expected object implementing Phalcon\Mvc\Model\ValidatorInterface, object of type Phalcon\Validation\Validator\Between given
\#0 W:\domains\ely.by\app\models\Users.php(60): Phalcon\Mvc\Model->validate(Object(Phalcon\Validation\Validator\Between))
\#1 [internal function]: Users->validation()
\#2 [internal function]: Phalcon\Mvc\Model->fireEventCancel('validation')
\#3 [internal function]: Phalcon\Mvc\Model->_preSave(Object(Phalcon\Mvc\Model\MetaData\Memory), true, 'id')
\#4 W:\domains\ely.by\app\controllers\AuthorizationController.php(88): Phalcon\Mvc\Model->save(Array, Array)
\#5 [internal function]: AuthorizationController->finishAction()
\#6 [internal function]: Phalcon\Dispatcher->dispatch()
\#7 W:\domains\ely.by\public\index.php(32): Phalcon\Mvc\Application->handle()
\#8 {main}


82.8k

Phalcon\Validation\Validator\Between is a validator that belongs to the Phalcon\Validation component, not to Phalcon\Mvc\Model

And what i need to to? (:



82.8k

That's why you're getting that exception, what is exactly the question?

How i can write this validation in my model?



16.2k
edited Sep '14

Phalcon has diffirent validation classes for forms and models.

To build validator for a model you need to extend Phalcon's Phalcon\Mvc\Model\Validator abstract class with an API described here http://docs.phalconphp.com/en/latest/api/Phalcon%5Mvc%5Model%5Validator.html

It is not described in the docs how to do this (validators for models).

I think it is very smiliar to creating forms validators. And this is described. You can read about it in the docs here http://docs.phalconphp.com/en/latest/reference/validation.html#validators or this is my post http://forum.phalconphp.com/discussion/3123/custom-date-validator



12.1k