I am very new to YUP library. I am trying to validate my form using yup.
export const userLogin = yup.object({
email:yup.string().email("Enter valid Email").required("This field is Required"),
password:yup.string().min(5).max(12).required(),
})
const data = {
email:"example@gmail.com",
password:"password"
}
userLogin.isValid(data)
.then((response) =>{
console.log(response) //true
})
Now I am tying to get the error messages which I have mentioned in my schema. how can I get it?
You can use validate
function of yup
library instead of isValid
function like:
userLogin
.validate(data, { abortEarly: false })
.then((responseData) => {
console.log("no validation errors");
console.log(responseData);
setCurrentErrors([]);
})
.catch((err) => {
console.log(err);
console.log(err.name); // ValidationError
console.log(err.errors);
setCurrentErrors(err.errors);
});
You can take a look at this sandbox for a live working example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.