Answer the question
In order to leave comments, you need to log in
How to do conditional validation?
Good evening.
on yii2 I'm trying to validate by condition - if the TIN is entered for 10 characters, then the checkpoint is 9-character, if the TIN is entered for 12 characters, then the checkpoint is not needed.
came out something like this:
['inn', function ($attribute, $params, $validator) {
if (preg_match('/^[0-9]{12}$/', $this->$attribute) || preg_match('/^[0-9]{10}$/', $this->$attribute)) {
$this->addError($attribute, 'Инн должен быть 10 или 12 символов.');
}
}],
[[kpp], 'match', 'pattern' => '/^[0-9]{9}*$/i', 'when' => function ($model) {
return preg_match('/^[0-9]{10}$/', $this->inn);}
]
Answer the question
In order to leave comments, you need to log in
['kpp', 'string', 'min'=>9, 'max'=>9, 'when'=>function($model){
$l = strlen ($model->inn);
return ($l == 12);
}],
['kpp', 'required', 'when'=>function($model){
$l = strlen ($model->inn);
return ($l != 12);
],
$attribute = intval($attribute);
if(empty($attribute) || preg_match('/^\d{12}$/', $attribute))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question