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.

Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface

I have upgraded to Phalcon 3.0.0:

  • Build Date Aug 9 2016 18:43:20
  • Powered by Zephir Version 0.9.3a-dev-e716dbe641

I am now experiencing the following problem in some of my models validators:

Catchable fatal error: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Uniqueness given

In my Model I have:

    public function validation()
    {
        $this->validate(new \Phalcon\Mvc\Model\Validator\Uniqueness(
            [
                'field' => 'name',
                'message' => 'This display name is already in use. Please try another one.'
            ]
        ));

        $this->validate(new \Phalcon\Mvc\Model\Validator\PresenceOf(
            [
                "field" => "name",
                "message" => "You must enter the group name to save it."
            ]
        ));

        return $this->validationHasFailed() != true;
    }

Either this is now wrong and the documentation is incorrect or there is a bug in Phalcon.

Can anyone advise me on this matter? Why is phalcon demanding Phalcon\ValidationInterface and not Phalcon\Mvc\Model\ValidatorInterface?



60.9k
Accepted
answer

mm yea, this is the old way. Docs has to be fixed there ..

https://blog.phalconphp.com/

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', //your field name
            new EmailValidator([
                'model' => $this,
                'message' => 'Please enter a correct email address'
            ])
        );

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

        return $this->validate($validator);
    }
}
edited Aug '16
'model' => $this,

also not needed.

Thanks!

This is the solution i was looking for.

To make things more interesting I know have this on one of my collections :

Argument 1 passed to Phalcon\Mvc\Collection::validate() must implement interface Phalcon\Mvc\Model\ValidatorInterface, instance of Phalcon\Validation given in

I take it they have not applied this to $this->validate() on \Phalcon\Mvc\Collection.