2

我正在创建更改密码功能,除了旧的密码验证规则外,所有东西都运行良好。这是我的代码

public function rules()
{
    return array(
        array('is_active', 'numerical', 'integerOnly'=>true),
        array('first_name, joining_date,last_name, employee_code, username, password, role', 'required','on'=>array('create')),     
        array('employee_code', 'numerical', 'integerOnly'=>true),
        array('username','email'),      
        array('username','valid_username','on'=>array('create')),

        //array('username', 'contraints', 'readOnly'=>true, 'on'=>'update'),

        array('currentPassword, newPassword, newPasswordRepeat', 'required','on'=>array('change')),
        //array('newPassword', 'length', 'min' => 6, 'max'=>20, 'message'=>Yii::t("translation", "{attribute} is too short.")),
        //array('newPassword','ext.SPasswordValidator.SPasswordValidator', 'preset' => 'strong', 'max' => 41),
        array('newPassword', 'compare', 'compareAttribute'=>'newPasswordRepeat','on'=>array('change')),

        array('currentPassword', 'equalPasswords','on'=>array('change')),

        array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>array('forgotPassword')),
        array('joining_date', 'safe'),
        array('user_id, first_name, last_name, employee_code, username, password, role, joining_date, pending_regular_leave, pending_medical_leave, allocated_regular_leave, allocated_medical_leave, is_active', 'safe', 'on'=>'search'),
    );
}

我的更改密码功能是

public function equalPasswords($attribute, $params)
{
    $user = Users::model()->findByPk(Yii::app()->user->id); 
    if ($user->password != md5($attribute))
    {
       $this->addError($attribute, 'Old password is incorrect.');
    }   
}

更新方法

public function actionChange()
{
        $model=new Users;
        $model->setScenario('change');

    if (isset($_POST['Users'])) {
        $model->setAttributes($_POST['Users']);                     
        if($model->validate())
            {       
                $pass = md5($_POST['Users']['newPassword']);            
                $userModel = Users::model()->findByPk(Yii::app()->user->id);
                $userModel->password = $pass; 
                $data = $userModel->update();
                Yii::app()->user->setFlash('success',"Password changed successfully!");
            }
        }

$this->render('change_password', array('model'=>$model,true));
}

当我尝试使用所有正确的参数(正确的旧密码、新密码、重新输入密码)更改密码时,它会更新密码,但也会向我显示您的旧密码不正确的错误。请帮我解决这个问题,因为我是新手易。先谢谢了。

4

2 回答 2

1

我不确定,但你可以尝试一次。

$user = Users::model()->findByPk(Yii::app()->user->id); 
if ($user->password != md5($this->attributes['currentPassword']))
{
     $this->addError($attribute, 'Old password is incorrect.');
} 

更改md5($attribute)md5($this->attributes['currentPassword']

并将其添加到您的规则中

public function rules()
{
    public $currentPassword; 
    // your rules here
}
于 2013-07-25T07:28:03.090 回答
0

在验证器中,您会收到属性名称,而不是$attribute. 要获得价值,您必须:

$value = $this->$attribute;
于 2013-07-25T07:14:46.367 回答