I tried to defined a custom setlocale for custom error messages, but always get the default ones defined in yup, I defined something like this, before my validation schema:
setLocale({
string: {
required: 'Requerido',
},
})
//schema
validationSchema: yup.object().shape({
password: yup.string().required(),
}),
The idea is define a global config error message to avoid doing the same in each validationschema like the example file:
validationSchema: Yup.object().shape({
email: Yup.string()
.email('Invalid email address')
.required('Email is required!'),
}),
Thank you this is an awesome Lib
Havent tried this. Can you setup a codesanbox?
@yanv1991 you need to import Yup after you've run setLocale.
Personally how I've done it is have a setYupLocale.js file which I import in App.js before any components that depend on Yup
// setYupLocale.js
import { setLocale } from 'yup/lib/customLocale';
setLocale({
mixed: {
required: 'Required'
}
});
I put setLocale in my app.js file and my yup imports are declared in children components but didn't work.Thanks
cc @jquense
what's your suggestion here? I would love to add this to Formik docs as a recipe.
with v0.23 (along with sync validation!) setting the locale is not import order dependent so should be a problem any more :)
Just a heads up to @yanv1991, your code wasn't working because there isn't a required locale for string.
You have to use mixed:
setLocale({
mixed: {
required: 'Requerido',
},
})
Most helpful comment
Just a heads up to @yanv1991, your code wasn't working because there isn't a
requiredlocale forstring.You have to use
mixed: