#85 bug
John-David Dalton

Make Form#serializeElements more accurate to the spec.

Reported by John-David Dalton | May 8th, 2008 @ 01:15 PM | in 1.6.1

As this page points out:

http://www.malsup.com/jquery/for...

Prototype needs to follow the w3c spec a bit more:

http://www.w3.org/TR/html401/int...

With recent commits we have ignored the file input elements, that's a start.

http://github.com/sstephenson/pr...

We still need to ignore reset buttons and properly handle input type "image".

We should also allow the "submit" option to be an element in case multiple buttons have the same name.

I have attached a patch that corrects these issues and makes the logic easier to read.

I still need to make unit tests (it passes the current ones).

Background info:

http://github.com/sstephenson/pr...

Comments and changes to this ticket

  • John-David Dalton

    John-David Dalton May 8th, 2008 @ 01:19 PM

      • → State changed from “new” to “bug”

    I consider this a bug because its not doing the intended functionality properly.

  • John-David Dalton

    John-David Dalton May 8th, 2008 @ 01:19 PM

    Please optimize the patch if you can make it smaller or more elegant.

  • John-David Dalton

    John-David Dalton May 8th, 2008 @ 01:20 PM

      • → Assigned user changed from “” to “John-David Dalton”
  • John-David Dalton

    John-David Dalton May 8th, 2008 @ 02:59 PM

    I have modified the patch a bit to allow you to specify the x,y coords of a click on an input type="image"

          if (isSubmitButton) {
            submitSerialized = true;
            if (isImageType) {
              var prefix = key ? key + '.' : '',
              x = options.x || 0, y = options.y || 0;
              
              result[prefix + 'x'] = x;
              result[prefix + 'y'] = y;
              return result;
            }
          }
    

    useage:

    $('myForm').serialize({submit:element, x:20, y:10});
    
  • Andrew Dupont

    Andrew Dupont May 8th, 2008 @ 03:35 PM

      • → Milestone changed from “” to “1.6.1”
  • John-David Dalton

    John-David Dalton May 13th, 2008 @ 06:29 PM

    Here is a more recent version of the patch:

      serializeElements: function(elements, options) {
        if (typeof options != 'object') options = { hash: !!options };
        else if (Object.isUndefined(options.hash)) options.hash = true;
        var key, value, type, isImageType, isSubmitButton, submit = {};
        
        if (Object.isElement(options.submit)) submit.element = options.submit;
        else if (Object.isString(options.submit)) submit.name = options.submit;
        else if (options.submit === false) submit = false;
        
        var data = elements.inject({ }, function(result, element) {
          element = $(element);
          key     = element.name; 
          value   = element.getValue();
          type    = element.type;
          
          isImageType = (type == 'image');
          isSubmitButton = (type == 'submit' || isImageType);
          
          if (element.disabled || value == null || type == 'file' || type == 'reset' || (isSubmitButton &&
             (submit === false || submit.serialized || (submit.name && key != submit.name) ||
               (submit.element && element != submit.element))))
            return result;
          
          if (isSubmitButton) {
            Object.extend(submit, {name:key, value:value, serialized:true});
            if (submit.isImageType = isImageType) {
              var prefix = key ? key + '.' : '',
              x = options.x || 0, y = options.y || 0;
              result[prefix + 'x'] = x;
              result[prefix + 'y'] = y;
              return result;
            }
          }
          else if (!key) return result;
          
          if (key in result) {
          
            /* Is this needed?
               How should we handle elements of different type's having the same name?
            
            // handle special conditions
            if (submit.isImageType && (submit.name == key ||
               (!submit.name && (key == 'x' || key == 'y'))))
              return result[key] = submit.value;
            else if (submit.name == key)
              return result[key] = value;
            */
            
            // a key is already present; construct an array of values
            if (!Object.isArray(result[key])) result[key] = [result[key]];
            result[key].push(value);
          }
          else result[key] = value;
          
          return result;
        });
        
        return options.hash ? data : Object.toQueryString(data);
      }
    

    This makes button elements with type "reset" ignored too (IE doesn't ignore then but every other browser does).

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins