From 18cc960755bee59cd2245e3c72d99edc2e6df1cb Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 19 Jul 2021 16:53:23 +0200 Subject: [PATCH] Add LSformRule_numberOfValues --- doc/conf/LSattribute/check-data.docbook | 1 + .../LSattribute-check_data.entities.xml | 1 + .../check_data/numberOfValues.docbook | 25 ++++++ .../class/class.LSformRule_numberOfValues.php | 88 +++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 doc/conf/LSattribute/check_data/numberOfValues.docbook create mode 100644 src/includes/class/class.LSformRule_numberOfValues.php diff --git a/doc/conf/LSattribute/check-data.docbook b/doc/conf/LSattribute/check-data.docbook index 011a9b83..43583211 100644 --- a/doc/conf/LSattribute/check-data.docbook +++ b/doc/conf/LSattribute/check-data.docbook @@ -60,6 +60,7 @@ règles. &conf-LSattribute-check-data-mimetype; &conf-LSattribute-check-data-nonzero; &conf-LSattribute-check-data-nopunctuation; +&conf-LSattribute-check-data-numberOfValues; &conf-LSattribute-check-data-numeric; &conf-LSattribute-check-data-password; &conf-LSattribute-check-data-rangelength; diff --git a/doc/conf/LSattribute/check_data/LSattribute-check_data.entities.xml b/doc/conf/LSattribute/check_data/LSattribute-check_data.entities.xml index 0010bfa5..afae6232 100644 --- a/doc/conf/LSattribute/check_data/LSattribute-check_data.entities.xml +++ b/doc/conf/LSattribute/check_data/LSattribute-check_data.entities.xml @@ -15,6 +15,7 @@ + diff --git a/doc/conf/LSattribute/check_data/numberOfValues.docbook b/doc/conf/LSattribute/check_data/numberOfValues.docbook new file mode 100644 index 00000000..bd59769f --- /dev/null +++ b/doc/conf/LSattribute/check_data/numberOfValues.docbook @@ -0,0 +1,25 @@ + + numberOfValues + Cette règle vérifie que le nombre de valeurs de l'attribut est comprise entre les limites + passées en paramètre. + + +Paramètres de configuration + + + min + + Nombre minimum de valeurs (paramètre optionnel). + + + + + max + + Nombre maximum de valeurs (paramètre optionnel). + + + + + + diff --git a/src/includes/class/class.LSformRule_numberOfValues.php b/src/includes/class/class.LSformRule_numberOfValues.php new file mode 100644 index 00000000..687405e1 --- /dev/null +++ b/src/includes/class/class.LSformRule_numberOfValues.php @@ -0,0 +1,88 @@ + + */ +class LSformRule_numberOfValues extends LSformRule { + + // Validate values one by one or all together + const validate_one_by_one = False; + + /** + * Validate value + * + * @param string $values The value to validate + * @param array $options Validation options + * @param object $formElement The related formElement object + * + * @return boolean true if the value is valide, false if not + */ + public static function validate($value, $options=array(), &$formElement) { + $max_values = LSconfig :: get('params.max', null, 'int', $options); + $min_values = LSconfig :: get('params.min', null, 'int', $options); + if(is_null($max_values) && is_null($min_values)) { + LSerror :: addErrorCode('LSformRule_01',array('type' => 'numberOfValues', 'param' => _('max (or min)'))); + return; + } + if (!is_null($max_values) && !is_null($min_values) && $max_values < $min_values) { + LSerror :: addErrorCode('LSformRule_numberOfValues_01'); + return; + } + + $count = count($value); + if (!is_null($min_values) && $count < $min_values) + throw new LSformRuleException( + getFData( + ngettext( + 'At least one value is required.', + 'At least %{min} values are required.', + $min_values + ), + $count + ) + ); + + if (!is_null($max_values) && $count > $max_values) + throw new LSformRuleException( + getFData( + ngettext( + 'Maximum one value is allowed.', + 'Maximum %{max} values are allowed.', + $count + ), + $max_values + ) + ); + return True; + } + +} + +/** + * Error Codes + **/ +LSerror :: defineError('LSformRule_numberOfValues_01', +___("LSformRule_numberOfValues: Parameter max could not be lower than parameter min.") +);