Yii how to use ajax to verify display of error messagebox _ php instance

Source: Internet
Author: User
This article describes how to use ajax to verify the display of error messagebox in Yii. you can set the Ajax prompt information by yourself, which is a very practical technique, for more information about how Yii uses ajax to verify the error message box, see the example in this article. Share it with you for your reference. The specific method is as follows:

Some friends may not know about yii's built-in ajax form verification, but I encountered some problems when using yii's self-contained ajax form verification today. I will sort out the examples below for your reference.

In Yii, you can use ajax to execute an action, but this action sometimes requires an error message to pop up. the processing method is as follows:

Basic ideas

Use exception, for example:

The code is as follows:

Throw new CHttpException (403, 'You are not authorized to perform this action .');

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

The code is as follows:

'Errorhandler' => array (
'Erroraction' => 'site/error ',),

However, in Yii 1.1.9 and later versions, the exceptions thrown by ajax requests are displayed through CApplication: displayException. This makes it impossible to customize the message display mode.

This is the case if the CGridView deletion request throws an exception (YII_DEBUG is true)

Yii 1.1.9 checks that the logic of the ajax request has been removed, so even ajax exceptions are now handled by CErrorHandler: errorAction.

In this way, ajax messages can be DIY.

Example

Use the following 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 sharing a piece of code.

Model:

The code is as follows:

Public function rules ()
{
// NOTE: you shocould 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:

The code is as follows:

If (isset ($ _ POST ['Ajax ']) & $ _ POST ['Ajax'] === 'Comment-form ')
{
Echo CActiveForm: validate ($ model );
Yii: app ()-> end ();
}


View:

The code is as follows:

<? Php $ form = $ this-> beginWidget ('cactiveform', array (
'Id' => 'Post-form', // This is the form id
'Enablesajaxvalidation '=> true, // enter true here.
);?>
<? Php echo CHtml: errorSummary ($ model);?>


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


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

You may use Markdown syntax.


<? Php
Echo $ form-> error ($ model, 'Content ');
?>



<? Php
$ This-> endWidget ();
?>

It seems that yii ajax display is well solved.

I hope this article will help you design PHP programs based on the Yii Framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.