Improve LSformRules to throw LSformRuleException with error details

This commit is contained in:
Benjamin Renard 2021-07-12 18:59:34 +02:00
parent 9315acc545
commit 0af81bf0a3
4 changed files with 27 additions and 13 deletions

View file

@ -46,11 +46,11 @@ class LSformRule_filesize extends LSformRule {
$maxSize = LSconfig :: get('params.maxSize', null, 'int', $options); $maxSize = LSconfig :: get('params.maxSize', null, 'int', $options);
if (is_int($maxSize) && $size > $maxSize) if (is_int($maxSize) && $size > $maxSize)
return; throw new LSformRuleException(_('File is too big.'));
$minSize = LSconfig :: get('params.minSize', null, 'int', $options); $minSize = LSconfig :: get('params.minSize', null, 'int', $options);
if (is_int($minSize) && $size < $minSize) if (is_int($minSize) && $size < $minSize)
return; throw new LSformRuleException(_('File is too light.'));
return true; return true;
} }

View file

@ -44,11 +44,11 @@ class LSformRule_mimetype extends LSformRule {
$mimetypes = ensureIsArray(LSconfig :: get('params.mimeType', null, null, $options)); $mimetypes = ensureIsArray(LSconfig :: get('params.mimeType', null, null, $options));
if ($mimetypes && !in_array($real_mimetype, $mimetypes)) 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); $mimeTypeRegEx = LSconfig :: get('params.mimeTypeRegEx', null, 'string', $options);
if (is_string($mimeTypeRegEx) && !preg_match($mimeTypeRegEx, $real_mimetype)) if (is_string($mimeTypeRegEx) && !preg_match($mimeTypeRegEx, $real_mimetype))
return false; throw new LSformRuleException(getFData(_('Invalid file type (%{type}).'), $real_mimetype));
return true; return true;
} }

View file

@ -44,16 +44,16 @@ class LSformRule_password extends LSformRule {
* @return boolean true si la valeur est valide, false sinon * @return boolean true si la valeur est valide, false sinon
*/ */
public static function validate($value, $options=array(), &$formElement) { public static function validate($value, $options=array(), &$formElement) {
$errors = array();
$maxLength = LSconfig :: get('params.maxLength', null, 'int', $options); $maxLength = LSconfig :: get('params.maxLength', null, 'int', $options);
if(!is_null($maxLength) && $maxLength != 0 && strlen($value) > $maxLength) { if(!is_null($maxLength) && $maxLength != 0 && strlen($value) > $maxLength) {
self :: log_debug("password is too long (".strlen($value)." > $maxLength)"); $errors[] = getFData(_("Password is too long (maximum: %{maxLength})."), $maxLength);
return;
} }
$minLength = LSconfig :: get('params.minLength', null, 'int', $options); $minLength = LSconfig :: get('params.minLength', null, 'int', $options);
if(!is_null($minLength) && $minLength != 0 && strlen($value) < $minLength) { if(!is_null($minLength) && $minLength != 0 && strlen($value) < $minLength) {
self :: log_debug("password is too short (".strlen($value)." < $minLength)"); $errors[] = getFData(_("Password is too short (minimum: %{minLength})."), $minLength);
return;
} }
$regex = ensureIsArray(LSconfig :: get('params.regex', null, null, $options)); $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'"); self :: log_debug("password does not match with regex '$r'");
} }
if ($valid < $minValidRegex) { if ($valid < $minValidRegex) {
self :: log_warning("password match with only $valid regex on ".count($regex).". $minValidRegex valid regex is required"); $errors[] = getFData(
return; _("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)); $prohibitedValues = ensureIsArray(LSconfig :: get('params.prohibitedValues', null, null, $options));
if(in_array($value, $prohibitedValues)) { if(in_array($value, $prohibitedValues)) {
self :: log_debug("this password is prohibited"); $errors[] = _("This password is prohibited.");
return;
} }
if ($errors)
throw new LSformRuleException($errors);
return true; return true;
} }

View file

@ -45,7 +45,15 @@ class LSformRule_rangelength extends LSformRule {
return; return;
} }
$len = strlen($value); $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;
} }
} }