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.

compatible with Phalcon\\Mvc\\Model\\ValidatorInterface

I was following custom model validators of this page, https://docs.phalconphp.com/en/latest/reference/models.html#validating-data-integrity

to build a validators for changing username, if a username exists but not match to current session user, it would return as invalidate.

but I am getting an error of

UniqueValidatorUser::validate() must be compatible with Phalcon\\Mvc\\Model\\ValidatorInterface::validate(Phalcon\\Mvc\\ModelInterface $record)

It was fine with phalcon 1.3.4, but not working with latest 2.0.3

use Phalcon\Mvc\Model\ValidatorInterface;
use Multiple\Models\Users;

class UniqueValidatorUser extends Validator implements ValidatorInterface
{
    public function validate($model)
    {
        $field = $this->getOption('field');
        $value = $model->$field;
        $di    = DI::getDefault();


        $condition = 'status = ?0 AND '.$field.' = ?1';
        $identity = $di->get('auth')->getIdentity();

        if($identity){
            $condition .= ' AND id !='.$identity['id'];
        }
        $users = Users::find([
                        $condition,
                        'bind'=>[0=>1, 1=>$value],
                    ]); //** only apply to status=1 user,

        if(count($users))
        {
            $this->appendMessage("User name exist!", $field, "Unique");
            return FALSE;
        }
        return TRUE;
    }
}


4.0k
Accepted
answer
edited Jul '15

Set EntityInterface type of $model parameter (https://github.com/phalcon/cphalcon/blob/2.0.x/phalcon/mvc/model/validatorinterface.zep#L45)

public function validate(EntityInterface $model)
{
...
}