From 9a8bf747141525434b7cf269ca311a988919c5a7 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 31 Jan 2017 17:15:49 +0800 Subject: [PATCH] #71 Added validation for element and template name length --- classes/edit_element_form.php | 12 ++++++++++-- classes/edit_form.php | 6 +++++- lang/en/customcert.php | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/classes/edit_element_form.php b/classes/edit_element_form.php index 2322267..34088ac 100644 --- a/classes/edit_element_form.php +++ b/classes/edit_element_form.php @@ -49,7 +49,7 @@ class edit_element_form extends \moodleform { $element = $this->_customdata['element']; // Add the field for the name of the element, this is required for all elements. - $mform->addElement('text', 'name', get_string('elementname', 'customcert')); + $mform->addElement('text', 'name', get_string('elementname', 'customcert'), 'maxlength="255"'); $mform->setType('name', PARAM_TEXT); $mform->setDefault('name', get_string('pluginname', 'customcertelement_' . $element->element)); $mform->addRule('name', get_string('required'), 'required', null, 'client'); @@ -76,6 +76,14 @@ class edit_element_form extends \moodleform { * @return array the errors that were found */ public function validation($data, $files) { - return $this->element->validate_form_elements($data, $files); + $errors = array(); + + if (\core_text::strlen($data['name']) > 255) { + $errors['name'] = get_string('nametoolong', 'customcert'); + } + + $errors += $this->element->validate_form_elements($data, $files); + + return $errors; } } diff --git a/classes/edit_form.php b/classes/edit_form.php index 475df4a..0b7bc99 100644 --- a/classes/edit_form.php +++ b/classes/edit_form.php @@ -51,7 +51,7 @@ class edit_form extends \moodleform { $mform =& $this->_form; - $mform->addElement('text', 'name', get_string('name', 'customcert')); + $mform->addElement('text', 'name', get_string('name', 'customcert'), 'maxlength="255"'); $mform->setType('name', PARAM_TEXT); $mform->addRule('name', null, 'required'); @@ -130,6 +130,10 @@ class edit_form extends \moodleform { public function validation($data, $files) { $errors = parent::validation($data, $files); + if (\core_text::strlen($data['name']) > 255) { + $errors['name'] = get_string('nametoolong', 'customcert'); + } + // Go through the data and check any width, height or margin values. foreach ($data as $key => $value) { if (strpos($key, 'pagewidth_') !== false) { diff --git a/lang/en/customcert.php b/lang/en/customcert.php index 3d8352b..285d994 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -86,6 +86,7 @@ $string['modulename_help'] = 'This module allows for the dynamic generation of P $string['modulename_link'] = 'Custom_certificate_module'; $string['mycertificates'] = 'My certificates'; $string['name'] = 'Name'; +$string['nametoolong'] = 'You have exceeded the maximum length allowed for the name'; $string['nocustomcerts'] = 'There are no custom certificates for this course'; $string['noimage'] = 'No image'; $string['notemplates'] = 'No templates';