setFieldError not working as expected.

See original GitHub issue

🐛 Bug report

Current Behavior

Currently I want to validate a hostName URL using a function that I have and set errors accordingly.

here is the component on which I want to set the errors to -

<TextField
        name="github"
        value={values.github}
        onChange={event => handleLinkChange(event, 'github', 'github.com')}
        label="Please link your Github account"
        error={
          errors.github &&
          touched.github && {
            [errors.github]: true,
          }
        }
      />

and here is the handleLinkChange function that I use -

const handleLinkChange = ({ target: { value } }, name, hostName) => {
          setFieldValue(name, value);
          setFieldTouched(name, true);
          if (!isValidHostURL(hostName, value)) {
            console.log(name);
            setFieldError(name, `Enter a valid ${name} URL`);
          }
        };

the function is working fine as it should but the error is not being set accordingly.

Expected behavior

The error should be set and shown in the field

Reproducible example

Suggested solution(s)

Additional context

I am also using Yup for form validation schema and have set github to Yup.string().required() in the Yup.object().shape({}) along with the other fields

Your environment

Software Version(s)
Formik 1.4.2
React 16.6.3
TypeScript -
Browser chrome 69
npm/Yarn npm 6.4.1
Operating System Windows 10 1709

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

31reactions
jaredpalmercommented, Feb 6, 2019

setFieldError should not be used in change handlers because validation will not be aware of it during submission. The only real use case for setFieldError is inside of onSubmit where you want to manually set a field error. You should move this logic to validate instead.

14reactions
vaibhavhrtcommented, Feb 15, 2020

@jaredpalmer what if on value change (or field blur) I want to immediately make an API call to check if value is valid and set error then, so that users can see errors and correct them before submitting form. A common use case is to check if username is available in signup forms.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why I'm unable to set error to formik field using setFieldError ...
Actually the problem was with the way I was calling the method. setFieldError and setErrors both are async and I was calling them...
Read more >
useForm - VeeValidate
Sets a field's error message, useful for setting messages form an API or that are not available as a validation rule. Setting the...
Read more >
<ErrorMessage /> | Formik
It expects that all error messages are stored for a given field as a string. ... If not specified, <ErrorMessage> will just return...
Read more >
How to use setFieldError in ListGrid - SmartClient Forums
Hi, I tried to use the method setFieldError in a ListGrid, but it seems not to work like I expected. I wrote a...
Read more >
[Solved]-Formik - Show ErrorMessage on Blur-Reactjs
... you've already done the work in the wrapper /> ); export default () => { return ... Formik setFieldError does not show...
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