Generated ORM and validation is not compatible to itself? :)

I have a database, and generated the model by command line tool. For an "email" field, it generated:

    $this->validate(
        new Email(
            array(
                'field'    => 'email',
                'required' => true,
            )
        )
    );

this code for validation. When it runs, Phalcon halts:

Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Email given.

On the other hand, Im looking at this: https://docs.phalconphp.com/en/latest/reference/validation.html

this uses different format.



11.8k

newest, 2.0.8 version



20.3k

I'm not sure what's going on because Phalcon\Mvc\Model::validate() expects the argument to be of type Model\ValidatorInterface and not Phalcon\ValidationInterface like you said. Also 2.0.8 is not the newest version anymore, make sure to restart the webserver after upgrading Phalcon.



469

I have same problem with same code in Phalcon 2.1.0. Any solution for that?

I'm on version 2.0.10, and I'm getting the same results.

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

Any thoughts?

Got the same issue (phalcon version 2.1.0b)



20.3k

From 2.1.x onwards Phalcon\Mvc\Model\Validation is deprecated, check the blog post at https://blog.phalconphp.com/post/phalcon-2-1-beta-released

The example in the blog post has an error though. Now the difference is that you create a validator using Phalcon\Validation and then pass it to the validate method:

use Phalcon\Mvc\Model;
use Phalcon\Validation;
use Phalcon\Validation\Validator\ExclusionIn;

class Subscriptors extends Model
{

    public function validation()
    {
        $validator = new Validation();
        $validator->add('status', new ExclusionIn(array(
            'domain' => array('A', 'I')
        )));

        return $this->validate($validator);
    }
}


469

Above doesnt work for me:

return $this->validate(); gives BadMethodCallException: Wrong number of parameters

return $this->validate($validator); gives Phalcon\Validation\Exception: There is no data to validate



2.1k
edited Mar '16

public function validation() { $validator = new \Phalcon\Validation(); $validator->add('name', new \Phalcon\Validation\Validator\Uniqueness( [ 'model' => $this, 'message' => 'Sorry, robot name is already taken' ] ));

    return $this->validate($validator);
}

Above doesnt work for me:

return $this->validate(); gives BadMethodCallException: Wrong number of parameters

return $this->validate($validator); gives Phalcon\Validation\Exception: There is no data to validate

Here is the same

public function validation() { $this->validate(new Uniqueness( array( "field" => "email", "message" => "The email is already registered", ) ));

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

But this error:

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 C:\xampp\htdocs\adtube\app\models\Users.php on line 125


32.8k

I have the same problem



222

The same problem still occurs on 3.0.x

same problem, v3.0.3



6.5k
edited Feb '17

I had the same error and it solved it. As Lewiz correctly stated, you should create a validator using Phalcon\Validation and then pass it to the validate method.

Model's 'validate' method now expects Phalcon\Validation, which implements Phalcon\ValidationInterface. And \Phalcon\Validation\Validator\Uniqueness and other validators which you pass to Model’s ‘validate’ method are implementing Phalcon\Validation\ValidatorInterface that’s why you see this error after upgrade to newest phalcon, yes it maybe confusing, but everything works as documented.

Given the above, If you upgraded phalcon from deprectaed version fist you need: 1) change Phalcon\Mvc\Model\Validator\.... to \Phalcon\Validation\Validator\ e.g Phalcon\Mvc\Model\Validator\Uniqueness to \Phalcon\Validation\Validator\Uniqueness

2) change

public function validation()
    {
        $this->validate(new Uniqueness([
            'field' => 'email',
            'message' => 'The email is already registered'
        ]));

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

to

public function validation()
  {
      $validation = new Validation();

      $validation -> add('email', new Uniqueness([
          'field' => 'email',
          'message' => 'The email is already registered'
      ]));

      return $this->validate($validation);
  }

where Validation is instance of \Phalcon\Validation class