Moodle
  1. Moodle
  2. MDL-21382

addGroupRule generates incorrect JS function names with square brackets

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.9.7
    • Fix Version/s: 1.9.10, 2.0
    • Component/s: Forms Library
    • Labels:
      None
    • Affected Branches:
      MOODLE_19_STABLE
    • Fixed Branches:
      MOODLE_19_STABLE, MOODLE_20_STABLE
    • Rank:
      27274

      Description

      When addGroupRule is called with an array as the second element, with client-side validation enabled, it will generate JavaScript code with function names that have square brackets in them. This can be seen, e.g. by going to /course/modedit.php?add=resource&type=file&course=2&section=0&return=0 in any Moodle site, which will try to create (and use) a JavaScript function called "validate_mod_resource_mod_form_reference[value](element)".

      This can be fixed by adding some character replacement to the getValidationScript function in lib/formslib.php, at around line 1347. The context for that line is:

      $validateJS = '';
      foreach ($test as $elementName => $jsandelement) {
      // Fix for bug displaying errors for elements in a group
      //unset($element);
      list($jsArr,$element)=$jsandelement;
      //end of fix
      $js .= '
      function validate_' . $this->formName . '' . $elementName . '(element) {
      var value = \'\';

      The character replacement has to come before the "$js .= " line.

        Activity

        Hide
        Hubert Chathi added a comment -

        I'm not sure what kind of character replacement should be done. One possibility is to replace all "special" characters by "_XX" where XX is the hex value of the character.

        Show
        Hubert Chathi added a comment - I'm not sure what kind of character replacement should be done. One possibility is to replace all "special" characters by "_XX" where XX is the hex value of the character.
        Hide
        Hubert Chathi added a comment -

        I have attached a patch that implements my suggested character replacement scheme, and outputs the escaped name in places where it should be escaped (and leaves the other places alone). I think...

        Show
        Hubert Chathi added a comment - I have attached a patch that implements my suggested character replacement scheme, and outputs the escaped name in places where it should be escaped (and leaves the other places alone). I think...
        Hide
        Erick Torres added a comment -

        I have implement this solution, and will done

        Show
        Erick Torres added a comment - I have implement this solution, and will done
        Hide
        Erick Torres added a comment -

        No yet

        Show
        Erick Torres added a comment - No yet
        Hide
        David Mudrak added a comment -

        Fixed in HEAD and MOODLE_19_STABLE. Credit goes to Hubert for the patch provided. I have also fixed the client side validation of 'Location' field in the file resource in 1.9.

        Show
        David Mudrak added a comment - Fixed in HEAD and MOODLE_19_STABLE. Credit goes to Hubert for the patch provided. I have also fixed the client side validation of 'Location' field in the file resource in 1.9.

          People

          • Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: