brainfoolong / form-data-json

A zero dependency, cross browser library to easily get or set/manipulate form input values as/from a json object.
https://brainfoolong.github.io/form-data-json
MIT License
57 stars 10 forks source link

Elements out of order are processed incorrectly #27

Closed Nazar84 closed 2 years ago

Nazar84 commented 2 years ago

HI! I`v got a little bit problem. When my input fields have names (index) out of order the hash is in wrong format.

<input type="hidden" name="Company[Phone][0][id]"         value="122"                   class="form-control" />
<input type="hidden" name="Company[Phone][0][phone]"  value="3339991111"     class="form-control" />

<input type="hidden" name="Company[Phone][2][id]"         value="125"                   class="form-control" />
<input type="hidden" name="Company[Phone][2][phone]"  value="33"                    class="form-control" />

Phone => {
      0 => {
        id => 122,
        phone => 3339991111,
      },
      2 => {
        id => 125,
        phone => 33,
      },

I dynamically add and delete fields so the situation is common. Maybe there is sense to use array without index (like additional opportunity to use the array). For method 'fromJson' use each next element without index binding.

<input type="hidden" name="Company[Phone][][id]"         value="122"                   class="form-control" />
<input type="hidden" name="Company[Phone][][phone]"  value="3339991111"     class="form-control" />
brainfoolong commented 2 years ago

Hi. I cannot find an error here, can you post what your expected result should be? The object you've posted show exactly the values and names of your given input examples.

Nazar84 commented 2 years ago

If I use fields with name index in 'correct' order like this:

<input type="hidden" name="Company[Phone][0][id]"         value="122"                   class="form-control" />
<input type="hidden" name="Company[Phone][0][phone]"  value="3339991111"     class="form-control" />

<input type="hidden" name="Company[Phone][1][id]"         value="125"                   class="form-control" />
<input type="hidden" name="Company[Phone][1][phone]"  value="33"                    class="form-control" />

I have got the result:

Phone => [
    {
      id => 122,
      phone => 3339991111,
    },{
      id => 125,
      phone => 33,
    },
],

At first example I receive hash of hashes. At this example I have array of hashes.

brainfoolong commented 2 years ago

Hi, this is exactly what should happen. This library does create data out of input names, and when your input names are not "in order", it must create an object instead of a real array, because arrays cannot have "gaps" in the index numbers.

This library does not check or "assume" that the gap should be ignored and make an array out of it. It doesn't matter if the keys are numbered or have any other arbitrary value, it will be an object (hash you call it).

Real arrays only, and really only, can be produced when you have keys starting with zero and with no gaps in between. 0,1,2,3,4 is ok, 0,1,3,4,5 isn't.

You have to create correct names of inputs if you really want an array instead of an object.

It is no bug nor a feature that is supported by this library. It boils basically down to a previous feature request of you, manipulating data before/after the output. You have to do it your own way, when your data/input names is different to the things you need.

Btw. - Working without any key name [] will also not work here, as every empty key [] will increase index number in the result, which is also expected.

Nazar84 commented 2 years ago

Thank You very much!