Permalink
Browse files

MDL-21382 mform: escape invalid characters when generating javascript…

… validation function name

This patch fixes the support for addGroupRule in cases when the element
name contains invalid characters like square brackets (that is if the
group element is an array element).

Credit goes to Hubert Chathi for the solution and the patch provided.
  • Loading branch information...
1 parent 50978a0 commit 82b7f70bf45812a397444d7c1a91145466d2f510 @mudrd8mz mudrd8mz committed Sep 17, 2010
Showing with 7 additions and 3 deletions.
  1. +7 −3 lib/formslib.php
View
10 lib/formslib.php
@@ -1349,8 +1349,12 @@ function qf_errorHandler(element, _qfMsg) {
//unset($element);
list($jsArr,$element)=$jsandelement;
//end of fix
+ $escapedElementName = preg_replace_callback(
+ '/[_\[\]]/',
+ create_function('$matches', 'return sprintf("_%2x",ord($matches[0]));'),
+ $elementName);
$js .= '
-function validate_' . $this->_formName . '_' . $elementName . '(element) {
+function validate_' . $this->_formName . '_' . $escapedElementName . '(element) {
var value = \'\';
var errFlag = new Array();
var _qfGroups = {};
@@ -1364,7 +1368,7 @@ function validate_' . $this->_formName . '_' . $elementName . '(element) {
}
';
$validateJS .= '
- ret = validate_' . $this->_formName . '_' . $elementName.'(frm.elements[\''.$elementName.'\']) && ret;
+ ret = validate_' . $this->_formName . '_' . $escapedElementName.'(frm.elements[\''.$elementName.'\']) && ret;
if (!ret && !first_focus) {
first_focus = true;
frm.elements[\''.$elementName.'\'].focus();
@@ -1375,7 +1379,7 @@ function validate_' . $this->_formName . '_' . $elementName . '(element) {
//unset($element);
//$element =& $this->getElement($elementName);
//end of fix
- $valFunc = 'validate_' . $this->_formName . '_' . $elementName . '(this)';
+ $valFunc = 'validate_' . $this->_formName . '_' . $escapedElementName . '(this)';
$onBlur = $element->getAttribute('onBlur');
$onChange = $element->getAttribute('onChange');
$element->updateAttributes(array('onBlur' => $onBlur . $valFunc,

0 comments on commit 82b7f70

Please sign in to comment.