简体   繁体   中英

How can I give an empty material UI text field a value of null, when submitting the form, instead of an empty string?

My problem is that the default value of the text field is zero but when I submit the form the value of the text field is an empty string and I don't want that. I want the value to be zero at the end. How can I implement this?

A part of my JSON:

"basicData": {
 "name": ""
 "partner": ""
 "riskFactor": ""
}

But what i want is this:

"basicData": {
 "name": null
 "partner": null
 "riskFactor": null
}

Maybe there is a more effective way but this was the first solution that came to my mind. I can suggest you convert the empty string to null just before form submit operation.

Object.keys(YourData.basicData).forEach(key => {
  if(YourData.basicData[key] === ""){
    YourData.basicData[key] = null;
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM