【问题标题】:Default browser form validation not working in Vuetify v-form默认浏览器表单验证在 Vuetify v-form 中不起作用
【发布时间】:2020-05-23 14:49:10
【问题描述】:

我想利用浏览器的默认表单验证,因为我将在我的服务层处理更深入的验证。我需要的唯一验证是此示例的电子邮件和电子邮件。

我正在使用此处描述的示例但没有成功。 requiredemail 属性将被忽略。

<template>
  <v-row justify="center">
    <v-dialog v-model="visible" persistent max-width="600px">
      <v-form ref="userForm" @submit.prevent="handleSubmit">
      <v-card>
        <v-card-title>
          <span class="headline">Adding User</span>
        </v-card-title>
        <v-card-text>
          <v-container>
            <v-row>
              <v-col cols="12">
                <v-alert
                  v-if="error"
                  border="left"
                  colored-border
                  type="error"
                  elevation="2"
                >{{error}}</v-alert>
              </v-col>
              <v-col cols="12">
                <v-text-field label="Name" required v-model="user.name" />
              </v-col>

              <v-col cols="12">
                <v-text-field label="Email" required type="email" v-model="user.email" />
              </v-col>

              <v-col cols="12">
                <v-text-field required label="Password" type="password" v-model="user.password" />
              </v-col>
            </v-row>
          </v-container>
        </v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="blue darken-1" text @click="onCancel">Close</v-btn>
          <v-btn color="blue darken-1" type="submit">Save</v-btn>
        </v-card-actions>
      </v-card>
      </v-form>
    </v-dialog>
  </v-row>
</template>

<script>

export default {
  name: "AddUser",
  data() {
    return {
      user: {}
    };
  },
  props: {
    error: {},
    permissions: {},
    visible: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleSubmit() {
      // This is executed on submit but no validation errors are shown

      // this.$refs.form.checkValidity() // throws an error on checkValidity not being a function
      this.$emit("onConfirmed", this.user);
    },
    onCancel() {
      this.$emit("onCancelled");
    }
  },
  created: function() {}
};
</script>

【问题讨论】:

    标签: html vue.js vuetify.js


    【解决方案1】:

    您必须在 vuetify 中使用规则进行验证,如 documentation 中所述

    <v-text-field
       v-model="email"
       :rules="[rules.required, rules.email]"
       label="E-mail"
    ></v-text-field>
    
    <script>
    export default {
    data () {
      return {
        email: '',
        rules: {
          required: value => !!value || 'Required.',
          email: value => {
            const pattern = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
            return pattern.test(value) || 'Invalid e-mail.'
          },
        },
      }
    },
    </script>
    

    【讨论】:

    • 几个问题 - this.$refs.form.validate() 是初始化验证检查的常用方法吗?在 Vue 项目中,你会在哪里全球化像 rules.required 这样的规则?
    • 说实话,我使用 'vuelidate' 包进行复杂的验证。为了使其全球化,我建议使用 vuex 商店或制作一个 utils.js 并将其导入到您的 vue 组件中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 2019-07-14
    • 2020-10-18
    • 2021-09-08
    • 2019-07-05
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多