MDL-66106 core_form: fix client side validation bug caused by MDL-65217
authorJake Dallimore <jake@moodle.com>
Mon, 8 Jul 2019 08:00:55 +0000 (16:00 +0800)
committerJake Dallimore <jake@moodle.com>
Fri, 20 Sep 2019 01:03:25 +0000 (09:03 +0800)
Forms with randomised ids need to support client side validation.

lib/formslib.php

index ca39e81..2dd9b33 100644 (file)
@@ -2470,14 +2470,16 @@ require(["core/event", "jquery"], function(Event, $) {
 ';
                 }
             }
+            // This handles both randomised (MDL-65217) and non-randomised IDs.
+            $errorid = preg_replace('/^id_/', 'id_error_', $this->_attributes['id']);
             $validateJS .= '
       ret = validate_' . $this->_formName . '_' . $escapedElementName.'(frm.elements[\''.$elementName.'\'], \''.$escapedElementName.'\') && ret;
       if (!ret && !first_focus) {
         first_focus = true;
         Y.use(\'moodle-core-event\', function() {
             Y.Global.fire(M.core.globalEvents.FORM_ERROR, {formid: \'' . $this->_attributes['id'] . '\',
-                                                           elementid: \'id_error_' . $escapedElementName . '\'});
-            document.getElementById(\'id_error_' . $escapedElementName . '\').focus();
+                                                           elementid: \'' . $errorid. '\'});
+            document.getElementById(\'' . $errorid . '\').focus();
         });
       }
 ';