Home >Backend Development >PHP Tutorial >Yii uses ajax verification to display error messagebox solution, yiimessagebox_PHP tutorial

Yii uses ajax verification to display error messagebox solution, yiimessagebox_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:23776browse

Yii uses ajax verification to display error messagebox solution, yiimessagebox

The example in this article describes the solution to the error messagebox displayed in Yii using ajax verification. Share it with everyone for your reference. The specific method is as follows:

Yii comes with ajax form verification. Some friends may not know this, but I encountered some problems today when using the ajax form verification that comes with yii. Let me compile examples for your reference.

In Yii, you can use ajax to execute an action, but this action sometimes needs to pop up an error message. The handling method is as follows

Basic idea

Use exception, such as:

Copy code The code is as follows:
throw new CHttpException(403, 'You are not authorized to perform this action.');

If this exception is CHttpException or YII_DEBUG is true, the error message can be displayed through CErrorHandler::errorAction. In the code generated by yiic by default, this is achieved by adding the following code to config/main.php

Copy code The code is as follows:
'errorHandler' => array(
'errorAction' => 'site/error',),

But in Yii 1.1.9 and above, exceptions thrown by ajax requests are displayed through CApplication::displayException(). This prevents us from customizing how messages are displayed.

This is what it looks like if the CGridView deletion request throws an exception, (YII_DEBUG is true)

Yii 1.1.9 The logic for checking ajax requests has been removed, so now even ajax exceptions are handled through CErrorHandler::errorAction.

In this way, ajax messages can be DIYed.

Example

Pass the following code

Copy code The code is as follows:
public function actionError(){
If($error=Yii::app()->errorHandler->error)
{
If(Yii::app()->request->isAjaxRequest)
echo $error['message'];
else
                  $this->render('error', $error);
}
}

Later I found a webmaster who shared a piece of code

model:

Copy code The code is as follows:
public function rules()
{
// NOTE: you should only define rules for those attributes that
               // will receive user inputs.
         return array(
             array('content, author, email', 'required'),
array('author, email, url', 'length', 'max'=>128),
             array('email','email'),
             array('url','url'),
);
}

controller:
Copy code The code is as follows:
if(isset($_POST['ajax']) && $_POST['ajax']= =='comment-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}

view:
Copy code The code is as follows:
beginWidget('CActiveForm',array(
    'id'=>'post-form',                      //这是表单id
    'enableAjaxValidation'=>true,      //这里一定写 true
)); ?>
   

   


         echo $form->labelEx($model,'title');
 ?>
        textField($model,'title',array('size'=>80,'maxlength'=>128));
 ?>
         echo $form->error($model,'title');
 ?>
   

   


         echo $form->labelEx($model,'content');
 ?>
         echo CHtml::activeTextArea($model,'content',array('rows'=>10, 'cols'=>70));
 ?>
       

You may use Markdown syntax.


         echo $form->error($model,'content');
 ?>
   

 
$this->endWidget();
?>

这样好像很好的解决了yii ajax显示问题。

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/920974.htmlTechArticleYii使用ajax验证显示错误messagebox的解决方法,yiimessagebox 本文实例讲述了Yii使用ajax验证显示错误messagebox的解决方法。分享给大家供大家参考...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn