macek/jquery-serialize-object

Doesn't support non-object "keys"

reggi opened this issue · 3 comments

There's no support for anything I'm trying to do here because it seems that there's only support for object dot-notation syntax.

  <input type="hidden" name="customer_properties[$email]" value="george.washington@example.com">
  <input type="hidden" name="properties[Total Price]" value="24.99">
  <input type="hidden" name="properties[Items Purchased][]" value="Cheerios">
  <input type="hidden" name="properties[Items Purchased][]" value="Milk">
  <input type="hidden" name="properties[Items Purchased][]" value="Cereal Bowls">

This should be totally valid

@reggi Please see issue #48

... 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.

The regexp are there for you to change. The default regexp this plugin uses supports a specific format outlined in the README.

If you want to support spaces, you would have to update the validate, key, and named patterns to include a space character

The following is untested code, and as with any solution provided online, should not be copy/pasted into your project before you understand how it works.

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

Compare the pattern above to the default patterns. This should give you a better idea how make changes on your own.

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

@macek didn't know you could set the patterns! Thanks a bunch! 😸

@reggi no problem. Happy to help.