From 0af81bf0a39a8cadf8bfd83c47abc843c35e69e5 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 12 Jul 2021 18:59:34 +0200 Subject: [PATCH] Improve LSformRules to throw LSformRuleException with error details --- .../class/class.LSformRule_filesize.php | 4 ++-- .../class/class.LSformRule_mimetype.php | 4 ++-- .../class/class.LSformRule_password.php | 22 ++++++++++++------- .../class/class.LSformRule_rangelength.php | 10 ++++++++- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/includes/class/class.LSformRule_filesize.php b/src/includes/class/class.LSformRule_filesize.php index f3980cf3..a610dade 100644 --- a/src/includes/class/class.LSformRule_filesize.php +++ b/src/includes/class/class.LSformRule_filesize.php @@ -46,11 +46,11 @@ class LSformRule_filesize extends LSformRule { $maxSize = LSconfig :: get('params.maxSize', null, 'int', $options); if (is_int($maxSize) && $size > $maxSize) - return; + throw new LSformRuleException(_('File is too big.')); $minSize = LSconfig :: get('params.minSize', null, 'int', $options); if (is_int($minSize) && $size < $minSize) - return; + throw new LSformRuleException(_('File is too light.')); return true; } diff --git a/src/includes/class/class.LSformRule_mimetype.php b/src/includes/class/class.LSformRule_mimetype.php index 9daaadbc..61e8a376 100644 --- a/src/includes/class/class.LSformRule_mimetype.php +++ b/src/includes/class/class.LSformRule_mimetype.php @@ -44,11 +44,11 @@ class LSformRule_mimetype extends LSformRule { $mimetypes = ensureIsArray(LSconfig :: get('params.mimeType', null, null, $options)); if ($mimetypes && !in_array($real_mimetype, $mimetypes)) - return false; + throw new LSformRuleException(getFData(_('Invalid file type (%{type}).'), $real_mimetype)); $mimeTypeRegEx = LSconfig :: get('params.mimeTypeRegEx', null, 'string', $options); if (is_string($mimeTypeRegEx) && !preg_match($mimeTypeRegEx, $real_mimetype)) - return false; + throw new LSformRuleException(getFData(_('Invalid file type (%{type}).'), $real_mimetype)); return true; } diff --git a/src/includes/class/class.LSformRule_password.php b/src/includes/class/class.LSformRule_password.php index 734fc4e5..b5d185a4 100644 --- a/src/includes/class/class.LSformRule_password.php +++ b/src/includes/class/class.LSformRule_password.php @@ -44,16 +44,16 @@ class LSformRule_password extends LSformRule { * @return boolean true si la valeur est valide, false sinon */ public static function validate($value, $options=array(), &$formElement) { + $errors = array(); + $maxLength = LSconfig :: get('params.maxLength', null, 'int', $options); if(!is_null($maxLength) && $maxLength != 0 && strlen($value) > $maxLength) { - self :: log_debug("password is too long (".strlen($value)." > $maxLength)"); - return; + $errors[] = getFData(_("Password is too long (maximum: %{maxLength})."), $maxLength); } $minLength = LSconfig :: get('params.minLength', null, 'int', $options); if(!is_null($minLength) && $minLength != 0 && strlen($value) < $minLength) { - self :: log_debug("password is too short (".strlen($value)." < $minLength)"); - return; + $errors[] = getFData(_("Password is too short (minimum: %{minLength})."), $minLength); } $regex = ensureIsArray(LSconfig :: get('params.regex', null, null, $options)); @@ -77,17 +77,23 @@ class LSformRule_password extends LSformRule { self :: log_debug("password does not match with regex '$r'"); } if ($valid < $minValidRegex) { - self :: log_warning("password match with only $valid regex on ".count($regex).". $minValidRegex valid regex is required"); - return; + $errors[] = getFData( + _("Password match with only %{valid} rule(s) (at least %{minValidRegex} are required)."), + array( + 'valid' => $valid, + 'minValidRegex' => $minValidRegex + ) + ); } } $prohibitedValues = ensureIsArray(LSconfig :: get('params.prohibitedValues', null, null, $options)); if(in_array($value, $prohibitedValues)) { - self :: log_debug("this password is prohibited"); - return; + $errors[] = _("This password is prohibited."); } + if ($errors) + throw new LSformRuleException($errors); return true; } diff --git a/src/includes/class/class.LSformRule_rangelength.php b/src/includes/class/class.LSformRule_rangelength.php index f6615d6e..c109faa5 100644 --- a/src/includes/class/class.LSformRule_rangelength.php +++ b/src/includes/class/class.LSformRule_rangelength.php @@ -45,7 +45,15 @@ class LSformRule_rangelength extends LSformRule { return; } $len = strlen($value); - return ($len >= $limits[0] && $len <= $limits[1]); + if ($len < $limits[0]) + throw new LSformRuleException( + getFData(_('Value is too short (minimum: %{limit}).'), $limits[0]) + ); + if ($len > $limits[1]) + throw new LSformRuleException( + getFData(_('Value is too long (maximum: %{limit}).'), $limits[1]) + ); + return True; } }