validation for a field that get its values from multiple dropdownlist

hi there

i have a required field that will get its value from multiple dropdowns and at least one of them must be selected.

the problem is the validation only works for first dropdown

model:




class Ads extends ActiveRecord {

....

....

    public $sub_categories;

public function rules() {

        $rules = [

            ['sub_categories', 'required'],

            ......

            ......

        ];

        return $rules;

    }



view:




.........

.......

<table style="table-layout: fixed;">

        <?php 

        $categories = Category::find()->all();

        for($index = -1; $index < count($categories) - 1; ):?>

            <tr>

                <?php $index++; if(isset($categories[$index])): ?>

                    <td><?php echo $form->field($model, 'sub_categories')->dropDownList(ArrayHelper::map($categories[$index]->subCategories, 'id', 'title'), ['prompt' => $categories[$index]->name]);?></td>

                <?php endif; ?>

                <?php $index++; if(isset($categories[$index])): ?>

                    <td><?php echo $form->field($model, 'sub_categories')->dropDownList(ArrayHelper::map($categories[$index]->subCategories, 'id', 'title'), ['prompt' => $categories[$index]->name]);?></td>

                <?php endif; ?>

                <?php $index++; if(isset($categories[$index])): ?>

                    <td><?php echo $form->field($model, 'sub_categories')->dropDownList(ArrayHelper::map($categories[$index]->subCategories, 'id', 'title'), ['prompt' => $categories[$index]->name]);?></td>

                <?php endif; ?>

            </tr>

        <?php endfor; ?>

    </table>

......

.......



how to set validation rules?

thanks in advanced

u can define custom variable in model.

::Model::




public dropdown1;

public dropdown2;


    public function rules(){

return [

            

            [['dropdown1', 'dropdown2'], 'required'],

}

::view::


 <?php 

        $categories = Category::find()->all();

        for($index = -1; $index < count($categories) - 1; ):?>

            <tr>

                <?php $index++; if(isset($categories[$index])): ?>

                    <td><?php echo $form->field($model, 'sub_categories')->dropDownList(ArrayHelper::map($categories[$index]->subCategories, 'id', 'title'), ['prompt' => $categories[$index]->name]);?></td>

                <?php endif; ?>

                <?php $index++; if(isset($categories[$index])): ?>

                    <td><?php echo $form->field($model, 'dropdown1')->dropDownList(ArrayHelper::map($categories[$index]->subCategories, 'id', 'title'), ['prompt' => $categories[$index]->name]);?></td>

                <?php endif; ?>

                <?php $index++; if(isset($categories[$index])): ?>

                    <td><?php echo $form->field($model, 'dropdown2')->dropDownList(ArrayHelper::map($categories[$index]->subCategories, 'id', 'title'), ['prompt' => $categories[$index]->name]);?></td>

                <?php endif; ?>

            </tr>

        <?php endfor; ?>

If only one of them should be selected, it is not correct to put a required validator on them because each individual one is not required, only one of them. The easiest way is probably to use conditional validation where an inline method can decide whether to call the validation:

[

['dropdown1', 'required', 'when' =&gt; function(&#036;model) {


    return &#036;model-&gt;dropdown2 == '';


}],

]

Something like that! It can also work on the client with ‘whenClient’

http://www.yiiframework.com/doc-2.0/guide-input-validation.html#conditional-validation