typescript - Vuetify表单验证,验证错误消息未显示

标签 typescript vue.js vuetify.js

使用 Vuetify 表单验证,未显示验证错误消息。

我正在使用 Vue.js , Vuetify typescript .我试图验证表单,但我的验证消息没有显示。尽管该领域正在变成红色。我还可以看到错误 func 被调用


<template>
  <v-layout row justify-center>
    <v-dialog v-model="show" persistent max-width="600px">
      <v-card>
        <v-card-title>
          <span class="headline">Testing validations</span>
        </v-card-title>
        <v-card-text>
          <v-container grid-list-md>
            <v-form v-model="form.valid" ref="form">
              <v-layout wrap>
                <v-flex xs12>
                  <v-text-field
                    label="Checklist Name"
                    v-model="list.name"
                    :rules="[errorFunc]"
                    name="checklist"
                  ></v-text-field>
                </v-flex>
              </v-layout>
            </v-form>
          </v-container>
        </v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="blue darken-1" flat @click="add">Save</v-btn>
          <v-btn color="blue darken-1" flat @click="show = false">Close</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  </v-layout>
</template>
<script lang="ts">
  import { Component, Prop, Vue } from "vue-property-decorator";

  @Component({
    components: {}
  })
  export default class listForm extends Vue {
    @Prop() visible!: boolean;

    form = {
      valid: false
    };

    list = {
      name: ""
    };

    errorFunc() {
      let val: any;
      if (this.list.name.length >= 3) {
        val = true;
      } else {
        val = "Errorrrr";
      }
      console.log(val);
      return val;
    }

    get show() {
      return this.visible;
    }
    set show(value) {
      if (!value) {
        this.$emit("close");
      }
    }
    add() {
      if (this.$refs.form.validate()) {
        this.$emit("save", this.list);
      }
    }
  }
</script>

我不知道为什么我看不到验证错误消息。

最佳答案

假设您的规则在控制台中记录了正确的错误消息,我看不出您的代码有任何问题,但这是我现在正在处理的项目中的一个工作示例。

  <v-text-field 
    v-model="data.editedItem.email" 
    label="Email" 
    :rules="[data.rules.required, data.rules.email]" 
  </v-text-field>

规则在这里
  data: {
    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) || "Must be a valid e-mail.";
      }
    }
  }

Error message
Vue tab in Dev tools
DOM of the entire v-text-field

关于typescript - Vuetify表单验证,验证错误消息未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54240816/

相关文章:

angular - Ionic 3 Firebase 电话身份验证不起作用

angular - Jasmine:Matcher 不同于 undefined 和 null (!= undefined and != null)

css - 如何从 vue-cli webpack-simple 元素中提取 css 文件?

javascript - Lightgallery 只能打开一次(VueJs)

vue.js - 如何使用vuetify使页脚与内容宽度相同?

frontend - 使用 Laravel 进行 Vuetify

vue.js - Vuelidate 重置特定字段,以便 $error 标志为 false

typescript - TypeScript 中的通用类转换

node.js - Passport-local-mongoose serializeUser 类型不正确

webpack - 如何使用 vue-cli 安装另一个不是最新的 vue 版本