这篇文章主要介绍了Yii使用ajax验证显示错误messagebox的解决方法,可以自行设置Ajax提示信息的方式,是非常实用的技巧,需要的朋友可以参考下
本文实例讲述了Yii使用ajax验证显示错误messagebox的解决方法。分享给大家供大家参考。具体方法如下:
yii 自带了ajax 表单验证 这个可能有些朋友不知道了,,但我今天在使用yii 自带的ajax 表单验证 时碰到一些问题,下面我来整理例子与大家参考一下。
在Yii中,可以利用ajax执行一个action,但是这个action有时候会有弹出错误讯息的需求,这时候的处理方式如下
基本思想
利用exception,比如:
复制代码 代码如下:
throw new CHttpException(403, 'You are not authorized to perform this action.');
如果这个异常是 CHttpException 或者 YII_DEBUG 为 true的时候,错误消息可以通过CErrorHandler::errorAction来显示。在yiic默认生成的代码中,就是通过在 config/main.php 中加入如下代码来实现的
复制代码 代码如下:
'errorHandler' => array(
'errorAction' => 'site/error',),
但是在Yii 1.1.9 以上,ajax请求抛出的exceptions是通过CApplication::displayException()来显示的。这使得我们无法定制消息的显示方式。
CGridView 删除请求抛出异常的话就是这个样子,(YII_DEBUG 为 true )
Yii 1.1.9 检查ajax请求的逻辑被移除了,所以现在即便是ajax的异常也是通过CErrorHandler::errorAction处理的。
这样ajax的消息就可以DIY了。
示例
通过如下代码
复制代码 代码如下:
public function actionError(){
if($error=Yii::app()->errorHandler->error)
{
if(Yii::app()->request->isAjaxRequest)
echo $error['message'];
else
$this->render('error', $error);
}
}
后来又发现一个站长分享了一段代码
model:
复制代码 代码如下:
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:
复制代码 代码如下:
if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
view:
复制代码 代码如下:
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程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment