【问题标题】:Validation Rules are not working for change password in Yii php?验证规则不适用于 Yii php 中的更改密码?
【发布时间】:2013-07-24 22:31:50
【问题描述】:

我正在创建更改密码功能,除了旧的密码验证规则外,一切都正常。这是我的代码

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 的新手。 提前致谢。

【问题讨论】:

    标签: php validation yii passwords


    【解决方案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
    }
    

    【讨论】:

      【解决方案2】:

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

      $value = $this->$attribute;
      

      【讨论】:

      • 好吧,然后做一些调试:在你的equalPasswords() 方法中添加一个echo $user->password; echo $this->$attribute;exit;。这应该可以帮助您找出问题所在。
      • 现在验证工作正常,当我评论更新方法时,它的工作但当我取消注释方法 $data = $userModel->update(); 时,它抛出了同样的错误,我用更新函数更新了我的问题。
      • 好的,但现在这是一个不同的问题。您最初验证失败的问题已解决。您在控制器中的代码实际上并不是那么好。您应该先加载用户模型,然后在那里分配属性。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      相关资源
      最近更新 更多