简体   繁体   中英

Collecting CActiveForm Errors On Server-Side

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 .

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.

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