admin管理员组文章数量:1414614
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:"[email protected]",
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 ?
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:"[email protected]",
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 ?
Share Improve this question asked Sep 10, 2022 at 8:52 Abhishek PoddarAbhishek Poddar 3392 gold badges7 silver badges16 bronze badges1 Answer
Reset to default 3You 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 StackBlitz for a live working example.
本文标签: javascriptHow to show error messages in react yup form validations without formikStack Overflow
版权声明:本文标题:javascript - How to show error messages in react yup form validations without formik - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745165658a2645660.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论