Skip to content

Commit

Permalink
Add number validators see #8177
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed May 3, 2016
1 parent b879482 commit d302357
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
2 changes: 2 additions & 0 deletions main/exercice/exercise.class.php
Expand Up @@ -2243,6 +2243,8 @@ public function createForm($form, $type='full')
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(
Expand Down
30 changes: 16 additions & 14 deletions main/inc/lib/pear/HTML/QuickForm/Rule/Range.php
Expand Up @@ -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':
Expand All @@ -67,7 +71,5 @@ function getValidationScript($options = null)
$test = '({jsVar}.length < '.$options[0].' || {jsVar}.length > '.$options[1].')';
}
return array('', "{jsVar} != '' && {$test}");
} // end func getValidationScript

} // end class HTML_QuickForm_Rule_Range
?>
}
}
5 changes: 3 additions & 2 deletions main/inc/lib/pear/HTML/QuickForm/RuleRegistry.php
Expand Up @@ -119,6 +119,8 @@ function getRule($ruleName)
'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',
Expand Down Expand Up @@ -149,8 +151,7 @@ function getRule($ruleName)
'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];
Expand Down

0 comments on commit d302357

Please sign in to comment.