使用 Vuetify 表单验证,未显示验证错误消息。
我正在使用 Vue.js 、 Vuetify 和 TypeScript 。我试图验证一个表单,但没有显示我的验证消息。尽管该字段正在变为红色。我还可以看到错误函数被调用
<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>
我不确定为什么我看不到验证错误消息。
原文由 Fakeer 发布,翻译遵循 CC BY-SA 4.0 许可协议
假设您的规则在控制台中记录了正确的错误消息,我看不出您的代码有任何问题,但这是我正在处理的项目中的一个工作示例。
规则在这里