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.

Phalcon Windows 3.3.2 Validator broken?

First time posting, so sorry if the formatting isn't clean.

I am trying to follow a tutorial, and I've checked the docs, and they both agree that this is how the Validators should be structured, but I'm still having this problem. Here's the Validators in question:

    $this->validate(new \Phalcon\Mvc\Model\Validator\Email([
          'field' => 'email'
        , 'message' => 'Invalid email'
    ]));
    $this->validate(new \Phalcon\Mvc\Model\Validator\Uniqueness([
          'field' => 'email'
        , 'message' => 'Email already in use'
    ]));
    $this->validate(new \Phalcon\Mvc\Model\Validator\StringLength([
          'field' => 'password'
        , 'max' => '50'
        , 'max' => '4'
        , 'messageMaximum' => 'Must be 50 characters or less'
        , 'messageMinimum' => 'Must be 4 characters or more'
    ]));

My problem is, according to the docs, all the objects above implement the "Phalcon\Mvc\Model\ValidatorInterface", but when my code hits these validators, I get "Fatal error: Uncaught TypeError: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface". Am correct in assuming this is a bug in the library I'm using, and not my code?

Hi @FluidMind the problem is very simple. \Phalcon\Mvc\Model\Validator namespace has changed by \Phalcon\Validator\Validator check new docs

Good luck

edited Jun '18

ok, namespace changed per docs....Validator\Validator isn't the correct path, but Validation\Validator could be...Here's the validators now:

    $this->validate(new \Phalcon\Validation\Validator\Email([
          'field' => 'email'
        , 'message' => 'Invalid email'
    ]));
    $this->validate(new \Phalcon\Validation\Validator\Uniqueness([
          'field' => 'email'
        , 'message' => 'Email already in use'
    ]));
    $this->validate(new \Phalcon\Validation\Validator\StringLength([
          'field' => 'password'
        , 'max' => '50'
        , 'min' => '4'
        , 'messageMaximum' => 'Must be 50 characters or less'
        , 'messageMinimum' => 'Must be 4 characters or more'
    ]));

But I got "Fatal error: Uncaught TypeError: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Validation\Validator\Email"



133.6k
Accepted
answer

No no no. you need to create Validation object like this:

public function validation()
{
    $validation = new Validation();
     $validation->add('email', new \Phalcon\Validation\Validator\Email([
        , 'message' => 'Invalid email'
    ]));
    $validation->add('email', new \Phalcon\Validation\Validator\Uniqueness([
        , 'message' => 'Email already in use'
    ]));
    $validation->add('password', new \Phalcon\Validation\Validator\StringLength([
        , 'max' => '50'
        , 'min' => '4'
        , 'messageMaximum' => 'Must be 50 characters or less'
        , 'messageMinimum' => 'Must be 4 characters or more'
    ]));

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