我正在使用vee验证表单验证。我正在使用vue transition显示验证错误,如下所示:
<input type="text" name="name" v-validate="'required'">
<transition name="slide-fade">
<div v-show="errors.has('name')">{{ errors.first('name') }}</div>
</transition>
的CSS:
.slide-fade-enter-active {
transition: all .3s ease;
}
.slide-fade-leave-active {
transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-enter, .slide-fade-leave-to
/* .slide-fade-leave-active below version 2.1.8 */ {
transform: translateX(10px);
opacity: 0;
}
现在,当输入错误时,转换正在进行,但是当错误消失时,转换不起作用。 为什么会这样?
答案 0 :(得分:2)
转型正在发挥作用。你看到它突然空白,因为没有errors.first('name')
可以显示了。
当你在errors.first('name')
添加其他字符串时,比如说Error:
,就会变得清晰。
Vue.use(VeeValidate);
new Vue({
el: '#demo'
})
.slide-fade-enter-active {
transition: all .3s ease;
}
.slide-fade-leave-active {
transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-enter, .slide-fade-leave-to
/* .slide-fade-leave-active below version 2.1.8 */ {
transform: translateX(10px);
opacity: 0;
}
<script src="https://unpkg.com/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vee-validate@latest/dist/vee-validate.js"></script>
<div id="demo">
Type something and then erase. And then type again.
<input type="text" name="name" v-validate="'required'">
<transition name="slide-fade">
<div v-show="errors.has('name')">Errors: {{ errors.first('name') }}</div>
</transition>
</div>
考虑额外代码的数量,但如果您真的必须这样做,则必须在input
的值中添加监视,并记录第一个错误当它发生时。
vee-validate没有为erros提供监听器,所以这和目前一样好。见下面的演示。
Vue.use(VeeValidate);
new Vue({
el: '#demo',
data: {
name: "Erase me",
nameError: ""
},
watch: {
name() {
// two ticks at least, because vee-validate takes one tick to validate
Vue.nextTick().then(Vue.nextTick()).then(() => {
if (this.errors.has('name')) { this.nameError = this.errors.first('name'); }
});
}
}
})
.slide-fade-enter-active {
transition: all .3s ease;
}
.slide-fade-leave-active {
transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-enter, .slide-fade-leave-to
/* .slide-fade-leave-active below version 2.1.8 */ {
transform: translateX(10px);
opacity: 0;
}
<script src="https://unpkg.com/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vee-validate@latest/dist/vee-validate.js"></script>
<div id="demo">
Erase and error will appear. Type again and error will leave.<br>
<input type="text" name="name" v-validate="'required'" v-model="name">
<transition name="slide-fade">
<div v-show="errors.has('name')">Errors: {{ nameError }}</div>
</transition>
</div>
答案 1 :(得分:1)
这有点老了,但我想出了一个更简单的解决方案,对于仍在搜索的每个人,只需最少的代码即可使用。
这是在每次您按下输入键(@keypress
事件)时检查错误消息,将其保存到变量中,该变量与div中的实际错误消息一起显示,使用OR,请确保error.message
变量永远不会获得undefined
。即使无法看到实际错误(由VeeValidate给出的错误),OR也可以保证将显示某些内容,直到动画完成为止。
Vue.use(VeeValidate);
new Vue({
el: '#demo',
data() {
return {
error: {
name: null,
},
}
},
methods: {
updateError() {
const message = this.errors.first('name');
if (message) { // check if message isn't undefined
this.error.name = message;
}
},
},
});
.slide-fade-enter-active {
transition: all .3s ease;
}
.slide-fade-leave-active {
transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-enter,
.slide-fade-leave-to {
transform: translateX(10px);
opacity: 0;
}
<script src="https://unpkg.com/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vee-validate@latest/dist/vee-validate.js"></script>
<div id="demo">
Type something and then erase. And then type again.
<input type="text" name="name" v-validate="'required'" @keypress="updateError()"><!-- added keypress event -->
<transition name="slide-fade">
<div v-show="errors.has('name')">Errors: {{ errors.first('name') || error.name }}</div><!-- OR -->
</transition>
</div>
如果您有多个输入,则可以执行以下操作:在updateError()
函数(成为updateError($event)
)中传递'$ event'变量,并在声明中接受它。然后,使用srcElement
获取启动该keypress事件的源元素(在我们的示例中为输入),并获取其名称/类/ id /任何内容;因此,您可以修改其各自的变量(在我们的示例中为error.name
和error.email
)。其余的很简单。
Vue.use(VeeValidate);
new Vue({
el: '#demo',
data() {
return {
error: {
name: null,
email: null,
},
}
},
methods: {
updateError(e) {
const input = e.srcElement.name;
const message = this.errors.first(input);
if (message) { // check if message isn't undefined
if (input === 'name') {
this.error.name = message;
} else if (input === 'email') {
this.error.email = message;
}
}
},
},
});
.row {
display: flex;
flex-wrap: wrap;
}
.col {
flex: 0 0 45%;
max-width: 45%;
}
.slide-fade-enter-active {
transition: all .3s ease;
}
.slide-fade-leave-active {
transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-enter,
.slide-fade-leave-to {
transform: translateX(10px);
opacity: 0;
}
<script src="https://unpkg.com/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vee-validate@latest/dist/vee-validate.js"></script>
<div id="demo" class="row">
<div class="col">
Type a name and then erase. And then type again.
<input type="text" name="name" v-validate="'required'" @keypress="updateError($event)">
<!-- added keypress event -->
<transition name="slide-fade">
<div v-show="errors.has('name')">Errors: {{ errors.first('name') || error.name }}</div>
<!-- OR -->
</transition>
</div>
<div class="col" style="border-left: 2px solid black; padding-left: 10px">
Type an email and then erase. And then type again.
<input type="text" name="email" v-validate="'required'" @keypress="updateError($event)">
<!-- added keypress event -->
<transition name="slide-fade">
<div v-show="errors.has('email')">Errors: {{ errors.first('email') || error.email }}</div>
<!-- OR -->
</transition>
</div>
</div>
希望这对您有所帮助!