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: `skipEmpty` still includes empty arrays/objects #16

Closed KES777 closed 2 years ago

KES777 commented 2 years ago

Describe the bug When nested form fields are not filled, they are included anyway

To Reproduce

    <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][]">
    <select name="client[status][]" id="cars" multiple>
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
    </select>

    <input type="checkbox" name="scales[]">
    <input type="checkbox" name="scales[]" checked value="77">

image

KES777 commented 2 years ago

Empty multiselect is returned even if skipEmtpy is true

image

EDIT
I am not sure, but array is not correspond to flat image

Should be here something like [ 'client[status][]', 'saab', 'client[status][]', 'opel' ]? similar to how it was flattened by browser: image

KES777 commented 2 years ago

And at this case not all values are returned. I expect array of email and phone values (html code is above):

image

Arrays are expected, because there are three fields with client[person][0][phone][] name, so array of 3 empty string elements should be returned. Same rule for other similar fields (please see next post) image

example for comparison how data is returned without either options: image

KES777 commented 2 years ago

Not all form values are returned when flatList is true

image

Expected: When flatList there array of fields with theirs values should be returned:

[
   'client[person][0][phone][]', 'yy',
   'client[person][0][phone][]', 'zz',
]

And vice-verse we can supply array field names with theirs values to .fromJson

brainfoolong commented 2 years ago

Thx for report.

skipEmpty now works in 2.0.3beta - All empty (empty array, empty fields) are deleted before output flatList now works in 2.0.3beta as suggested, same keys as native FormData would have

KES777 commented 2 years ago

Why array of arrays instead of even sized array of key, value? I do not know where this already used, but it seems overkill.

In any case items with empty value should not be returned: docn, docdate image

brainfoolong commented 2 years ago

The flatList now is considered to reproduce FormData behaviour. Key 0 is always the original input name, Key 1 is always the value of the input. If it is a multiple field, than it's for sure an array value.

Combination of flatList and skipEmpty still seems buggy, i'll look into that.

brainfoolong commented 2 years ago

Combination of flatList and skipEmpty is fixed in latest commit.

KES777 commented 2 years ago

No names for fields are returned:

image

brainfoolong commented 2 years ago

Thanks. Lol. I need a break :) I come to you when i have something new.

brainfoolong commented 2 years ago

Ok, there was a glitch, this should be fixed in latest commit in v2 (No release created yet)

KES777 commented 2 years ago

working well, thank you ;-)

brainfoolong commented 2 years ago

Nice. Thx.