Answer the question
In order to leave comments, you need to log in
How to make checkBox and validation for form with variable number in yii2?
Good afternoon, tell me, we display categories through REST, the user selects the necessary ones by ticking the checkboxes. How can you provolute and what is the best widget to use?
Answer the question
In order to leave comments, you need to log in
$categories = Category::find()->indexBy('id')->orderBy('id')->all();
$items = ArrayHelper::map($categories,'id','title');
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'checkboxList')->checkboxList($items); ?>
<?php ActiveForm::end(); ?>
Not enough information. What do you want to validate?
On the offhand: that there is at least one category - required, that all integer values \u200b\u200bare each, that there are such categories - in or exist, b, etc. You can write a custom validator (method\anonymous function\class).
I would choose a custom validator as there is a lot to check and one method that does it all is more expressive than multiple validators.
As for the widget - Angular :) Perhaps you can do something like
for($i =0; $i< 10; $i++){
$form->field($model, 'checkboxList[]')->checkboxList($items); ?>
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question