bietkul / react-reactive-form

Angular like reactive forms in React.
MIT License
309 stars 32 forks source link

Multipart form data #2

Closed ryan-ds17 closed 6 years ago

ryan-ds17 commented 6 years ago

Does this work with multipart form data?

bietkul commented 6 years ago

I think that it's not a big deal. You can do something like that.

var form_data = new FormData();

for ( var key in myForm.value ) {
    form_data.append(key, item[key]);
}
ryan-ds17 commented 6 years ago

Yes it works. Thank you

I had another doubt. When I use input type="number" for a form control I have, when I submit the form, that value is changed to a string. How can I prevent this from happening?

bietkul commented 6 years ago

Actually input type="number" always returns a string. Check it here.

So, if you want to use the value as a number instead of string then you can use parseInt to parse the string as a number.

Btw, I'm planning to add normalizers & parsers in next version.

ryan-ds17 commented 6 years ago

Where would I use parseInt?

bietkul commented 6 years ago

If you're using multipart form data then you don't need to parse because .append will change the number to string. If you really want to do that then you can do something like that before submitting the form values.

const formData = {};
Object.keys(myForm.value).forEach((name) => {
   let value = myForm.value[name];
   if(name === "age") {
      value = parseInt(value, 10);
   }
  formData[name] = value;
});
bietkul commented 6 years ago

This issue is not active since a long time.