Yii uses AJAX validation to display error MessageBox solutions, yiimessagebox_php Tutorial

Source: Internet
Author: User

Yii uses AJAX validation to display error MessageBox solutions, Yiimessagebox


The example in this article describes how Yii uses AJAX validation to display error MessageBox solutions. Share to everyone for your reference. Here's how:

Yii comes with Ajax form verification This may be some friends do not know, but I am today in the use of Yii comes with the Ajax form validation encountered some problems, let me take a look at the example and you reference.

In Yii, you can use Ajax to execute an action, but this action sometimes has the need to eject the error message, which is handled as follows

Basic ideas

Using exception, for example:
Copy the code as follows: throw new Chttpexception (403, ' You is not authorized to perform this action. ');

If the exception is Chttpexception or Yii_debug is true, the error message can be displayed by Cerrorhandler::erroraction. In the YIIC default generated code, it is done by adding the following code to the config/main.php
Copy the Code Code as follows: ' ErrorHandler ' = = Array (
' ErrorAction ' = ' site/error ',),

However, in Yii 1.1.9, the AJAX request exceptions is displayed by CApplication::d isplayexception (). This makes it impossible to customize the way messages are displayed.

Cgridview Delete request throws an exception that's what it looks like, (Yii_debug is True)

Yii 1.1.9 The logic of checking for AJAX requests is removed, so even the exception of Ajax is handled by Cerrorhandler::erroraction now.

So the AJAX message can be DIY.

Example

By following the code
Copy the Code Code 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 found a webmaster shared a piece of code

Model:
Copy code code as follows: Public function rules ()
{
//note:you should only Define rules for those attributes that
//would receive user inputs.
Return Array (
Array (' content, author, emai L ', ' required '),
Array (' Author, email, url ', ' Length ', ' Max ' =>128),
Array (' email ', ' email '),
Array (' URL ') , ' url '),
);
}
Controller:
Copy code code as follows: if (Isset ($_post[' ajax ') ') && $_post [' Ajax ']=== ' Comment-form ')
{
echo cactiveform::validate ($model);
Yii::app ()->end ();
}
view:
Copy code code as follows: <?php $form = $this->beginwidget (' Cactiveform ', array (
' id ' = ' post-form ',//This is the form ID
' enableajaxvalidation ' =>true,//must write 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));
?>

Markdown syntax.


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


<?php
$this->endwidget ();
?>

This seems to be a good solution to the Yii Ajax display problem.

It is hoped that this article is helpful to the PHP program design based on YII framework.

http://www.bkjia.com/PHPjc/920974.html www.bkjia.com true http://www.bkjia.com/PHPjc/920974.html techarticle Yii uses Ajax to verify the solution that displays the error MessageBox, Yiimessagebox This example illustrates how Yii uses AJAX validation to display error MessageBox solutions. Share to everyone for your reference ...

  • Related Article

    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.