From e18b6492503d68bdefa19c00b12c4c795dc9fcde Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Tue, 8 Mar 2022 21:23:45 +0100 Subject: [PATCH] Return array of error messages on password validate --- src/MD_STD_IN.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/MD_STD_IN.php b/src/MD_STD_IN.php index 572ba7f..e0e0c83 100644 --- a/src/MD_STD_IN.php +++ b/src/MD_STD_IN.php @@ -230,22 +230,28 @@ final class MD_STD_IN { /** * Validates a password (minimum requirements: 8 characters, including - * one number and one special char). + * one number and one special char) and returns a list of errors, + * if there are any. * * @param string $input Input string. * - * @return boolean + * @return array */ - public static function validate_password(string $input):bool { + public static function validate_password(string $input):array { + $errors = []; if (mb_strlen($input) < 8) { - return false; + $errors[] = 'password_too_short'; } - if ((\preg_match('@[0-9]@', $input)) === false) return false; - if ((\preg_match('@[^\w]@', $input)) === false) return false; + if ((\preg_match('@[0-9]@', $input)) === false) { + $errors[] = 'password_has_no_number'; + } + if ((\preg_match('@[^\w]@', $input)) === false) { + $errors[] = 'password_has_no_special_char'; + } - return true; + return $errors; }