我有一个表格。我仍然希望表单延迟验证,所以每次我离开一个字段时,它都应该显示它是错误的还是正确的。但在某些时候,我有一种方法必须检查整个表格是否有效。但是当我使用 validate() 时,它会显示所有错误。我可以使用 resetValidate() 但这只会删除所有错误,即使是从填充的字段中。有什么建议么?
来自 Vuetify页面的 MWE:
<template>
<validation-observer
ref="observer"
v-slot="{ invalid }"
>
<form @submit.prevent="submit">
<validation-provider
v-slot="{ errors }"
name="Name"
rules="required|max:10"
>
<v-text-field
v-model="name"
:counter="10"
:error-messages="errors"
label="Name"
required
></v-text-field>
</validation-provider>
<validation-provider
v-slot="{ errors }"
name="phoneNumber"
:rules="{
required: true,
digits: 7,
regex: '^(71|72|74|76|81|82|84|85|86|87|88|89)\\d{5}$'
}"
>
<v-text-field
v-model="phoneNumber"
:counter="7"
:error-messages="errors"
label="Phone Number"
required
></v-text-field>
</validation-provider>
<validation-provider
v-slot="{ errors }"
name="email"
rules="required|email"
>
<v-text-field
v-model="email"
:error-messages="errors"
label="E-mail"
required
></v-text-field>
</validation-provider>
<validation-provider
v-slot="{ errors }"
name="select"
rules="required"
>
<v-select
v-model="select"
:items="items"
:error-messages="errors"
label="Select"
data-vv-name="select"
required
></v-select>
</validation-provider>
<validation-provider
v-slot="{ errors }"
rules="required"
name="checkbox"
>
<v-checkbox
v-model="checkbox"
:error-messages="errors"
value="1"
label="Option"
type="checkbox"
required
></v-checkbox>
</validation-provider>
<v-btn
class="mr-4"
type="submit"
:disabled="invalid"
>
submit
</v-btn>
<v-btn @click="clear">
clear
</v-btn>
</form>
</validation-observer>
</template>
<script>
import { required, digits, email, max, regex } from 'vee-validate/dist/rules'
import { extend, ValidationObserver, ValidationProvider, setInteractionMode } from 'vee-validate'
setInteractionMode('eager')
extend('digits', {
...digits,
message: '{_field_} needs to be {length} digits. ({_value_})',
})
extend('required', {
...required,
message: '{_field_} can not be empty',
})
extend('max', {
...max,
message: '{_field_} may not be greater than {length} characters',
})
extend('regex', {
...regex,
message: '{_field_} {_value_} does not match {regex}',
})
extend('email', {
...email,
message: 'Email must be valid',
})
export default {
components: {
ValidationProvider,
ValidationObserver,
},
data: () => ({
name: '',
phoneNumber: '',
email: '',
select: null,
items: [
'Item 1',
'Item 2',
'Item 3',
'Item 4',
],
checkbox: null,
}),
methods: {
submit () {
v = this.$refs.observer.validate()
console.log("isValid" + v)
},
clear () {
this.name = ''
this.phoneNumber = ''
this.email = ''
this.select = null
this.checkbox = null
this.$refs.observer.reset()
},
},
}
</script>
如何重写提交按钮,以便检查整个表单而不添加或删除错误?