Yii CCheckBoxColumn 在客户端验证 (Java 脚本).


Yii CCheckBoxColumn validation in client side (Java Script)

嗨,我在CGridView中使用CCheckBoxColumn。我想在提交表单之前至少选中一个复选框值。如何为此编写规则。

以下是代码。查看.php

    'class' => 'CCheckBoxColumn', 'selectableRows' => 2,
    'checkBoxHtmlOptions' => array(
        'name' => 'custids[]',
        'value'=>'CHtml::checkBox("cid[]",null,array("value"=>$data->customer_id,
        "id"=>"cid_".$data-customer_id
     ))',
     'type'=>'raw',
  ),

在模数中

public function rules()  {   // NOTE: you should only define rules for
those attributes that   // will receive user inputs. return array( ...
....  array('custids', 'CheckSelected' ), ... }

public function CheckSelected($attribute,$params ) {
    if(count($this->custids) == 0)
           $this->addError($attribute,'Please select the cust ids'); }

如何在客户端验证此复选框 []。

我已经尝试了验证或限制在 Ccheckbox 列链接中选择的复选框数量中提到的示例。

您可以在验证客户端选项之后添加此检查

$form = $this->beginWidget(
         'CActiveForm',
         array('id' => 'add_category_form',
             'enableAjaxValidation' => true, 
             'clientOptions' => array(
             'validateOnSubmit' => true, 
                 'ajaxVar' => 'ajax', 
                 'afterValidate' => "js: function(form, data, hasError) {
                    ///check it there
                    return false;
                }
                "
             )
         )
    );

我没有看到您的活动表单的代码,但是您始终可以在客户端选中此内容,仅在选中至少选中一个复选框的情况下仅允许表单提交,

您可以在客户端(javasctipt)中检索选中的列:

 var idArray = $(gridID).yiiGridView('getChecked', columnID);
 // or
 $.fn.yiiGridView.getSelection(gridID);

现在,如果这是一个空数组,您可以停止。