客户端的Yii CCheckBoxColumn验证(Java Script)

时间:2014-04-05 18:03:48

标签: javascript php validation yii

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

以下是代码。 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',
  ),

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

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

我尝试了 Validate Or Limit Number Of Checkboxes Selected In Ccheckboxcolumn链接中提到的示例。

2 个答案:

答案 0 :(得分:1)

您可以在afterValidate客户端选项

中添加此签入
$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;
                }
                "
             )
         )
    );

答案 1 :(得分:0)

我没有看到您的活动表单的代码,但是如果至少选中了其中一​​个复选框,则只需在表单提交时就可以在客户端进行检查,

你可以retrieve the checked column in client side (javasctipt)

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

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

相关问题