Formik: does this library has support for custom setLocaly in yup

Created on 1 Oct 2017  路  6Comments  路  Source: formium/formik

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

Most helpful comment

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',
    },
  })

All 6 comments

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',
    },
  })
Was this page helpful?
0 / 5 - 0 ratings

Related issues

giulioambrogi picture giulioambrogi  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments

emartini picture emartini  路  3Comments

Jungwoo-An picture Jungwoo-An  路  3Comments

najisawas picture najisawas  路  3Comments