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

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

I was following custom model validators of this page, https://docs.phalcon.io/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)
{
...
}