我有两个要验证的字段,但在我提交后总是在方法validateBeforeSubmit()中返回true。
Whitout按钮提交它很好用并以优秀的语言返回errors.first(name)
我做错了什么?
App.vue:
<form @submit.prevent="validateBeforeSubmit">
<form-input v-on:input="handleTitle" :validate="'required|email'" label="email" labelvalue="email" type="text" placeholder="" name="email" :value="title" classname="form-control" id=""></form-input>
<form-input v-on:input="handleLink" :validate="'required'" label="Link" labelvalue="Link" type="text" placeholder="" name="link" :value="link" classname="form-control" id=""></form-input>
methods: {
validateBeforeSubmit() {
this.$validator.validateAll().then((result) => {
if (result) {
// eslint-disable-next-line
alert('Form Submitted!');
return;
}
alert('Correct them errors!');
});
},
Input.vue:
<template>
<div>
<div class="form-group">
<span>{{ errors.first(name) }}</span>
<label v-if="label" :for="label" v-html="labelvalue"></label>
<input v-validate="validate" v-on:input="updateValue($event)" :type="type" :placeholder="placeholder" :name="name" :value="value" :class="classname" :id="id">
</div>
</div>
</template>
export default {
props: {
validate: String,
type: String,
placeholder: String,
name: String,
value: String,
classname: String,
id: String,
label: String,
labelvalue: String
},
methods: {
updateValue: function (evt) {
this.$emit('input', evt)
}
}
}
答案 0 :(得分:2)
validateAll
不会查看子组件。您必须注入父验证程序。在inject: ['$validator']
文件中添加Input.vue
。它应该解决问题。导出块看起来像这样
export default {
inject: ['$validator'],
props: {
validate: String,
type: String,
placeholder: String,
name: String,
value: String,
classname: String,
id: String,
label: String,
labelvalue: String
},
methods: {
updateValue: function (evt) {
this.$emit('input', evt)
}
}
}
有关inject
的更多信息,请查看this reference