question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question about validation - Casting an input value to 'number'

See original GitHub issue

I have an input that is supposed to contain a number. If I set the validation schema for this field to be yup.number().required(), when I go to edit the field it complains, likely since it is treating the input value as a string. I see from the yup docs that they have a cast method, but I’m not sure how to have formik attempt to cast values before/during validation.

Is there something I’m missing? Am I meant to manually specify this cast in mapValuesToPayload?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
agoscommented, Jan 28, 2019

input type number works, but is not available for select and, in general, for custom inputs

4reactions
johnromcommented, Sep 17, 2019

I’d recommend doing something similar to @davidspiess above, except you can create a composable callback like this:

const stringToNumberConverter = stringValue => parseInt(stringValue, 10);
const parseAndHandleChange = (handleParse, handleChange) => event => {
    const value = handleParse(event.target.value);

    handleChange(event.target.name)(value);
}
const MyForm = () => {
    <Formik /* etc */>
        {(formikProps) => (
            <Field 
                name="myNumberField" 
                onChange={parseAndHandleChange(stringToNumberConverter, formikProps.handleChange)} 
            />
        )}
    </Formik>
}

FYI in v2 there are possible plans for an onParse-like API. Hopefully that will make things easier #1525

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Tip: Validating user input as number (Integer)
To do so you can use the input() function: e.g. username=input("What is your username?"
Read more >
<input type="number"> - HTML: HyperText Markup Language
<input> elements of type number are used to let the user enter a number. They include built-in validation to reject non-numerical entries.
Read more >
Formik input value for a number becomes a string even after it ...
When the price is entered, any non-number is met with a warning. However, when I console log the the value passed to the...
Read more >
Check user Input is a Number or String in Python - PYnative
Solution: In such a situation, We need to convert user input explicitly to integer and float to check if it's a number. If...
Read more >
Why the number input is the worst input - Stack Overflow Blog
When the number input contains an invalid value and you retrieve the value, you get a blank string. · Valid numbers include more...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found