验证规则不适用于Yii php中的更改密码?

时间:2013-07-25 06:58:37

标签: php validation yii passwords

我正在创建更改密码功能,除旧密码验证规则外所有工作都正常。这是我的代码

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));
}

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

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

在验证程序中,您会收到属性名称,而不是$attribute中的值。要获得您必须拥有的价值:

$value = $this->$attribute;