diff --git a/admin/tool/messageinbound/settings.php b/admin/tool/messageinbound/settings.php index 72d23ab4381..bb562e00c52 100644 --- a/admin/tool/messageinbound/settings.php +++ b/admin/tool/messageinbound/settings.php @@ -41,9 +41,9 @@ if ($hassiteconfig) { $settings->add(new admin_setting_heading('messageinbound_mailboxconfiguration', new lang_string('mailboxconfiguration', 'tool_messageinbound'), new lang_string('messageinboundmailboxconfiguration_desc', 'tool_messageinbound'), '')); - $settings->add(new admin_setting_configtext('messageinbound_mailbox', + $settings->add(new admin_setting_configtext_with_maxlength('messageinbound_mailbox', new lang_string('mailbox', 'tool_messageinbound'), - null, '', PARAM_RAW)); + null, '', PARAM_RAW, null, 15)); $settings->add(new admin_setting_configtext('messageinbound_domain', new lang_string('domain', 'tool_messageinbound'), null, '', PARAM_RAW)); diff --git a/lib/adminlib.php b/lib/adminlib.php index 485a7100e65..380d1d50866 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -2182,6 +2182,59 @@ class admin_setting_configtext extends admin_setting { } } +/** + * Text input with a maximum length constraint. + * + * @copyright 2015 onwards Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class admin_setting_configtext_with_maxlength extends admin_setting_configtext { + + /** @var int maximum number of chars allowed. */ + protected $maxlength; + + /** + * Config text constructor + * + * @param string $name unique ascii name, either 'mysetting' for settings that in config, + * or 'myplugin/mysetting' for ones in config_plugins. + * @param string $visiblename localised + * @param string $description long localised info + * @param string $defaultsetting + * @param mixed $paramtype int means PARAM_XXX type, string is a allowed format in regex + * @param int $size default field size + * @param mixed $maxlength int maxlength allowed, 0 for infinite. + */ + public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, + $size=null, $maxlength = 0) { + $this->maxlength = $maxlength; + parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype, $size); + } + + /** + * Validate data before storage + * + * @param string $data data + * @return mixed true if ok string if error found + */ + public function validate($data) { + $parentvalidation = parent::validate($data); + if ($parentvalidation === true) { + if ($this->maxlength > 0) { + // Max length check. + $length = core_text::strlen($data); + if ($length > $this->maxlength) { + return get_string('maximumchars', 'moodle', $this->maxlength); + } + return true; + } else { + return true; // No max length check needed. + } + } else { + return $parentvalidation; + } + } +} /** * General text area without html editor.