vee_validate将API返回的错误渲染到指定File上:
示例代码:
const { handleSubmit, setFieldError, setErrors } = useForm();
const onSubmit = handleSubmit(async values => {
// Send data to the API
const response = await client.post('/users/', values);
// all good
if (!response.errors) {
return;
}
// set single field error, 这个是我需要的
if (response.errors.email) {
setFieldError('email', response.errors.email);
}
// set multiple errors, assuming the keys are the names of the fields
// and the key's value is the error message, 这个还没试过,但是应该很好用,相当于 laravel的form验证错误渲染了
setErrors(response.errors);
});
参考:https://vee-validate.logaretm.com/v4/guide/composition-api/handling-forms/ 下的 Setting Errors Manually 部分
开始我是这样:
email.errorMessage.value = "User account already exists";
没有生效,应该:
setFieldError("email", "User account already exists");
