my custom validation rule works perfectly in server side but not in client side here is the code :
Model :
/**
* @inheritdoc
*/
public function rules()
{
return [
[['iscompany'], 'boolean'],
[['lname', 'national_code', 'address', 'bc_place', 'bc_date', 'postal_code'], 'required'],
[['name', 'father_name'], 'required', 'when' => function($model) {
return !$model->iscompany;
}],
[['address'], 'string'],
[['parent1', 'parent2', 'param1', 'param2', 'param3'], 'integer'],
[['name', 'lname', 'job', 'father_name', 'bc_place', 'description', 'param4', 'param5', 'param6'], 'string', 'max' => 255],
[['national_code', 'bc_number', 'tel', 'cell'], 'string', 'max' => 16],
[['bc_date', 'postal_code'], 'string', 'max' => 10],
['national_code', 'validateNationalCode', 'skipOnEmpty' => false, 'skipOnError' => false, 'when' => function($model) {
return !$model->iscompany;
}],
];
}
/**
* check if national code is correct ot not.
* @param $attribute
* @return bool
*/
public function validateNationalCode($attribute)
{
$code = $this->$attribute;
$code = (string) preg_replace('/[^0-9]/','',$code);
if(strlen($code)>10 or strlen($code)<8 or in_array($code, array('0000000000','1111111111','2222222222','3333333333','4444444444','5555555555','6666666666','7777777777','8888888888','9999999999')))
{
$this->addError($attribute, Yii::t('app', "must be 10digit numerical and not same digit"));
return false;
}
if(strlen($code)==<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />
$code = "00".$code;
if(strlen($code)==9)
$code = "0".$code;
$list_code = str_split($code);
$last = (int) $list_code[9];
unset($list_code[9]);
$i = 10;
$sum = 0;
foreach($list_code as $key=>$_)
{
$sum += intval($_) * $i;
$i--;
}
$mod =(int) $sum % 11;
if($mod >= 2)
$mod = 11 - $mod;
if(!($mod == $last))
$this->addError($attribute, Yii::t('app', "must be true format"));
}
Controller :
public function actionCreate()
{
$model = new Customer();
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
View :
...
<?php $form = ActiveForm::begin([
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "{label}\n<div class=\"col-lg-8\">{input}</div>\n<div class=\"col-lg-offset-3 col-lg-8\">{error}</div>",
'labelOptions' => ['class' => 'col-lg-3 control-label'],
],
]); ?>
...
even the builtin rules works correctly in same field i put custom rule on it!
any advice?