Home > Mobile >  How to change default error message in Yup
How to change default error message in Yup

Time:06-27

How to change default error message to custom error message: my custom message

const VALIDATION_SCHEME = Yup.object().shape({
      numOne: Yup.Number().required('!'),
      numTwo: Yup.Number()
        .required('!')
        .when('numOne', ((numOne, schema) => schema.max(numOne - 1)))
    });

CodePudding user response:

You need to use moreThan hook to perform the validation you want as below.

const validationSchema = yup.object({
  numOne: yup.string().required("Custom 2"),
  numTwo: yup
    .number()
    .lessThan(yup.ref("numOne"), "Your Custom Message")
    .max(yup.ref("numOne"))
});

Working example - https://codesandbox.io/s/peaceful-cache-k2g4lc?file=/index.js

CodePudding user response:

alternative:

numOne: yup.number()
  .required("1")
  .moreThan(yup.ref("numTwo"), "This is 1"),
numTwo: yup.number()
  .required("2")
  .lessThan(yup.ref("numOne"), "This is 2")
  • Related