macek/jquery-serialize-object

Ability to omit form fields.

howardroark opened this issue · 8 comments

Hey! Great tool. I can't believe jquery does not have this feature. Must be stuck in Web 2.0 ;)

I think this issue is like #46 but I'm not sure. Essentially I am using stripe.js to convert credit card data to a hash for my application to use. I would like to pass my API all the form details except for the fields related to credit card data.

It would be awesome if this tool could omit form fields that were marked with a particular class or data attribute.

If you point me in the right direction I could take a stab at it as well.

Thanks!

I think that can be done in one of the magical regexes...
I don't dare touch them, though :)

I think you could just do this....

  FormSerializer.serializeObject = function serializeObject() {
    if (this.length > 1) {
      return new Error("jquery-serialize-object can only serialize one form at a time");
    }
    var filteredForm = this.find(':input').not('.omit');
    return new FormSerializer($, this).
      addPairs(filteredForm.serializeArray()).
      serialize();
  };

vs this...

  FormSerializer.serializeObject = function serializeObject() {
    if (this.length > 1) {
      return new Error("jquery-serialize-object can only serialize one form at a time");
    }
    return new FormSerializer($, this).
      addPairs(this.serializeArray()).
      serialize();
  };

I think that can be done in one of the magical regexes...
I don't dare touch them, though :)

@malles the regexps are not meant to filter specific fields. Modifications to the regexps are only there to differentiate how various inputs are handled while serializing.

@howardroark I suspect this can be accomplished with minimal modification to the plugin itself

$("#my-form :input").not(".omit").serializeObject();

I like this approach because of the inversion of control. From the end user's point of view, there's no additional API/convention to learn and it's compatible with any kind of user-created form. (see also #23).

To support this, we'd have to remove the following check in the code you mentioned above

if (this.length > 1) {
  return new Error("jquery-serialize-object can only serialize one form at a time");
}

After that, we're just calling this.serializeArray() which works on any set of inputs.

Add a couple tests to verify everything works as intended and we'd be good to go.

Thoughts ?

I like the idea of having it as open as possible as well! Have a look at #53 :)

$.serializeArray() does not appear to work unless you give a form object. I too would like to be able to serialize a sub-section of my form.

@mnpenner this is something I think that should be supported as well. I'll get to some of these issues as soon as I have a little extra time. Things have just been really hectic for me lately.

yes please @macek

macek commented

I just published version 2.5.0 which allows you to serialize multiple elements

Doing something like this will not work

$("#my-form :input").not(".omit").serializeObject();

Please read the specified behavior in selector-test.js for more info.