Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dashes in element names #6

Closed
billdami opened this issue May 9, 2013 · 5 comments
Closed

Allow dashes in element names #6

billdami opened this issue May 9, 2013 · 5 comments
Labels

Comments

@billdami
Copy link

billdami commented May 9, 2013

Currently the function excludes any elements in the form that contain a dash in the name attribute, however using dashes are completely valid according the the W3C spec.

To include elements with dashed names in the returned object, I just updated the patterns.validate, patterns.key, and patterns.named regular expressions to include a dash in their character sets resulting in the following:

patterns = {
    validate: /^[a-zA-Z][a-zA-Z0-9_-]*(?:\[(?:\d*|[a-zA-Z0-9_]+)\])*$/,
    key: /[a-zA-Z0-9_-]+|(?=\[\])/g,
    push: /^$/,
    fixed: /^\d+$/,
    named: /^[a-zA-Z0-9_-]+$/
};
@macek
Copy link
Owner

macek commented May 11, 2013

Thanks for submitting this bug.

I'm going to build some unit tests to ensure functionality of the plugin first, then I'll do an iteration that closes out this issue for 2.1

@briantjacobs
Copy link

Likewise, for allowing dots in element names. Swapping out the dashes in the regex above with dots seems to do the trick.

@reggi
Copy link

reggi commented Dec 29, 2013

👍

@brandonb927
Copy link

Any progress on this? I just applied this "patch" to my forked version and have been using it on some heavily nested complex form items with no hiccups :)

@macek
Copy link
Owner

macek commented Mar 11, 2014

Hey everyone,

I've made some improvements to the plugin over the last couple of days.

For now, I think I'm going to leave the regexp patterns the way they are, but I've exposed them so you can customize them in your own apps. This pleases everyone because you can hack them up to have the plugin behave however you like.

I've made this decision to keep it easy to access the object returned from the plugin. For example

<input name="foo" value="a">

allows us to access the serialized object as obj.foo. Whereas

<input name="foo-bar" value="a">

would require us to use obj["foo-bar"] which is significantly less ideal.

For those looking for the simple addition of hyphens (-), I've provided an example in the API documentation.

I'll also paste the API amendment here


FormSerializer.patterns — modify the patterns used to match field
names

Many of you have requested to allow - in field names or use . to nest keys.
You can now configure these to your heart's content.

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

Validating and Key parsing

  • validate — only valid input names will be serialized; invalid names
    will be skipped
  • key — this pattern parses all "keys" from the input name; You will
    want to use /g as a modifier with this regexp.

Key styles

  • push — pushe a value to an array

    <input name="foo[]" value="a">
    <input name="foo[]" value="b">
    $("form").serializeObject();
    //=> {foo: [a, b]}
  • fixed — add a value to an array at a specified index

    <input name="foo[2]" value="a">
    <input name="foo[4]" value="b">
    $("form").serializeObject();
    //=> {foo: [, , "a", , "b"]}
  • named — adds a value to the specified key

    <input name="foo[bar]" value="a">
    <input name="foo[bof]" value="b">
    <input name="hello" value="world">
    $("form").serializeObject();
    //=> {foo: {bar: "a", bof: "b"}, hello: "world"}

If anyone needs additional support, I will be happy to help out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants