当客户端验证不起作用时,Yii2复选框数组

时间:2018-02-14 07:00:36

标签: checkbox yii2 client

我正在使用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 1 radiovalue 2时设为checked。但客户端验证无效。

1 个答案:

答案 0 :(得分:1)

我用两个隐藏的字段修复了这个问题。而不是制作所需的复选框数组。我采用隐藏字段并对该隐藏字段应用所需的验证。

例如:我有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代码将值设置为隐藏。