我将以下代码用作模式
schema = yup.object().shape({
name: yup.string().required('Required Field in name'),
industry: yup.string().required('Required Field in industry'),
address: yup.string().required('Required Field in address'),
crn: yup.number('Number required'),
});
我具有以下状态值
name: {
value: '',
isRequired: true,
hasError: false,
order: 1,
errorMessage: '',
},
industry: { value: '', isRequired: true, hasError: false, order: 2 },
address: { value: '', isRequired: true, hasError: false, order: 3 },
crn: {
value: '',
isRequired: true,
hasError: false,
order: 4,
errorMessage: '',
},
website: { value: '', isRequired: true, hasError: false, order: 5 },
employeesNbr: { value: '', isRequired: true, hasError: false, order: 6 },
我正在按如下方式使用yup的.validate函数
this.schema
.validate(
{
name: profile.name.value,
industry: profile.industry.value,
address: profile.address.value,
crn: profile.crn.value,
},
{ abortEarly: false },
)
.catch(err => {
console.log(err);
});
我需要.catch函数的帮助,例如如何提取错误的路径,以便可以将它们连接到errorMessages的状态。 通常可以访问错误的路径名,它会变成err.inner [0] .pathname,err.inner [1] .pathname 接线应采用这种方式
profile[err.inner[x].path].hasError = true;
profile[err.inner[x].path].errorMessage = err.inner[x].message
对于在catch中创建循环算法的帮助将不胜感激