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();
$this->message = 'Invalid status input.';
}
public function validateAttribute($model, $attribute) {
$value = $model->$attribute;
return true;
}
public function clientValidateAttribute($model, $attribute, $view) {
$message = json_encode('Invalid pincode', JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
return <<<JS
var regexObj = {
canada: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ]( )?\d[ABCEGHJKLMNPRSTVWXYZ]\d$/i, //i for case-insensitive
usa: /^\d{5}(-\d{4})?$/,
}
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("Not a valid Postal Code or Zip Code");
JS;
}
}
I want to use this class to validation in model
public function rules() {
return [
[['postal_code'], 'app\components\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