gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:37:09 25 4
gpt4 key购买 nike

使用 Vuelidate,您可以使用 this.$v.$reset() 重置验证错误。 .在这个 Codepen example重置 lastName使用 Vuetify 组件的字段有效 - $invalid为真,而 $error设置为假。

重置 firstName 的常规文本输入时它不能作为 $error flag 仍然为真。如何修改文本输入,以便在调用重置时 $error 为假?

我也试过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, maxLength: maxLength(2)
},
}
}
})
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>

最佳答案

在您的示例中,您使用的是 oninputonblur HTML 属性,但在 Vue 中,您应该使用 @input ( v-on:input ) 和 @blur ( v-on:blur ) 绑定(bind)。见 docs详情。
用 Vue 绑定(bind)替换 HTML 属性使您的示例正常工作:

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, maxLength: maxLength(2)
},
}
}
})
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%"
@input="$v.form.firstName.$touch()"
@blur="$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>

关于vue.js - Vuelidate 重置特定字段,以便 $error 标志为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59986139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com