/** * Validate this field * * @param Validator $validator * @return bool */ public function validate($validator) { $name = $this->name; // if field isn't visible, don't validate if (!$this->isSaveable()) { return true; } $this->passwordField->setValue($this->value); $this->confirmPasswordfield->setValue($this->confirmValue); $value = $this->passwordField->Value(); // both password-fields should be the same if ($value != $this->confirmPasswordfield->Value()) { $validator->validationError($name, _t('Form.VALIDATIONPASSWORDSDONTMATCH', "Passwords don't match"), "validation"); return false; } if (!$this->canBeEmpty) { // both password-fields shouldn't be empty if (!$value || !$this->confirmPasswordfield->Value()) { $validator->validationError($name, _t('Form.VALIDATIONPASSWORDSNOTEMPTY', "Passwords can't be empty"), "validation"); return false; } } // lengths if ($this->minLength || $this->maxLength) { $errorMsg = null; $limit = null; if ($this->minLength && $this->maxLength) { $limit = "{{$this->minLength},{$this->maxLength}}"; $errorMsg = _t('ConfirmedPasswordField.BETWEEN', 'Passwords must be {min} to {max} characters long.', array('min' => $this->minLength, 'max' => $this->maxLength)); } elseif ($this->minLength) { $limit = "{{$this->minLength}}.*"; $errorMsg = _t('ConfirmedPasswordField.ATLEAST', 'Passwords must be at least {min} characters long.', array('min' => $this->minLength)); } elseif ($this->maxLength) { $limit = "{0,{$this->maxLength}}"; $errorMsg = _t('ConfirmedPasswordField.MAXIMUM', 'Passwords must be at most {max} characters long.', array('max' => $this->maxLength)); } $limitRegex = '/^.' . $limit . '$/'; if (!empty($value) && !preg_match($limitRegex, $value)) { $validator->validationError($name, $errorMsg, "validation"); } } if ($this->requireStrongPassword) { if (!preg_match('/^(([a-zA-Z]+\\d+)|(\\d+[a-zA-Z]+))[a-zA-Z0-9]*$/', $value)) { $validator->validationError($name, _t('Form.VALIDATIONSTRONGPASSWORD', "Passwords must have at least one digit and one alphanumeric character"), "validation"); return false; } } // Check if current password is valid if (!empty($value) && $this->getRequireExistingPassword()) { if (!$this->currentPasswordValue) { $validator->validationError($name, _t('ConfirmedPasswordField.CURRENT_PASSWORD_MISSING', "You must enter your current password."), "validation"); return false; } // Check this password is valid for the current user $member = Member::currentUser(); if (!$member) { $validator->validationError($name, _t('ConfirmedPasswordField.LOGGED_IN_ERROR', "You must be logged in to change your password."), "validation"); return false; } // With a valid user and password, check the password is correct $checkResult = $member->checkPassword($this->currentPasswordValue); if (!$checkResult->valid()) { $validator->validationError($name, _t('ConfirmedPasswordField.CURRENT_PASSWORD_ERROR', "The current password you have entered is not correct."), "validation"); return false; } } return true; }