MDL-87874 customfield_number: validation for maximum size constraints.

This commit is contained in:
Paul Holden
2026-02-06 15:53:20 +00:00
parent e1c3872f65
commit fe2572647f
4 changed files with 38 additions and 1 deletions
@@ -59,7 +59,18 @@ class data_controller extends \core_customfield\data_controller {
return;
}
// Required for subsequent input field comparison rule.
$mform->addElement('hidden', "{$elementname}_maximum", SQL_INT_MAX + 1);
$mform->setType("{$elementname}_maximum", PARAM_INT);
// Number input field.
$mform->addElement('float', $elementname, $this->get_field()->get_formatted_name());
$mform->addRule(
[$elementname, "{$elementname}_maximum"],
get_string('maximumvalueerror', 'customfield_number', SQL_INT_MAX),
'compare',
'lt',
);
if (!$this->get('id')) {
$mform->setDefault($elementname, $this->get_default_value());
}
@@ -50,20 +50,42 @@ class field_controller extends \core_customfield\field_controller {
}
}
// Required for subsequent input field comparison rule.
$mform->addElement('hidden', "maximum", SQL_INT_MAX + 1);
$mform->setType("maximum", PARAM_INT);
// Default value.
$mform->addElement('float', 'configdata[defaultvalue]', get_string('defaultvalue', 'core_customfield'));
$mform->addRule(
['configdata[defaultvalue]', 'maximum'],
get_string('maximumvalueerror', 'customfield_number', SQL_INT_MAX),
'compare',
'lt',
);
if ($this->get_configdata_property('defaultvalue') === null) {
$mform->setDefault('configdata[defaultvalue]', '');
}
// Minimum value.
$mform->addElement('float', 'configdata[minimumvalue]', get_string('minimumvalue', 'customfield_number'));
$mform->addRule(
['configdata[minimumvalue]', 'maximum'],
get_string('maximumvalueerror', 'customfield_number', SQL_INT_MAX),
'compare',
'lt',
);
if ($this->get_configdata_property('minimumvalue') === null) {
$mform->setDefault('configdata[minimumvalue]', '');
}
// Maximum value.
$mform->addElement('float', 'configdata[maximumvalue]', get_string('maximumvalue', 'customfield_number'));
$mform->addRule(
['configdata[maximumvalue]', 'maximum'],
get_string('maximumvalueerror', 'customfield_number', SQL_INT_MAX),
'compare',
'lt',
);
if ($this->get_configdata_property('maximumvalue') === null) {
$mform->setDefault('configdata[maximumvalue]', '');
}
@@ -109,7 +109,10 @@ final class data_controller_test extends advanced_testcase {
$category = $generator->create_category();
$field = $generator->create_field(['categoryid' => $category->get('id'), 'type' => 'number']);
$formdata = array_merge((array) $course, ['customfield_' . $field->get('shortname') => 42]);
$formdata = array_merge((array) $course, [
'customfield_' . $field->get('shortname') => 42,
'customfield_' . $field->get('shortname') . '_maximum' => SQL_INT_MAX + 1,
]);
core_customfield_test_instance_form::mock_submit($formdata);
$form = new core_customfield_test_instance_form('POST', ['handler' => $category->get_handler(), 'instance' => $course]);
@@ -93,6 +93,7 @@ final class field_controller_test extends advanced_testcase {
$field = $generator->create_field(['categoryid' => $category->get('id'), 'type' => 'number']);
$submitdata = (array) $field->to_record();
$submitdata['maximum'] = SQL_INT_MAX + 1;
$submitdata['configdata'] = array_merge($field->get('configdata'), [
'defaultvalue' => $defaultvalue,
'minimumvalue' => $minimumvalue,