有关ajax验证返回值问题 [ 新手入门 ]
我在更新密码操作时,用的是ajax验证,在提交的时候会根据rules里的规则进行服务器端的验证,但是yii都会把不符合验证规则的错误信息,返回到view中,不能返回更新成功的值,比如说:更新成功后,我想在view里让它显示,“密码更新成功”。成功的返回值,我在controller里该怎么写呢?下面是代码
controller: // 更新个人密码
public function actionUpdatePassword()
{
$model = new SecurityUsers;
$model->setScenario('UpdatePassword');
if (!empty($_POST['SecurityUsers']))
{
$this->performAjaxValidation($model, $_POST['SecurityUsers']);
$newPassword = $_POST['SecurityUsers']['newPassword'];
$updateResult = $model->updateByPk(Yii::app()->user->id,array('password'=>$model->encrypt($newPassword)));
}
$this->render('updatePassword', array('model'=>$model));
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation($model, $attributeArr)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='update-password-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
view:
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'update-password-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<div class="row">
<?php echo $form->labelEx($model,'newPassword'); ?>
<?php echo $form->passwordField($model,'newPassword',array('size'=>20,'maxlength'=>50, 'style'=>'margin-left:13px;')); ?>
<?php echo $form->error($model,'newPassword'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'confirmPassword'); ?>
<?php echo $form->passwordField($model,'confirmPassword',array('size'=>20,'maxlength'=>50)); ?>
<?php echo $form->error($model,'confirmPassword'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('修改密码'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
共 6 条回复
-
1648053976 回复于 2012-01-17 09:48 举报
这办法真不错
YiiHoney 昆仑万维
注册时间:2011-09-23
最后登录:1970-01-01
在线时长:0小时0分
最后登录:1970-01-01
在线时长:0小时0分
- 粉丝0
- 金钱275
- 威望0
- 积分275