I'm using a combobox with input data validation (Vuelidate):
<template>
<v-combobox
clearable
v-model="surname"
:items="commonSurnames"
label="Surname"
placeholder="Type in the surname"
class="pt-5 pb-5"
:error-messages="surnameErrors"
@input="$v.surname.$touch()"
@blur="$v.surname.$touch()">
</v-combobox>
</template>
<script>
import { validationMixin } from 'vuelidate'
import { required, maxLength } from 'vuelidate/lib/validators'
export default {
mixins: [validationMixin],
validations: {
surname: {
required,
maxLength: maxLength(30),
validSurname(surname) {
return (
/^[a-zA-Z]-?*.+$/.test(surname)
)
}
},
name: 'Surnames',
data() {
return {
surname: '',
[...]
},
methods: {
[...]
},
computed: {
surnameErrors() {
const errors = []
if (!this.$v.surname.$dirty) return errors
!this.$v.surname.validSurname && errors.push('Format must be like: Smith or Smith-Wesson')
!this.$v.surname.maxLength && errors.push('Surname must be at most 30 characters long.')
!this.$v.surname.required && errors.push('Surname is required.')
return errors
}
}
</script>
Versions of components:
"dependencies": {
"@vue/compiler-sfc": "^3.0.0",
"core-js": "^3.6.5",
"vue": "^2.6.11",
"vuelidate": "^0.7.5",
"vuetify": "^2.2.11"
},
I suppose I did everything as in the Vuetify Documentation, but my form gets validated a bit differently than what is there, in the docs: I can exceed the limit of 30 characters without being notified about it while typing. I only know it when the input loses focus. The same situation with RegEx validation: any value is accepted without error notification. If the value is not valid, I get notified when leaving the input field.
Did I miss something when copying the example from the docs, or the @input
listener works incorrectly? Or is it that v-combobox
can't be validated this way?
v-combobox
emitschange
orinput
events only when one of its valid values from the dropdown are selected. The text entered in the combobox is actually search input, used to lookup a valid value; and that entry is bound tov-combobox
'ssearch-input
prop.The
v-combobox
API listsupdate:search-input
as an event that is fired when the search input changes, so you could use that to bind the validation handler: