Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty input type=number results in NaN #284

Closed
vuhrmeister opened this issue Dec 2, 2017 · 6 comments
Closed

Empty input type=number results in NaN #284

vuhrmeister opened this issue Dec 2, 2017 · 6 comments

Comments

@vuhrmeister
Copy link

I'm using Formik with Yup Schema validation.

I have an input field of type "number". If I enter a value and empty the field this results in a NaN. This behavior might be as expected. For that I added a transform function to my schema like that:

yup
.number()
.transform(value => {
  console.log(value, Number.isNaN(value) ? 0 : value 
  return Number.isNaN(value) ? 0 : value
})

Should it respect the transform function? Or is the schema actually only be used for validation, not for "cleaning" the data? If so, is there a way how to solve that? Maybe I'm missing something.
Of course, I can check the value before attaching it to the input field. But the value in the global values object stays the same.

@sepehr500
Copy link

From what I understand, yup can only used for validation. Any kind of cleaning you try to do in yup will not effect the values object that formik manages internally.

@markuspelnens
Copy link

markuspelnens commented Dec 9, 2017

Hi @vuhrmeister, did you manage to solve it? I'm running into the same problem. Until I find a permanent solution, I ended up implementing this workaround:

value={values.quantity ? values.quantity : "" }

@vuhrmeister
Copy link
Author

@markuspelnens nope, used the same workaround (but checking for Number.isNaN()).

@timc13
Copy link
Contributor

timc13 commented Jan 22, 2018

is there any reason for this? It seems like we should be able to use replace the form values with the values returned by Yup validate(). If we're allowing integration with Yup, why not integrate all the way? Yup transforming is very powerful and it would be great if it "just worked"

@EmirGluhbegovic
Copy link

I am having the same issue, has this been fixed or is there some recommended way of dealing with this?

@nucab
Copy link

nucab commented Aug 3, 2018

try to use setFieldValue

onChange={e => setFieldValue('condition.operand', +e.target.value)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants