1

如果我有一个输入字段,例如:

  • 用户邮箱:
  • 用户电话号码:
  • 用户旧密码:
  • 用户新密码:
  • 用户重新输入密码:

在我的操作更新中,如果 UserOldPassword 不为空,我将只需要 UserNewPassword,否则唯一需要更新的是 UserEmail 或 UserPhoneNumber。我将如何实施此规则?仅在更新时?或者我应该在我的模型上创建一个自定义验证器?

所以在我的 afterFind() 上,我有这个代码来避免输出散列密码:

public function afterFind()
{
    //reset the password to null because we don't want the hash to be shown.
    $this->old_password = $this->password;
    $this->password = null;

    parent::afterFind();
}

我创建了一个自定义验证。它确实验证了,问题是即使我提交了带有空 UserNewPassword 的表单我仍然收到错误,并且 $this->password 字段现在从我的数据库返回一个散列值

请查看我的代码并纠正我的错误:

public function checkForEmpty($attribute,$params){
        if(!empty($this->$attribute)){
            if(empty($params['oldPassword'])){
                $this->addError('oldPassword','We require your old password to proceed changing password');
            }
            if(empty($params['retypePassword'])){
                $this->addError('retype_password','To assure that you type the right password please retype your password');
            }
            if(!empty($params['oldPassword']) && !empty($params['retypePassword'])){
                $this->compareOldPass($params['oldPassword'],$this->old_password);
            }
        }

    }

提前致谢...

4

1 回答 1

1

您可以在模型中执行 beforeSave()。我认为这是最好的解决方案,因为您的逻辑复杂,而不是您的应用程序的全球问题。

更新:

public function beforeSave()
{
    if(parent::beforeSave())
    {
        //implement you logic here

        //or check it is a new record
        if($this->isNewRecord)
        { 
            // do stuff here if new 
        }

        //or you can return false if it doesn't meet your logic


        return true;
    }
    else
        return false;
}
于 2013-09-25T13:15:27.250 回答