T
T
teodor7teodor72016-07-27 14:36:42
Yii
teodor7teodor7, 2016-07-27 14:36:42

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

2 answer(s)
A
Anton Natarov, 2016-07-27
@HanDroid

$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(); ?>

I gave the code conditionally, you can make a method in the model and request it in the controller.
Well, see the rules yourself, the type is boolean, and it is mandatory or optional. the rest according to circumstances.

M
mitaichik, 2016-07-28
@mitaichik

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); ?>
}

But this is speculation and needs to be verified.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question