Yii ajaxSubmitButton客户端验证问题

时间:2014-01-18 06:39:21

标签: javascript php jquery ajax yii

您可能知道,Yii不使用CHtml :: ajaxSubmitButton执行客户端验证。因此,经过长时间的谷歌和stackoverflow花费,我发现我应该使用以下链接

a-simple-way-to-get-yii-client-side-form-validation-run-when-submitting-by-ajax

现在,它可以执行客户端验证。但是,成功完整 ajax函数在验证后不会触发。

任何想法?

为了更熟悉主题,我把代码放在这里:

Yii::app()->clientScript->registerCoreScript('yii');

Yii::app()->clientScript->registerScript('Yii Fix',";$.yii.fix = {
        ajaxSubmit : {  
            beforeSend : function(form) {
                return function(xhr,opt) {
                    form = $(form);
                    $._data(form[0], 'events').submit[0].handler();
                    var he = form.data('hasError');
                    form.removeData('hasError');
                    return he===false;
                }
            },

            afterValidate : function(form, data, hasError) {
                $(form).data('hasError', hasError);
                return true;
            }
        }
    };",CclientScript::POS_HEAD);

我的表格:

$uniqid = uniqid();

$form = $this->beginWidget('CActiveForm', array(
'id' => 'pagescontents-add-new-contents'.$uniqid,
'htmlOptions' => array(
    'class' => 'form-horizontal',
    'role' => 'form'
),
'enableClientValidation' => true,
'enableAjaxValidation'=>true,
'clientOptions' => array(
    'validateOnSubmit' => true,
    'validateOnChange'=>false,
    'afterValidate'=>'js:$.yii.fix.ajaxSubmit.afterValidate',
),
));
//The rest of code
...
//Ajax submit button

$button_uniqid = uniqid();
echo CHtml::ajaxSubmitButton(Messages::getMessage('ADD_NEW'), $this->createUrl('pagescontents/addPage/'), array(
            'beforeSend' => '$.yii.fix.ajaxSubmit.beforeSend("#pagescontents-add-new-contents'.$uniqid.'")',
            'type'=>'POST',
            'success' => "js:function(data){
                            $('#page_modal').html(''); 
                            $('.modal-backdrop').remove(); 
                            $('#pageContents_list').html(data);
                        }", 

        ),
        array(
            'class' => 'btn btn-primary',
//          'data-dismiss' => 'modal',
            'id' => 'deletesubmit_' . $button_uniqid
            )
        );

1 个答案:

答案 0 :(得分:1)

我尝试使用您的代码并发现如果您删除“'enableAjaxValidation'=> true”这一行,您的代码将完美运行,请检查并让我知道这对您有用。