mirror of
https://gitlab.easter-eggs.com/ee/ldapsaisie.git
synced 2024-11-17 15:59:06 +01:00
LSformRule: add possibility to throw custom exception to provide error details
This commit is contained in:
parent
16c6d9fa6e
commit
988d744836
3 changed files with 46 additions and 22 deletions
|
@ -29,7 +29,7 @@ règles.</para>
|
|||
<varlistentry>
|
||||
<term>msg</term>
|
||||
<listitem>
|
||||
<simpara>Le message d'erreur à afficher lors que la règle n'est pas respectée.</simpara>
|
||||
<simpara>Le message d'erreur à afficher lors que la règle n'est pas respectée (optionel).</simpara>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
|
|
|
@ -395,13 +395,13 @@ class LSform extends LSlog_staticLoggerClass {
|
|||
|
||||
// Iter on rules and check element values with each of them
|
||||
foreach($this -> _rules[$element] as $rule) {
|
||||
if (
|
||||
!LSformRule :: validate_values(
|
||||
$errors = LSformRule :: validate_values(
|
||||
$rule['name'], $values, $rule['options'], $this -> elements[$element]
|
||||
)
|
||||
) {
|
||||
);
|
||||
if (is_array($errors)) {
|
||||
$retval = false;
|
||||
$this -> setElementError($this -> elements[$element], $rule['options']['msg']);
|
||||
foreach ($errors as $error)
|
||||
$this -> setElementError($this -> elements[$element], $error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,18 +48,32 @@ class LSformRule extends LSlog_staticLoggerClass {
|
|||
|
||||
// Load PHP class (with error if fail)
|
||||
if (!LSsession :: loadLSclass($rule_class)) {
|
||||
LSerror :: addErrorCode('LSformRule_02', $rule_name);
|
||||
return False;
|
||||
return array(
|
||||
getFData(_('Invalid syntax checking configuration: unknown rule %{rule}.'), $rule_name)
|
||||
);
|
||||
}
|
||||
|
||||
if (! $rule_class :: validate_one_by_one)
|
||||
return $rule_class :: validate($values, $options, $formElement);
|
||||
|
||||
$errors = false;
|
||||
try {
|
||||
if (! $rule_class :: validate_one_by_one) {
|
||||
if (!$rule_class :: validate($values, $options, $formElement))
|
||||
throw new LSformRuleException();
|
||||
}
|
||||
else {
|
||||
foreach ($values as $value) {
|
||||
if (!$rule_class :: validate($value, $options, $formElement))
|
||||
return False;
|
||||
throw new LSformRuleException();
|
||||
}
|
||||
return True;
|
||||
}
|
||||
}
|
||||
catch (LSformRuleException $e) {
|
||||
$errors = $e->errors;
|
||||
$msg = LSconfig :: get('msg', null, null, $options);
|
||||
if ($msg || !$errors) {
|
||||
$errors[] = ($msg?__($msg):_('Invalid value'));
|
||||
}
|
||||
}
|
||||
return ($errors?$errors:true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -77,12 +91,22 @@ class LSformRule extends LSlog_staticLoggerClass {
|
|||
|
||||
}
|
||||
|
||||
class LSformRuleException extends Exception {
|
||||
|
||||
public $errors = array();
|
||||
|
||||
public function __construct($errors=array(), $code = 0, Throwable $previous = null) {
|
||||
$this -> errors = ensureIsArray($errors);
|
||||
$message = _("Invalid value");
|
||||
if ($this -> errors)
|
||||
$message .= ': '.implode(", ", $this -> errors);
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Error Codes
|
||||
**/
|
||||
LSerror :: defineError('LSformRule_01',
|
||||
___("LSformRule_%{type}: Parameter %{param} is not found.")
|
||||
);
|
||||
LSerror :: defineError('LSformRule_02',
|
||||
___("LSformRule: Unknown rule type %{type}.")
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue