redlightfilms
redlightfilms

Reputation: 233

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
}

Upvotes: 0

Views: 1985

Answers (1)

mafirat
mafirat

Reputation: 287

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;
  }
})

Upvotes: 1

Related Questions