Custom validation class not working

I was trying to write a validation class for validating postal

I place the file in app\components\PostalcodeValidation

<?php

namespace app\components;

use yii\validators\Validator;

use app\models\Status;

class PostCodeValidator extends Validator

{

public function init()


{


    parent::init();


    &#036;this-&gt;message = 'Invalid status input.';


}





public function validateAttribute(&#036;model, &#036;attribute) {


     &#036;value = &#036;model-&gt;&#036;attribute;


     return true;


     


}








 public function clientValidateAttribute(&#036;model, &#036;attribute, &#036;view) {


    &#036;message = json_encode('Invalid pincode', JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);


    return &lt;&lt;&lt;JS


    var regexObj = {


    canada: /^[ABCEGHJKLMNPRSTVXY]&#092;d[ABCEGHJKLMNPRSTVWXYZ]( )?&#092;d[ABCEGHJKLMNPRSTVWXYZ]&#092;d&#036;/i, //i for case-insensitive


    usa: /^&#092;d{5}(-&#092;d{4})?&#036;/,


        }


  


var regexp = new RegExp(regexObj.canada);


if (regexp.test(value)) {


    return true;


}


regexp = null;


regexp = new RegExp(regexObj.usa);


if (regexp.test(value)) {


    return true;


}





messages.push(&quot;Not a valid Postal Code or Zip Code&quot;);

JS;

}

}

I want to use this class to validation in model

public function rules() {

    return [


           [['postal_code'], 'app&#092;components&#092;PostCodeValidator', ],


           ];


            }

It works fine when I follow basic template file structure .

when I change move web folder to sub directory yii generate error app\components\PostCodeValidator

What I think is that component folder is not registered in yii2 so I am getting error

Can any one help how to resolve this problem

how to register a new folder to yii 2

I used this reference to create this class but there also it is not mentioned how to use it

http://www.yiiframework.com/doc-2.0/guide-input-validation.html#implementing-client-side-validation

Thanks

Have you tried to set the rule like this:




use app\components\PostCodeValidator;


public function rules() {

    return [

        [['postal_code'], PostCodeValidator::className()],

    ];

}



I tried your code but it is till till not working .

The error is

Exception ‘ReflectionException’ with message ‘Class PostCodeValidator::className() does not exist’

If you have yii2 basic template place your PostCodeValidator.php file in /components folder not in /app/components (see explanation here http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html#using-the-yii-autoloader ). The rest stays the same so:

PostCodeValidator namespace:


namespace app/components;

In your model:


use app/components/PostCodeValidator;

Rule:


[['postal_code'], PostCodeValidator::className()],

Thanks Bizley for reply

But I was unable to understand your last reply.

I read the link you shared but I was unable to understand where to write autoload code for Automaticly including clases in app\component folder

As there was no folder with the name ‘component’ in basic template. I created that folder and placed my PostCodeValidator in that folder.

In which file do I need to write the autoload code for including component folder .

Ok, I thought you have created folder "app" and "components" inside the first one.

So you move "web" folder somewhere else? Have you changed the paths inside the index.php from that folder?

You can always modify class map in the tricky cases ( http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html#class-map )