mirror of
https://gitlab.easter-eggs.com/ee/ldapsaisie.git
synced 2024-11-18 00:09: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>
|
<varlistentry>
|
||||||
<term>msg</term>
|
<term>msg</term>
|
||||||
<listitem>
|
<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>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
|
|
|
@ -395,13 +395,13 @@ class LSform extends LSlog_staticLoggerClass {
|
||||||
|
|
||||||
// Iter on rules and check element values with each of them
|
// Iter on rules and check element values with each of them
|
||||||
foreach($this -> _rules[$element] as $rule) {
|
foreach($this -> _rules[$element] as $rule) {
|
||||||
if (
|
$errors = LSformRule :: validate_values(
|
||||||
!LSformRule :: validate_values(
|
|
||||||
$rule['name'], $values, $rule['options'], $this -> elements[$element]
|
$rule['name'], $values, $rule['options'], $this -> elements[$element]
|
||||||
)
|
);
|
||||||
) {
|
if (is_array($errors)) {
|
||||||
$retval = false;
|
$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)
|
// Load PHP class (with error if fail)
|
||||||
if (!LSsession :: loadLSclass($rule_class)) {
|
if (!LSsession :: loadLSclass($rule_class)) {
|
||||||
LSerror :: addErrorCode('LSformRule_02', $rule_name);
|
return array(
|
||||||
return False;
|
getFData(_('Invalid syntax checking configuration: unknown rule %{rule}.'), $rule_name)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $rule_class :: validate_one_by_one)
|
$errors = false;
|
||||||
return $rule_class :: validate($values, $options, $formElement);
|
try {
|
||||||
|
if (! $rule_class :: validate_one_by_one) {
|
||||||
|
if (!$rule_class :: validate($values, $options, $formElement))
|
||||||
|
throw new LSformRuleException();
|
||||||
|
}
|
||||||
|
else {
|
||||||
foreach ($values as $value) {
|
foreach ($values as $value) {
|
||||||
if (!$rule_class :: validate($value, $options, $formElement))
|
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
|
* Error Codes
|
||||||
**/
|
**/
|
||||||
LSerror :: defineError('LSformRule_01',
|
LSerror :: defineError('LSformRule_01',
|
||||||
___("LSformRule_%{type}: Parameter %{param} is not found.")
|
___("LSformRule_%{type}: Parameter %{param} is not found.")
|
||||||
);
|
);
|
||||||
LSerror :: defineError('LSformRule_02',
|
|
||||||
___("LSformRule: Unknown rule type %{type}.")
|
|
||||||
);
|
|
||||||
|
|
Loading…
Reference in a new issue