Answer the question
In order to leave comments, you need to log in
Validation via "each" in yii2?
validation does not work through each, the
rules
fieldpublic $child_age = [];
public function rules()
{
return [
['child_age', 'each', 'rule' => ['integer']],
['child_age', 'each', 'rule' => ['compare', 'compareValue' => 2, 'operator' => '>=']]
];
}
<div class="number-control">
<?= $form->field($model, 'child_age[]', [
'template' => '{input}{error}'.Html::button('', ['class' => 'minus']).Html::button('', ['class' => 'plus'])
])->input('text', ['value' => 6])->label(false) ?>
</div>
Answer the question
In order to leave comments, you need to log in
Try adding the skipOnEmpty parameter to the validator. Like this:
public function rules()
{
return [
['child_age', 'each', 'rule' => ['integer'], 'skipOnEmpty' => false],
['child_age', 'each', 'rule' => ['compare', 'compareValue' => 2, 'operator' => '>='], 'skipOnEmpty' => false]
];
}
It is unlikely that the validation does not work if everything is done correctly. Validation may not work only in the following cases:
1. You do not load the attribute data from the request into the model (it would not hurt to show the controller where this happens)
2. You have client-side validation or Ajax validation on other fields before submitting the form to the server, accordingly each-validation does not work.
It would not hurt that you showed the complete code of the form.
In general, judging by the code, you are reinventing the wheel. To solve your problem, it is easier to use a ready-made solution, for example this
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question