简体   繁体   中英

Yii CCheckBoxColumn validation in client side (Java Script)

Hi Iam using CCheckBoxColumn in CGridView. I want to check at least one check box value should be selected before submit a form. how to write a rules for this.

Following is the code. view.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',
  ),

In Moduls

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'); }

How to validate this checkbox[] in client side.

I have tried with the examples mentioned in Validate Or Limit Number Of Checkboxes Selected In Ccheckboxcolumn links.

You can add this check in afterValidate client option

$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;
                }
                "
             )
         )
    );

I don't see the code for your active form, but you can always check this in client side by only letting the form to submit if at least one of the checkboxes is checked,

you can retrieve the checked column in client side (javasctipt) :

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

now you can stop if this is an empty array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM