首页 > 解决方案 > 如何使用 yup 和 react-hook-form 逐步验证?

问题描述

    const schema = yup.object().shape({
      email: yup
        .string()
        .required()
        .email('wrong email.')
        .test('unique email', 'already using', async (value) => {
          try {
            const res = await axios.post('/accounts/emailcheck', { email: value });
            return res.data.possible;
          } catch (err) {
            console.error(err);
          }
        }),
  })

    const { register, handleSubmit, formState: { errors } } = useForm({ mode: 'onBlur', resolver: yupResolver(schema) });

我只想使用 axios(需要,电子邮件)是可以的。但它始终适用于Blur。

如何使用 yup 和 react-hook-form 逐步验证?

标签: axiosreact-hook-formyup

解决方案


推荐阅读