Snehalk
Snehalk

Reputation: 61

Yii2 checkbox array with when client validation not working

I was using checkboxlist() but in the array was not working with checkbox list to keep checkbox checked on edit page so changed the checkbox to checkbox array as below.

<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
   <?php $check = (in_array(3, $visible_to))?'checked="checked"':''; ?>
   <?= $form->field($model,'chk_visible_to[]',
       [
        'options' => ['class' => 'form-group p-t-5'],
        'template' => '<div class="checkbox style-grey"><label class="control-label"><input type="checkbox" value="3" class="form-control" name="chk_visible_to[]" '.$check.'>'.Yii::t('frontend','lbl_Publishers').'</label></div>'
       ])->checkbox(['label' => null]); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
    <?php $check = (in_array(4, $visible_to))?'checked="checked"':''; ?>
       <?= $form->field($model,'chk_visible_to[]',
         [
           'options' => [
               'class' => 'form-group p-t-5'
            ],
           'template' => '<div class="checkbox style-grey">
             <label class="control-label"><input type="checkbox" value="4"
             class="form-control" name="chk_visible_to[]" '.$check.'>'
            .Yii::t('frontend','lbl_Agents').' / 
            '.Yii::t('frontend','lbl_Agency').'</label></div>'
         ])
       ->checkbox(['class' => 'form-control checkPrivacy','label' => null]); ?>
</div>

Now also I have a radio button in the page I have to validate at least one checkbox required when selected radio value is 2.

In model, I have to define

public $chk_visible_to;

['chk_visible_to','required','when' => function($model){
                         return $model->flg_profile_visiblity == '2'  ;
                        },
         'whenClient' => "function (attribute, value) {
               return $('#memberprivacy-txt_profile_visiblity').val() == 1;
         }",
         'message' => 'Please select atleast one member type'
]

#memberprivacy-txt_profile_visiblity is hidden input, set value 1 when radio with value 2 is checked. But client validation is not working.

Upvotes: 0

Views: 1145

Answers (1)

Snehalk
Snehalk

Reputation: 61

I have fixed this with two hidden fields. Rather than making required checkbox array. I take hidden field and apply required validation for that hidden field.

Ex: I have 3 radio button with values 1,2,3 and when radio with value 2 is selected at least one checkbox must be checked. On change of radio button set value to first hidden. And on check uncheck of check box set length for other hidden.

<div class="row">
    <div class="col-lg-12">
        <?= $form->field($model,'flg_profile_visiblity',['options' => ['class' => 'form-group custom-radio-btns p-t-0']])->radioList(
                                            [0 => Yii::t('frontend','lbl_invisible_to_all_users'), 1 => Yii::t('frontend','lbl_Visible_to_ANYONE'), 2 => Yii::t('frontend','lbl_Visible_to_ONLY_Members')],
                                            ['item' => function($index, $label, $name, $checked, $value){
                                                $id     = 'profile_visible_'.$value;
                                                $check  = ($checked == 1)?"checked='checked'":"";
                                                return "<div class='radio style-blue'><label class='control-label'><input type='radio' ".$check." class='form-control' name='".$name."' id='".$id."' value='".$value."' >".$label."</label></div>";
                                        }])->label(false); ?>
    </div>
</div>
<?= $form->field($model,'txt_profile_visiblity',['options' => ['class' => 'show-none']])->hiddenInput(['value' => $model->flg_profile_visiblity])->label(false); ?>

Check Box Part

<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?php $check = (in_array(6, $visible_to))?'checked="checked"':''; ?>
    <?= $form->field($model,'chk_visible_to[]',['options' => ['class' => 
   'form-group p-t-5'],'template' => 
    '<div class="checkbox style-grey"><label class="control-label"><input 
     type="checkbox" value="6" class="form-control" 
     name="chk_visible_to[]"'.$check.'>'
     .Yii::t('frontend','lbl_Service_Provider').'</label></div>'
                                            ])->checkbox(['class' => 'form-control checkPrivacy','label' => null]); ?>
  </div>
  <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
                                            <?php $check = (in_array(7, $visible_to))?'checked="checked"':''; ?>
                                            <?= $form->field($model,'chk_visible_to[]',['options' => ['class' => 'form-group p-t-5'],'template' => 
                                            "<div class='checkbox style-grey'><label class='control-label'><input type='checkbox' value='7' class='form-control' name='chk_visible_to[]' ".$check.">".Yii::t('frontend','lbl_Approved_Contacts')."</label>\n{error}</div>"
                                            ])->checkbox(['class' => 'form-control checkPrivacy','label' => null]); ?>
   </div>
   <div class="row">
      <?php $visible_to_len = count($visible_to); ?>
           <?= $form->field($model,'validate_membertype_checkbox',['options' => ['class' => ''],'template' => "{input}\n{error}"])->hiddenInput(['value' => $visible_to_len,'id' => 'validate_membertype_checkbox'])->label(false); ?>
 </div>

Model Validation Part

['validate_membertype_checkbox','required','when' => function($model){
                                            return $model->flg_profile_visiblity == '2';
                                        },'whenClient' => "function(attribute, value){
                                            return $('#memberprivacy-txt_profile_visiblity').val() == '2';
                                        }",
                                        'message' =>'Please select atleast one member type'
        ],

Set values to hidden using jquery code.

Upvotes: 1

Related Questions