yii2复选框数组与客户端验证不起作用时



我正在使用 checkboxlist(),但是在数组中不使用复选框列表来保留在编辑页面上检查复选框,因此将复选框更改为复选框阵列,如下所示。

<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>

现在,我在页面中也有一个广播按钮,我必须验证至少一个选定的无线电值为2的复选框。

在模型中,我必须定义

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_visiblityhidden输入,当value 2radiochecked时,请设置value 1。但是客户端验证不起作用。

我已经用两个隐藏的字段修复了此问题。而不是进行必需的复选框数组。我采用隐藏的字段,并对该隐藏的字段应用所需的验证。

ex:我有3个带有值1,2,3的无线电按钮,当选择具有值2的无线电时,必须至少检查一个复选框。在更改单选按钮设置值为首先隐藏的时。并在检查选中选中的复选框设置了其他隐藏的长度。

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

复选框部分

<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>

模型验证部分

['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'
        ],

使用jQuery代码将值设置为隐藏。

最新更新