forms - (Vue) Ant Design 使用 v-decorator 以 Ant 的形式显示客户端和服务器端验证

标签 forms validation vue.js antd

我用过 Ant 设计的表单组件与 v-decorators用于验证表单,我想显示客户端(我目前已完成的 v-decorator 规则验证)以及服务器端表单数据验证。

将此视为示例登录表单:

<template>
  <AForm
    :form="form"
    @submit.prevent="handleSubmit"
  >
    <FormItem>
      <AInput
        v-decorator="['email', { rules: [{ required: true, message: 'Please input your email!' }] }]"
        placeholder="Email"
      />
    </FormItem>
    <FormItem>
      <AInput
        v-decorator="['password', { rules: [{ required: true, message: 'Please input your Password!' }] }]"
        placeholder="Password"
        type="password"
      />
    </FormItem>
    <FormItem>
      <AButton
        html-type="submit"
        class="w-full"
        :loading="loading"
      >
        Log in
      </AButton>
    </FormItem>
  </AForm>
</template>

<script>
import { Form, Input, Button } from 'ant-design-vue';
import { mapActions } from 'vuex';

export default {
  components: {
    AForm: Form,
    FormItem: Form.Item,
    AInput: Input,
    AButton: Button,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      errors: {},
      loading: false,
    };
  },
  methods: {
    ...mapActions(['login']),
    handleSubmit() {
      this.errors = {};
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loading = true;
          this.login(values)
            .then(() => {
              this.$router.push('/');
            })
            .catch(({ response = {}, message }) => {
              const { errors } = response.data;
              if (errors) {
                this.errors = errors; // I want to display these errors
                return;
              }
              this.$notify.error(message || 'Unable to login');
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
  },
};
</script>

我已经将表单数据提交到 Laravel 服务器,最终我会得到一些验证错误,我需要将这些错误显示到 ant 的表单中。我的验证错误对象如下所示:

{
    errors: {
        email: "The email must be a valid email address.",
        password: "(some validation message here)"
    }
}

我不想失去 ant 的表单验证功能,我还想同时显示服务器端验证错误。有什么办法可以在 vue 中实现这一点吗?

最佳答案

您可以使用 setFields设置错误状态的表单方法。

this.login(values)
.then(() => {
    this.$router.push('/');
})
.catch(({
        response = {},
        message
    }) => {
        const {
            errors
        } = response.data;
        if (errors) {
            this.form.setFields({
                'email': {
                    errors: [{
                        "message": errors.email,
                        "field": "email"
                    }]
                },
                'password': {
                    errors: [{
                        "message": errors.password,
                        "field": "password"
                    }]
                }
            });
            return;
        }

关于forms - (Vue) Ant Design 使用 v-decorator 以 Ant 的形式显示客户端和服务器端验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60024729/

相关文章:

javascript - 提交表格并在不同的页面上阅读

javascript - 电话验证 : How To Overwrite Text In a Textbox When Typing Without Clearing Whole Field?

javascript - 我们应该使用 v-model 来修改 Vuex store 吗?

mysql - Laravel 验证 MSSQL

javascript - 使用 can.Map.Validate 验证动态对象

html - 是否可以有条件地渲染布局?

angular - 像 AngularJS 中的作用域注入(inject),用于更现代的库

javascript - AngularJS 表单拒绝验证提交

javascript - 将 html 表单数据保存到文件

php - 在单独的页面上将数据从一种形式传递到另一种形式