From f82c9d8d061e1c3874bb12a5fee8dfd84e69493a Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Tue, 9 Apr 2019 15:40:52 +0200 Subject: [PATCH] MDL-65217 form: option to randomise element ids --- lib/formslib.php | 2 ++ lib/pear/HTML/QuickForm/element.php | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/lib/formslib.php b/lib/formslib.php index 9e2c96f0190..18fe4710420 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -163,6 +163,8 @@ abstract class moodleform { * @param string $target target frame for form submission. You will rarely use this. Don't use * it if you don't need to as the target attribute is deprecated in xhtml strict. * @param mixed $attributes you can pass a string of html attributes here or an array. + * Special attribute 'data-random-ids' will randomise generated elements ids. This + * is necessary when there are several forms on the same page. * @param bool $editable * @param array $ajaxformdata Forms submitted via ajax, must pass their data here, instead of relying on _GET and _POST. */ diff --git a/lib/pear/HTML/QuickForm/element.php b/lib/pear/HTML/QuickForm/element.php index 18332ea9997..042f1910d63 100644 --- a/lib/pear/HTML/QuickForm/element.php +++ b/lib/pear/HTML/QuickForm/element.php @@ -384,6 +384,10 @@ class HTML_QuickForm_element extends HTML_Common switch ($event) { case 'createElement': static::__construct($arg[0], $arg[1], $arg[2], $arg[3], $arg[4]); + if ($caller->getAttribute('data-random-ids') && !$this->getAttribute('id')) { + $this->_generateId(); + $this->updateAttributes(array('id' => $this->getAttribute('id') . '_' . random_string())); + } break; case 'addElement': $this->onQuickFormEvent('createElement', $arg, $caller);