Question

What If I have an input field for example:

  • UserEmail:
  • UserPhoneNumber:
  • UserOldPasword:
  • UserNewPassword:
  • UserRetypePassword:

And on my action update I will only require UserNewPassword if UserOldPassword is not empty else the only thing that needs to be updated is either UserEmail or UserPhoneNumber. How will I implement this rule?only on update? Or should I create a custom validator on my model?

so on my afterFind() I have this code to avoid the output of the hashed password:

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

and I created a custom validation. it does validates, the problem is even if I submitted the form with empty UserNewPassword I still get an error and $this->password field is now returning a hashed value from my database

Please see my code and correct my mistakes:

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

    }

Thanks in advance...

Was it helpful?

Solution

you can do a beforeSave() in your model. I think it's the best solution because of your complicated logic, and not being a global issue of your app.

UPDATE:

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top