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

Get empty value in the data if use field name with nesting (array) #25

Closed Nazar84 closed 2 years ago

Nazar84 commented 2 years ago

Hi! I suppose I do not understand something. I need data in format:

{
   _Company: {
      name: 'xxx',
      _Phone: [
        {
            phone: 3333,
        }
     ],
  },
}

The 'skipEmpty' is on 'true'. When I used the code without nesting array the empty value (id) was not presented in data: <input type="hidden" name="_Company[_Phone][id]" class="form-control" />

Now I use <input type="hidden" name="_Company[_Phone][0][id]" class="form-control" /> and data has id: , with empty value. Why is that?

brainfoolong commented 2 years ago

Do you have a fully reproducible example code? I cannot reproduce your problem, for me, when i have exactly the input you provided, it is not in the result as expected.

Nazar84 commented 2 years ago

There is the code as it is:

<form action="/multidata" class="js-ajax" data-after-submit="updateForm" id="multiform" method="POST">
    <input id="company_id" name="_Company[id]" type="hidden">
    <p>
        <div class="form-outline" >
            <input class="form-control" id="company_name" name="_Company[name]" type="text" value="Comp1">
            <label class="form-label" for="company_name">Company</label>
        </div>
    </p>

    <input id="company_id" name="_Company[_Phone][0][id]" type="hidden">
    <p>
        <div class="form-outline" >
            <input class="form-control" id="phone_phone" name="_Company[_Phone][0][phone]" type="text" value="22222222">
            <label class="form-label" for="phone_phone">Phone</label>
        </div>
    </p>

    <input class="btn btn-success btn-sm" style="font-size: 15px; font-weight: 600;" type="submit" value="Save">
</form>

And there is the data:

{
  _Company => {
    _Phone => [
      {
        id => ,
        phone => 22222222,
      },
    ],
    name => Comp1,
  },
}
brainfoolong commented 2 years ago

Alright. This was indeed a bug, which should now be fixed with newest bugfix release.

Nazar84 commented 2 years ago

Thank you!