PHP开发实例大全(提高卷) 中文完整pdf扫描版[244MB]
模型user.php
public $old_password; public $new_password; public $repeat_password; //Define the rules for old_password, new_password and repeat_password with changePwd Scenario. public function rules() { return array( array('old_password, new_password, repeat_password', 'required', 'on' => 'changePwd'), array('old_password', 'findPasswords', 'on' => 'changePwd'), array('repeat_password', 'compare', 'compareAttribute'=>'new_password', 'on'=>'changePwd'), ); } //matching the old password with your existing password. public function findPasswords($attribute, $params) { $user = User::model()->findByPk(Yii::app()->user->id); if ($user->password != md5($this->old_password)) $this->addError($attribute, 'Old password is incorrect.'); } 控制器 public function actionChangepassword($id) { $model = new User; $model = User::model()->findByAttributes(array('id'=>$id)); $model->setScenario('changePwd'); if(isset($_POST['User'])){ $model->attributes = $_POST['User']; $valid = $model->validate(); if($valid){ $model->password = md5($model->new_password); if($model->save()) $this->redirect(array('changepassword','msg'=>'successfully changed password')); else $this->redirect(array('changepassword','msg'=>'password not changed')); } } $this->render('changepassword',array('model'=>$model)); } 模板文件 (changepassword.php) <h2>修改密码</h2> <div class="form" style="margin-left:50px;margin-top:20px;"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'account-form', 'enableClientValidation'=>true, 'enableAjaxValidation'=>false, )); <?php //echo $form->errorSummary($model); <div class="row"> <?php echo $form->labelEx($model,'old_password'); <?php echo $form->passwordField($model,'old_password',array('size'=>60,'maxlength'=>128,'class'=>'pwd_pro_input')); <?php echo $form->error($model,'old_password'); </div> <div class="row"> <?php echo $form->labelEx($model,'new_password'); <?php echo $form->passwordField($model,'new_password',array('size'=>60,'maxlength'=>128,'class'=>'pwd_pro_input')); <?php echo $form->error($model,'new_password'); </div> <div class="row"> <?php echo $form->labelEx($model,'new_password'); <?php echo $form->passwordField($model,'repeat_password',array('size'=>60,'maxlength'=>128,'class'=>'pwd_pro_input')); <?php echo $form->error($model,'repeat_password'); </div> <div class="row buttons"> <?php echo CHtml::submitButton('修改密码',array('class'=>'btn_reset')); </div> <?php $this->endWidget();