指尖上的记忆指尖上的记忆
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub

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");