开启 ajax 验证
view 层
<?php $form = $this->beginWidget('CActiveForm', array( 'id'=>'user-form', 'enableAjaxValidation'=>true, 'enableClientValidation'=>true, 'focus'=>array($model,'firstName'), )); ?>
然后在控制器里面
public function actionCreate() { $model=new User; $this->performAjaxValidation($model); if(isset($_POST['User'])) { $model->attributes=$_POST['User']; if($model->save()) $this->redirect('index'); } $this->render('create',array('model'=>$model)); } protected function performAjaxValidation($model) { if(isset($_POST['ajax']) && $_POST['ajax']==='user-form') { echo CActiveForm::validate($model); Yii::app()->end(); } }
另附上一个地址 讲的很详细 http://www.cnblogs.com/duanxz/p/3480254.html