From d30235711163af546b45cc91f978ed5addd85acf Mon Sep 17 00:00:00 2001 From: jmontoyaa Date: Tue, 3 May 2016 13:14:01 +0200 Subject: [PATCH] Add number validators see #8177 --- main/exercice/exercise.class.php | 2 ++ .../lib/pear/HTML/QuickForm/Rule/Range.php | 30 ++++++++++--------- .../lib/pear/HTML/QuickForm/RuleRegistry.php | 5 ++-- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/main/exercice/exercise.class.php b/main/exercice/exercise.class.php index 33a61274bc..83699b4519 100755 --- a/main/exercice/exercise.class.php +++ b/main/exercice/exercise.class.php @@ -2243,6 +2243,8 @@ class Exercise array('id' => 'pass_percentage') ); $form->addRule('pass_percentage', get_lang('Numeric'), 'numeric'); + $form->addRule('pass_percentage', get_lang('ValueTooSmall'), 'min_numeric_length', 0); + $form->addRule('pass_percentage', get_lang('ValueTooBig'), 'max_numeric_length', 100); // add the text_when_finished textbox $form->addHtmlEditor( diff --git a/main/inc/lib/pear/HTML/QuickForm/Rule/Range.php b/main/inc/lib/pear/HTML/QuickForm/Rule/Range.php index b40222ca31..65da6b429c 100755 --- a/main/inc/lib/pear/HTML/QuickForm/Rule/Range.php +++ b/main/inc/lib/pear/HTML/QuickForm/Rule/Range.php @@ -40,21 +40,25 @@ class HTML_QuickForm_Rule_Range extends HTML_QuickForm_Rule * @access public * @return boolean true if value is valid */ - function validate($value, $options) + public function validate($value, $options) { - // Modified by Ivan Tcholakov, 16-MAR-2010. - //$length = strlen($value); $length = api_strlen($value); - // + switch ($this->name) { - case 'minlength': return ($length >= $options); - case 'maxlength': return ($length <= $options); - default: return ($length >= $options[0] && $length <= $options[1]); + case 'min_numeric_length': + return (float) $value >= $options; + case 'max_numeric_length': + return (float) $value <= $options; + case 'minlength': + return $length >= $options; + case 'maxlength': + return $length <= $options; + default: + return $length >= $options[0] && $length <= $options[1]; } - } // end func validate - + } - function getValidationScript($options = null) + public function getValidationScript($options = null) { switch ($this->name) { case 'minlength': @@ -67,7 +71,5 @@ class HTML_QuickForm_Rule_Range extends HTML_QuickForm_Rule $test = '({jsVar}.length < '.$options[0].' || {jsVar}.length > '.$options[1].')'; } return array('', "{jsVar} != '' && {$test}"); - } // end func getValidationScript - -} // end class HTML_QuickForm_Rule_Range -?> + } +} \ No newline at end of file diff --git a/main/inc/lib/pear/HTML/QuickForm/RuleRegistry.php b/main/inc/lib/pear/HTML/QuickForm/RuleRegistry.php index 4b90f49830..838b22cae3 100755 --- a/main/inc/lib/pear/HTML/QuickForm/RuleRegistry.php +++ b/main/inc/lib/pear/HTML/QuickForm/RuleRegistry.php @@ -119,6 +119,8 @@ class HTML_QuickForm_RuleRegistry 'required' => 'HTML_QuickForm_Rule_Required', 'maxlength' => 'HTML_QuickForm_Rule_Range', 'minlength' => 'HTML_QuickForm_Rule_Range', + 'max_numeric_length' => 'HTML_QuickForm_Rule_Range', + 'min_numeric_length' => 'HTML_QuickForm_Rule_Range', 'rangelength' => 'HTML_QuickForm_Rule_Range', 'email' => 'HTML_QuickForm_Rule_Email', 'regex' => 'HTML_QuickForm_Rule_Regex', @@ -149,8 +151,7 @@ class HTML_QuickForm_RuleRegistry 'maxfilesize', 'HTML_QuickForm_Rule_MaxFileSize', 'mimetype', 'HTML_QuickForm_Rule_MimeType', 'filename', 'HTML_QuickForm_Rule_FileName', - 'validquestiontype' => 'HTML_QuickForm_Rule_QuestionType', - + 'validquestiontype' => 'HTML_QuickForm_Rule_QuestionType' ); $class = $rules[$ruleName];