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

Can not setup values to form #9

Closed KES777 closed 3 years ago

KES777 commented 3 years ago

Describe the bug this does not setup all form values, just client.name:

FormDataJson.fillFormFromJsonValues($("form")[0], {docn : 'asdf', docdate: 'addd', client: { name: 'asdf DDD'  } },
    new FormDataJsonOptions({ unsetAllInputsOnFill: true }) 
)

But this setup correct:

FormDataJson.fillFormFromJsonValues($("form")[0], {docn : 'asdf', docdate: 'addd', client: { name: 'asdf DDD'  } } ) 
)

To mine mind problem is when we go into nested data, then it clear outer form fields

KES777 commented 3 years ago

Next will setup only phone:

FormDataJson.fillFormFromJsonValues($("form")[0], 
  {docn : 'asdf', docdate: 'addd', client: { name: 'asdf DDD', phone: { 1: 'asdf' } } }, 
 new FormDataJsonOptions({ unsetAllInputsOnFill: true }) 
)

source of my html:

<form>
    <input name = "docn">
    <input name = "docdate">

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

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

    <input name = "client[email][]">
    <input name = "client[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[]" checked value="77">
    <input type="checkbox" name="scales[]">
</form>
brainfoolong commented 3 years ago

Thanks for your bug report. I can confirm the bug with unsetAllInputsOnFill = true and nested values. I will fix this.

KES777 commented 3 years ago

seems we must clear that flag when do recursive call https://github.com/brainfoolong/form-data-json/blob/master/src/form-data-json.js#L259

brainfoolong commented 3 years ago

This error should be fixed in the new pre-release.

KES777 commented 3 years ago

fixed