Standalone Validator in advanced application template


When I use my Standalone Validator i see following error.

my class is in common/validator/NationalIdValidator.php

Please help me.

PHP User Error – yii\base\ErrorException

Exception ‘ReflectionException’ with message ‘Class NationalId does not exist’

in D:\xampp\htdocs\repos\trunk\vendor\yiisoft\yii2\di\Container.php:411

Stack trace:

#0 D:\xampp\htdocs\repos\trunk\vendor\yiisoft\yii2\di\Container.php(411): ReflectionClass->__construct(‘NationalId’)

#1 D:\xampp\htdocs\repos\trunk\vendor\yiisoft\yii2\di\Container.php(354): yii\di\Container->getDependencies(‘NationalId’)

#2 D:\xampp\htdocs\repos\trunk\vendor\yiisoft\yii2\di\Container.php(147): yii\di\Container->build(‘NationalId’, Array, Array)

#3 D:\xampp\htdocs\repos\trunk\vendor\yiisoft\yii2\BaseYii.php(344): yii\di\Container->get(‘NationalId’, Array, Array)

#4 D:\xampp\htdocs\repos\trunk\vendor\yiisoft\yii2\validators\Validator.php(204): yii\BaseYii::createObject(Array)

#5 D:\xampp\htdocs\repos\trunk\vendor\yiisoft\yii2\base\Model.php(422): yii\validators\Validator::createValidator(‘NationalId’, Object(common\models\Person), Array, Array)

Show code how it connecting and code of the validator. And pay attention to namespaces.

I have the same problem… I’m still searching…

In fact rules expects the complete class name with path…

Suppose we have our validator class named :




[size=“2”]We have to declare it in model’s rules like this : [/size]

use app\components\MyCompareValidator;


['my_field', MyCompareValidator::className(), 'my_params']


I found problem, I should using namespaces that yii supports in advances application template.

and some notes: please check yii docs

1- addError function in validation that i read in docs have 2 args but actually it has 3 args

please see :

and then :