Skip to content

Commit

Permalink
MDL-65217 form: option to randomise element ids
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Apr 29, 2019
1 parent 521989e commit f82c9d8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/formslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
4 changes: 4 additions & 0 deletions lib/pear/HTML/QuickForm/element.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ function onQuickFormEvent($event, $arg, &$caller)
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);
Expand Down

0 comments on commit f82c9d8

Please sign in to comment.