macek/jquery-serialize-object

Issue with mixed keys

rafa8626 opened this issue · 2 comments

Based on #18 I was able to override fixed using $.extend(FormSerializer.patterns, {...}) by setting fixed: /^$/

The problem is that I have a case like this:

<form id="contact">
  <input name="user[email]" value="jsmith@example.com">
  <input name="user[pets][]" type="checkbox" value="cat" checked>
  <input name="user[pets][]" type="checkbox" value="dog" checked>
  <input name="user[pets][]" type="checkbox" value="bird">
  <input name="user[pets][7]" type="checkbox" value="tiger" checked>
  <input name="user[pets][182]" type="checkbox" value="bull" checked>
  <input name="user[pets][19999]" type="checkbox" value="chicken">
  <input type="submit">
</form>

So that thing solves the issue #18 but it's not grabbing the ones that don't have a number between [], any advises on how to achieve this?

macek commented

Well your situation is sort of broken, at least as I see it here

What is 7, 182, and 19999? What is their significance?

How do you see the following form working?

<input name="user[pets][]" type="checkbox" value="cat" checked>
<input name="user[pets][]" type="checkbox" value="dog" checked>
<input name="user[pets][1]" type="checkbox" value="bird" checked>

The bird would overwrite the dog value because

  1. cat gets pushed onto user.pets (at index 0)
  2. dog gets pushed onto user.pets (at index 1)
  3. bird gets manually set to users.pets[1], overwriting the existing value, dog

I can see that in your static example, there is no collision of indexes, but I'm guessing those indexes (7, 182, and 19999) are determined by dynamic values of some kind.

Without knowing more about your specific situation, my only recommendation would be to just us a manual index

<input name="user[email]" value="jsmith@example.com">
<input name="user[pets][0]" type="checkbox" value="cat" checked>
<input name="user[pets][1]" type="checkbox" value="dog" checked>
<input name="user[pets][2]" type="checkbox" value="bird">
<input name="user[pets][7]" type="checkbox" value="tiger" checked>
<input name="user[pets][182]" type="checkbox" value="bull" checked>
<input name="user[pets][19999]" type="checkbox" value="chicken">

These are IDs from the database, but the user adds new elements and we keep the array element open. Well I'll try to check with my team about this. Thanks.