douzhi1937 2013-04-21 08:04
浏览 24

在服务器端收集CActiveForm错误

I have a CActiveForm with this summary:

.
.
.
'id'='email-form',
'enableAjaxValidation`=>true,
'clientOptions' => array('validateOnSubmit'=>true),
.
.
.

Now i intend collect form Errors in Server-Side and sent it via json object to the client. in Client-Side there is and Jquery function that parse the json object(form Errors) and set data to errorSummary and at last shows the errorSummary of form.

i have done it without any problem, my question is what following functions don't collect form Errors:

protected function getErrorSummary($model)
{
    if(isset($_POST['ajax']) && $_POST['ajax']==='email-form'){
        $errors=CActiveForm::validate($model);
        if($errors !== '[]')
            Yii::app()->end($errors);
    }
}

But following collect form Errors:

protected function getErrorSummary($model)
{
        $errors=CActiveForm::validate($model);
        if($errors !== '[]')
            Yii::app()->end($errors);
}

notice that both functions truly acts on validateOnChange.

  • 写回答

1条回答 默认 最新

  • duangang1991 2013-04-22 07:31
    关注

    I use something like this in controller:

    if(Yii::app()->request->isAjaxRequest)
                {   
                $error=CActiveForm::validate($model);
                    if($error!='[]'){
                        echo $error; 
                        Yii::app()->end();
                    }
                }
            if(isset($_POST['Lists']))
            {
                $model->attributes=$_POST['Lists'];
                if($model->save())
                    {
                        echo CJSON::encode(array(
                                      'status'=>'success',  
                                 ));
                        Yii::app()->end();       
                    }   
            }
    

    You can use ajaxSubmitButton instead of jquery function. Something like this:

    <?php echo CHtml::ajaxSubmitButton ($model -> isNewRecord ? 'Create' : 'Save' , Yii::app()->request->url, array (
            'dataType' => 'json', 
            'type'=>'post',
            'success' =>
            'js:function (data) {
    
            if(!$.isEmptyObject(data)) {         
                $.each(data, function(key, val) {                       
                            $("#lists-form #"+key+"_em_").text(val+" ");
                            $("#lists-form #"+key+"_em_").parent(".error_wrapter").addClass("error");
                            $("#lists-form #"+key+"_em_").css(\'display\',\'block\');                                   
                                });//here you show your errors on form fields from JSON object
              };
    
            if(data.status=="success"){
            //here you can use custom notifications or redirect                           
                }
            else {
    
                //here you can display errorsummary or notifications
              };
              }',
        ), array (
            'id' => 'lists-form_submit_'.rand(1,255), // Need a unique id or they start to conflict with more than one load.
        ));?>
    

    Hope this helped.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分