macek/jquery-serialize-object

Support for keys with hyphens

Closed this issue · 1 comments

Thanks so much for creating this code. I was especially interested in getting hyphenated input names to work.

In using the code you provided in your Readme, I found that an input name such as foo[bar-baz] still does not work, but this can be fixed by updating the relevant group in the patterns.validate regex to include a dash, as follows:

$.extend(FormSerializer.patterns, {
  validate: /^[a-z][a-z0-9_-]*(?:\[(?:\d*|[a-z0-9_-]+)\])*$/i,
  key:      /[a-z0-9_-]+|(?=\[\])/gi,
  named:    /^[a-z0-9_-]+$/i
});

@marcosrogers thanks for submitting this issue.

Either way, the regexp patterns were opened up so that end users could customize them however they want. "Hyphen notation" is not any sort of official thing supported by this plugin, but I provided examples to help people understand how they could customize the plugin to meet their needs.

I updated the regexp in the README to

$.extend(FormSerializer.patterns, {
  validate: /^[a-z][a-z0-9_-]*(?:\[(?:\d*|[a-z0-9_-]+)\])*$/i,
  key:      /[a-z0-9_-]+|(?=\[\])/gi,
  named:    /^[a-z0-9_-]+$/i
});

The only difference here is the extra - in the bracketed section near the end.

I added a test in version 2.4.5 to test that this regexp functions properly now

If you need any other help, please let me know.