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);
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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}
}