【问题标题】:(Vue) Ant Design display client side as well as server-side validations in ant's form with v-decorator(Vue) Ant Design 使用 v-decorator 以 ant 的形式显示客户端和服务器端验证
【发布时间】:2020-02-02 09:21:42
【问题描述】:

我使用 Ant design 的表单组件和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中实现这一点?

【问题讨论】:

    标签: forms validation vue.js antd


    【解决方案1】:

    您可以使用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;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 2011-10-17
      相关资源
      最近更新 更多