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.

InclusionIn and Uniqueness validator errors

When i try to validate two values with two model validators i get errors on both Its a rest API and values are send like JSON in body. Errors occurs with InclusionIn and Uniqueness validators. Version of the Phalcon is 3.0.1

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Uniqueness;
use Phalcon\Mvc\Model\Validator\InclusionIn;

class Objects extends Model
{
    public function validation()
    {
        $this->validate(
            new InclusionIn(
                [
                    "field"  => "type",
                    "domain" => ["one","two"]
                ]
            )
        );

        $this->validate(
            new Uniqueness(
                [
                    "field"   => "name",
                    "message" => "The object name must be unique",
                ]
            )
        );
        if ($this->validationHasFailed() === true) {
            return false;
        }
    }
}

and the InclusionIn error is

( ! ) Fatal error: Uncaught TypeError: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Inclusionin given in C:\wamp64\www\twm\models\Robots.php on line 18
( ! ) TypeError: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Inclusionin given in C:\wamp64\www\twm\models\Robots.php on line 18

the Uniqueness error is

 ( ! ) Fatal error: Uncaught TypeError: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Uniqueness given in C:\wamp64\www\twm\models\Robots.php on line 28
( ! ) TypeError: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Uniqueness given in C:\wamp64\www\twm\models\Robots.php on line 28


107.5k
Accepted
answer
edited Mar '17

You muse use Phalcon\Validation namespace, check docs https://docs.phalconphp.com/en/3.0.1/reference/model-validation.html

namespace Store\Toys;

use Phalcon\Mvc\Model;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Uniqueness;
use Phalcon\Validation\Validator\InclusionIn;

class Robots extends Model
{
    public function validation()
    {
        $validator = new Validation();

        $validator->add(
            "type",
            new InclusionIn(
                [
                    "domain" => [
                        "Mechanical",
                        "Virtual",
                    ]
                ]
            )
        );

        $validator->add(
            "name",
            new Uniqueness(
                [
                    "message" => "The robot name must be unique",
                ]
            )
        );

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


494

Ahh... thank you obviosly the problem was in wrong doc i read before, with correct one work like a charm . Ty you