macek/jquery-serialize-object

Fieldnames starting with a number not in object

malles opened this issue · 3 comments

When a fieldname startst with a number, it is not serialized, although it is in the output of serializeArray.

http://plnkr.co/edit/Qx8EZixStTeWiwZZUAB2?p=preview

Is that fixable? The fieldname is a token that is rendered and I don't have control over.

Thanks for the detailed paste. It makes it way easier to help with this kind of issue.

The fix is relatively simple. You have full control over the regexp patterns that this plugin is using internally.

A simple tweak to FormSerializer.patterns.validate can allow numbers as the initial character

// this is the original
FormSerializer.patterns.validate = /^[a-z_][a-z0-9_]*(?:\[(?:\d*|[a-z0-9_]+)\])*$/i;

Override the pattern using $.extend

// allow alphanumeric first character
$.extend(FormSerializer.patterns, {
  validate: /^[a-z0-9_-]+(?:\[(?:\d*|[a-z0-9_]+)\])*$/i,
});

Set this snippet to initialize on page load. Any subsequent calls to $.fn.serializeObject or $.fn.serializeJSON will utilize the new patterns.

Let me know if this works for you.

@malles, just one more thing to mention here.

The 3.x version of this plugin will be serializing according to the W3C draft spec for JSON serialization of forms.

Such modifications will not be required to get your desired output once that's ready.

Thanks @macek! It works like a charm.
Awaiting the new version, this plugin helps me out bigtime.