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
60 stars 10 forks source link

Conversion of a list of element[number] not done when a number is missing #29

Closed basileroutier closed 2 years ago

basileroutier commented 2 years ago

Conversion of a list of element[number] not done when a number is missing When the form contains inputs (e.g.) that want to become arrays in Json and these elements are not numbered properly (e.g. the bug happens if the element looks like this (see below) or contains no [0]). Example :

input type="text" name="localisation[elementRoute][3][nom]" value="1945–1986" /> input type="text" name="localisation[elementRoute][3][nombre]" value="1945–1986" />

input type="text" name="localisation[elementRoute][2][nom]" value="1945–1986" /> input type="text" name="localisation[elementRoute][2][nombre]" value="1945–1986" />

input type="text" name="localisation[elementRoute][11][nom]" value="1945–1986" /> input type="text" name="localisation[elementRoute][11][nombre]" value="1945–1986" />

The result look like this :

{"localisation":{"elementRoute":{"3":{"nom":"1945–1986","nombre":"1945–1986"},"3":{"nom":"1945–1986","nombre":"1945–1986"},"2":{"nom":"1945–1986","nombre":"1945–1986"},"11":{"nom":"1945–1986","nombre":"1945–1986"}}

The result that 'I would like (or normally need to be) :

{"localisation":{"elementRoute":[{{"nom":"1945–1986","nombre":"1945–1986"},{"nom":"1945–1986","nombre":"1945–1986"},{"nom":"1945–1986","nombre":"1945–1986"}}]

It will be great if the "bug" will be fixed for less code (and more 'real' coding =) ). Have a great day

brainfoolong commented 2 years ago

Hi, thanks for reaching out.

Unfortunately, this library is not designed to fix wrong form names. When form names are not named properly in ascending numeric order, they will not become an array.

The library cannot and will never know if this should be an array. Numbers 3,2,11 can even be an ID from your DB for example. In this case you absolutely not want the library to change this in the output.

So, this is absolutely not a bug of this library. This must be fixed in the form names itself. This library will make an array, if you name it in ascending numeric order, starting from 0. Example: 0,1,2

basileroutier commented 2 years ago

Oh okay, it would be (I think), a good idea to add in the configuration a line allowing people (as in my case) to do this without having a name in our database with numbers and to put it in Array format. I would understand if it's hard to do (it's just a feature that would be really nice =D ).

brainfoolong commented 2 years ago

I understand your case, but this library is not intended to be used like that. Everything can be done with code, but this is not the right place for this piece of problem.

I suggest you, when you cannot change your input names, you manipulate the data, that comes out of toJson, afterwards yourself.

Have a nice day.