【问题标题】:Yii CCheckBoxColumn validation in client side (Java Script)Yii CCheckBoxColumn 在客户端验证(Java Script)
【发布时间】:2014-04-05 18:03:48
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript php validation yii


    【解决方案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;
                    }
                    "
                 )
             )
        );
    

    【讨论】:

      【解决方案2】:

      我没有看到您的活动表单的代码,但您始终可以在客户端检查这一点,只有在至少有一个复选框被选中时才让表单提交,

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

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

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

      【讨论】:

      • 这是javascript,在发送表单数据之前使用它
      • 您能否提供有关在何处以及如何使用此 javascript 的示例代码?
      猜你喜欢
      • 2015-03-06
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 2015-05-31
      • 2018-06-12
      相关资源
      最近更新 更多