Custom Validation

Hi everyone

Excuse me for my very bad english

What directory do I save custom validation class and how import it

thanks

No need to import anything. Just put the class under corresponding namespace and then write "use" statement where using it.

Hi

you could use either protected/extensions or protected/components (Personally Used components for this type of class)

check also this

http://www.yiiframework.com/forum/index.php/topic/17332-where-to-save-own-validator-class/

I agree with Gustavo :)

I put my custom validator file under component directory but I received an error

Fatal Error – yii\base\ErrorException

Class ‘phonenumber’ not found

What is the name of the class? You need to use fully qualified name including namespace.

Thanks for you’r reply.

When I put my custom validation file under yii/validators directory and use fully qualified name this work. but when I put under another directory doesn’t work.

I dont know how and where I add Yii::setAlias to auto load that file.

Thanks again.

You don’t need to use setalias. Just use fully qualified name of the class.

For example I extend validator class named PhoneValidator in file PhoneValidator.php and put in under myproject/additionalValidators and in rules I write


[['phonenumber'], 'yii\validators\PhoneNumberValidator']

but this not work but if I put PhoneValidator.php under yii/validators it work.

For first situation realy not nead use setalias?

Again excuse me for my bad english.

Well, you need to use the same class name as you’re using for the class. i.e. if you’re putting your class to myproject/additionalValidators and the class namespace is myproject/additionalValidators you should write myproject\additionalValidators\PhoneNumberValidator in rules.

I did what you said but doesn’t work until I add


Yii::setAlias('@myproject', realpath(__DIR__ . '/../../myproject'));

to params.php file

also in official document

is wrote

Well, that’s true if you have your own root namespace that doesn’t match application namespace.

Thanks alot for you’r responsibility.