iThink
iThink

Reputation: 1259

validation rule in yii

The following is the validation rule

public function rules()
    {
        return array(
            // username and password are required
            array('oldPassword', 'required'),
            array('oldPassword', 'authenticate'),
                    ....
        );
    }
public function authenticate($attribute,$params)
{
    $this->userModel=User::model()->findByPk(Yii::app()->user->id);
    if($this->userModel!=null){
      if(!$this->userModel->validatePassword($this->oldPassword))
        $this->addError($attribute, "Incorrect current password");
    }
}

Everything works fine but the problem lies here... when I keep the oldPassword blank both the validation error for "required" & "authentication' are shown whereas I want to show the error msg for the first one,if not blank then the later.

Upvotes: 1

Views: 1079

Answers (1)

John Watson
John Watson

Reputation: 2573

Add a condition in authenticate() to only validate if oldPassword is not empty:

 public function authenticate($attribute, $params) {
      if ($this->oldPassword) {
          ...
      }
 }

Upvotes: 2

Related Questions