0

Is there a way to make formik field required but not showing no error message?

Even by removing any string from string.required(), Yup stills shows a default error.

 validationSchema={Yup.object().shape({
                FirstName: Yup.string()
                 .required() // msg: FirstName is a required field
Jim_Mcdonalds
  • 426
  • 5
  • 15
  • @UjwalAgrawal by default Yup returns, 'FirstName is a required field' – Jim_Mcdonalds Aug 20 '20 at 03:05
  • If you do not want to show the error on the input field then this is how you can achieve it. https://codesandbox.io/s/formik-example-forked-6hvpl?file=/index.js In this codesandbox example you can see that the input field does not show the error message but you still have the error persisted for you to use while submitting. If this is not what you want then do let me know. – Ujwal Agrawal Aug 20 '20 at 03:16
  • Thanks @UjwalAgrawal, thanks for that but surprisingly, for me even when leaving required() parameter blank shows " is a required field" – Jim_Mcdonalds Aug 20 '20 at 03:25
  • this solution should work. Look at line 66 of index.js file of this code https://codesandbox.io/s/formik-example-forked-6hvpl?file=/index.js and if it's not working for you then could you please share a sample of your code on Codesandbox so that I can have a look at it and hopefully provide a fix for that. Thanks. – Ujwal Agrawal Aug 20 '20 at 03:29

1 Answers1

4

Use .required(" "); a string with space in between will hide the error message.

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
HUGO-DEV
  • 897
  • 8
  • 20