使用Vuelidate,您可以使用this.$v.$reset()
重设验证错误。在此Codepen example中,重置使用Vuetify组件的lastName
字段是有效的-$invalid
为true,而$error
设置为false。
在重置firstName
的常规文本输入时,由于$error
标志仍然为true,因此无法正常工作。如何在调用reset时修改文本输入,使$ error为false?
我也尝试过this.$nextTick(() => {...})
,但这也不起作用。
Vue.use(window.vuelidate.default)
var validationmixin = window.vuelidate.validationmixin
const {
maxLength,required
} = window.validators
new Vue({
el: '#app',mixins: [validationmixin],data: () => ({
form: {
firstName: '',lastName: ''
}
}),validations: {
form: {
firstName: {
required,maxLength: maxLength(2)
},lastName: {
required,}
}
})
input.raw {
border: solid;
}
.is-invalid {
border-color: #FF5252 !important;
}
<html>
<head>
<script src="https://unpkg.com/vuelidate@0.6.1/dist/validators.min.js"></script>
<script src="https://unpkg.com/vuelidate@0.6.1/dist/vuelidate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
</head>
<body>
<div id="app">
<label for="firstName">First Name</label>
<input
v-model="form.firstName"
id="firstName"
class="raw"
:class="{ 'is-invalid': $v.form.firstName.$error }"
type="text"
width="100%"
:oninput="$v.form.firstName.$touch()"
:onblur="$v.form.firstName.$touch()"
/>
<button @click="$v.form.firstName.$touch()">
$touch
</button>
<button @click="$v.form.firstName.$reset()">
$reset
</button>
<pre>{{ $v.form.firstName }}</pre>
</div>
</body>
</html>