YiiHoney 2011-10-21 12:16:26 5052次浏览 6条回复 1 0 0

我在更新密码操作时,用的是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 -->
您需要登录后才可以回复。登录 | 立即注册