Currently, I have implemented a combobox with input data validation using 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"
},
After following the instructions outlined in the Vuetify Documentation, I noticed that my form validation behaves slightly differently. For instance, I am able to exceed the 30-character limit without any notification while typing. The same goes for RegEx validation, where any value is accepted without an error until I leave the input field. Could I have missed something during the copying of the example from the docs, or is there an issue with how the @input
listener functions? Perhaps, v-combobox
cannot be validated in this manner?