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

v2-dev: toJson return object instead of array: #13

Closed KES777 closed 2 years ago

KES777 commented 2 years ago

Describe the bug

    <input name = "client[][phone][]">
    <input name = "client[][phone][]">
    <input name = "client[][phone][]">

    <input name = "client[][email][]">
    <input name = "client[][email][]">

    <input name = "client[][phone][]">
    <input name = "client[][phone][]">

    <input name = "client[][email][]">
    <input name = "client[][email][]">
    <input name = "client[][email][]">

To Reproduce

FormDataJson.toJson( $('form') )

image

Here I get 10 array elements as expected (green), also first phone is pushed into array as expected, but next phone (second, third) are objects first email is array as expected, but next email (second) is object

for second,third email/phone I expect arrays

**probably this example is meaningless, because this is unusual to create email, phone objects at separate array elements. They should be grouped together semantically. So for this case warning should be issued.

**despite on unusual naming objects are not expected, e.g. counter should be flushed, when [] is processing
To my mind it should be just command to push into array and counter should not have matter. For cases when it has matter developer will use explicit value: [1] or [7] etc. (see post below)

KES777 commented 2 years ago

For this case it works fine:

    <input name = "client[0][phone][]">
    <input name = "client[0][phone][]">
    <input name = "client[0][phone][]">

    <input name = "client[0][email][]">
    <input name = "client[0][email][]">

    <input name = "client[1][phone][]">
    <input name = "client[1][phone][]">

    <input name = "client[1][email][]">
    <input name = "client[1][email][]">
    <input name = "client[1][email][]">

image

KES777 commented 2 years ago

Also, probably, index and key usage on same level is semantic error, so warning also should be issued:

    <input name = "client[name]">
    <input name = "client[0][phone][]">

probably more correct way to write:

    <input name = "client[name]">
    <input name = "client[edrpou]">

    <input name = "client[person][0][phone][]">
    <input name = "client[person][0][phone][]">
    <input name = "client[person][0][phone][]">

    <input name = "client[person][0][email][]">
    <input name = "client[person][0][email][]">

    <input name = "client[person][1][phone][]">
    <input name = "client[person][1][phone][]">

    <input name = "client[person][1][email][]">
    <input name = "client[person][1][email][]">
    <input name = "client[person][1][email][]">

NOTICE: Here I add person key image

brainfoolong commented 2 years ago

Thanks for report.

Case 1 https://github.com/brainfoolong/form-data-json/issues/13#issue-1008086845

It is a bug. It's probably wrong in sematics, but not technically. Genrally, i go the same way as PHP does. I think PHP does a pretty good job of converting any form, doesn't matter how corrupt it is, to multidimensional array data.

This library should not be responsible for semantic warning. It is another topic.

So, case 1 will be fixed by fixing the auto increments internally.

Case 2 https://github.com/brainfoolong/form-data-json/issues/13#issuecomment-927836476

For me

<input name = "client[name]">
<input name = "client[0][phone][]">

Is also valid. 0 as a key as any other, in Javascript and in any other programming language. Only empty keys [] are handled specially.

So this example should return {"client":{"0":{"phone":[""]},"name":""}}

For sure, your second example is more correct, if you need it like that.

KES777 commented 2 years ago

I agree with case 2. No problem with:

<input name = "client[name]">
<input name = "client[0][phone][]">
brainfoolong commented 2 years ago

Fixed Case 1 in 2.0.3beta

KES777 commented 2 years ago

work as expected, thank you