We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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}


98.9k

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? (:



98.9k

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

How i can write this validation in my model?



26.3k
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 https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Validator.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 https://docs.phalcon.io/en/latest/reference/validation.html#validators or this is my post https://forum.phalcon.io/discussion/3123/custom-date-validator



22.1k