mirror of
https://gitlab.easter-eggs.com/ee/ldapsaisie.git
synced 2024-12-18 22:43:47 +01:00
LSformRule: add validate_values() method and use it to value rules
Also add validate_one_by_one class constant to allow to handle validation on all values together instead of one-by-one.
This commit is contained in:
parent
089693ea0e
commit
114e3c48ac
2 changed files with 60 additions and 22 deletions
|
@ -381,23 +381,25 @@ class LSform extends LSlog_staticLoggerClass {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($values as $value) {
|
// If no rule configured for this attribute, just ignore this check
|
||||||
if (empty($value)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!isset($this -> _rules[$element]) || !is_array($this -> _rules[$element]))
|
if (!isset($this -> _rules[$element]) || !is_array($this -> _rules[$element]))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
// Load LSformRule class
|
||||||
LSsession :: loadLSclass('LSformRule', null, true);
|
LSsession :: loadLSclass('LSformRule', null, true);
|
||||||
|
|
||||||
|
// Iter on rules and check element values with each of them
|
||||||
foreach($this -> _rules[$element] as $rule) {
|
foreach($this -> _rules[$element] as $rule) {
|
||||||
$ruleType="LSformRule_".$rule['name'];
|
if (
|
||||||
LSsession :: loadLSclass($ruleType);
|
!LSformRule :: validate_values(
|
||||||
if (! call_user_func_array(array( $ruleType,'validate') , array($value, $rule['options'], &$this -> elements[$element]))) {
|
$rule['name'], $values, $rule['options'], $this -> elements[$element]
|
||||||
|
)
|
||||||
|
) {
|
||||||
$retval = false;
|
$retval = false;
|
||||||
$this -> setElementError($this -> elements[$element], $rule['options']['msg']);
|
$this -> setElementError($this -> elements[$element], $rule['options']['msg']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return $retval;
|
return $retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,17 +29,50 @@ LSsession :: loadLSclass('LSlog_staticLoggerClass');
|
||||||
*/
|
*/
|
||||||
class LSformRule extends LSlog_staticLoggerClass {
|
class LSformRule extends LSlog_staticLoggerClass {
|
||||||
|
|
||||||
|
// Validate values one by one or all together
|
||||||
|
public const validate_one_by_one = True;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validation de données
|
* Validate form element values with specified rule
|
||||||
*
|
*
|
||||||
* @param mixed $value Données à valider
|
* @param mixed $rule_name The LSformRule name
|
||||||
* @param array $options Options de validation
|
* @param mixed $value The values to validate
|
||||||
* @param object $formElement L'objet formElement attaché
|
* @param array $options Validation options
|
||||||
|
* @param object $formElement The attached LSformElement object
|
||||||
*
|
*
|
||||||
* @return boolean True si les données sont valide, False sinon.
|
* @return boolean True if value is valid, False otherwise
|
||||||
|
*/
|
||||||
|
public static function validate_values($rule_name, $values, $options=array(), &$formElement) {
|
||||||
|
// Compute PHP class name of the rule
|
||||||
|
$rule_class = "LSformRule_".$rule_name;
|
||||||
|
|
||||||
|
// Load PHP class (with error if fail)
|
||||||
|
if (!LSsession :: loadLSclass($rule_class)) {
|
||||||
|
LSerror :: addErrorCode('LSformRule_02', $rule_name);
|
||||||
|
return False;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $rule_class :: validate_one_by_one)
|
||||||
|
return $rule_class :: validate($values, $options, $formElement);
|
||||||
|
|
||||||
|
foreach ($values as $value) {
|
||||||
|
if (!$rule_class :: validate($value, $options, $formElement))
|
||||||
|
return False;
|
||||||
|
}
|
||||||
|
return True;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate form element value
|
||||||
|
*
|
||||||
|
* @param mixed $value The value to validate
|
||||||
|
* @param array $options Validation options
|
||||||
|
* @param object $formElement The attached LSformElement object
|
||||||
|
*
|
||||||
|
* @return boolean True if value is valid, False otherwise
|
||||||
*/
|
*/
|
||||||
public static function validate($value, $options=array(), &$formElement) {
|
public static function validate($value, $options=array(), &$formElement) {
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -50,3 +83,6 @@ class LSformRule extends LSlog_staticLoggerClass {
|
||||||
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