如何使用 Formik 和 yup 显示多个错误行?

ppp*_*ppp 6 javascript reactjs yup formik

我尝试编写正在使用formik和yup验证的多个错误行。使用后this.createError(),它将错误消息转换为字符串而不是数组{errors: '4 errors occurred'}。

我所做的示例代码:

const messages = [
  { key: "length", message: "should have  password at least 8 characters" },
  { key: "notRepeat", message: "should not repeat old password" }
];

this.createError({
  path: `${this.path}`,
  message: messages,
  errors: messages
});
Run Code Online (Sandbox Code Playgroud)

我需要的是错误,我需要它作为消息返回([])康斯坦斯,然后我通过 UI 显示错误列表。

有任何意见或建议来解决这个问题吗?

谢谢。

小智 0

尝试这个:

const errors = messages.map((msg) => ({
  path: this.path,
  message: msg.message,
}));

this.createError(errors);
Run Code Online (Sandbox Code Playgroud)