This article describes the workaround for Yii using AJAX to verify display error MessageBox. Share to everyone for your reference. The specific methods are as follows:
Yii comes with Ajax form verification This may not be known to some friends, but today I have some problems with the Ajax form validation with Yii, and I'll take a look at the examples below.
In Yii, you can use Ajax to perform an action, but this action sometimes has the need to eject the error message, which is handled in the following way
Basic ideas
Using exception, for example:
Copy Code code as follows:
throw new Chttpexception (403, ' You are don't 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 accomplished by adding the following code to the config/main.php
Copy Code code as follows:
' ErrorHandler ' => Array (
' ErrorAction ' => ' site/error ',),
But above Yii 1.1.9, the exceptions that the Ajax request throws is displayed by CApplication::d isplayexception (). This makes it impossible for us to customize how messages are displayed.
Cgridview Delete request throws an exception that's the way it is, (Yii_debug is True)
Yii 1.1.9 The logic of checking AJAX requests is removed, so now even Ajax exceptions are handled through cerrorhandler::erroraction.
So the AJAX message can be DIY.
Example
Through the following code
Copy 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 to share a piece of code
Model
Copy Code code as follows:
Public Function Rules ()
{
Note:you should only define the rules for those attributes
would 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 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,//here must write true
));?>
<?php Echo chtml::errorsummary ($model);?>
<div class= "Row"
<?php
echo $form->labelex ($model, ' title ');
?>
<?php echo $form->textfield ($model, ' title ', Array (' Size ' =>80, ' maxlength ' =>128));
?>
<?php
echo $form->error ($model, ' Title ");
?>
</div>
<div class= "Row" >
<?php
echo $form->labelex ($model, ' content ');
?>
<?php
Echo Chtml::activetextarea ($model, ' content ', array (' Rows ' =>10, ' cols ' =>70));
?>
<p class= "hint" >you may use <a target= "_blank" href= "http://daringfireball.net/projects/markdown/syntax" >markdown syntax</a>.</p>
<?php
echo $form->error ($model, ' content ');
?>
</div>
<?php
$this->endwidget ();
?>
This seems to be a good solution to the Yii Ajax display problem.
I hope this article will help you with the PHP program design based on the YII framework.