表单
当涉及到表单验证时,Vuetify 具有大量的集成和功能。 是否要使用第三方验证插件? 开箱即用,您可以使用 Vee-validate 和 vuelidate。
用例
内部的 v-form
组件使添加验证到表单输入变得容易。 所有输入组件都有一个“ rules
属性,该属性带有一系列功能。 这些函数允许您指定字段为valid 或 invalid 的条件。 每当输入值更改时,数组中的每个函数都将接收新值。 如果函数返回 false 或字符串,则验证失败。
template script
<template>
<v-form v-model="valid">
<v-container>
<v-row>
<v-col
cols="12"
md="4"
>
<v-text-field
v-model="firstname"
:rules="nameRules"
:counter="10"
label="First name"
required
></v-text-field>
</v-col>
<v-col
cols="12"
md="4"
>
<v-text-field
v-model="lastname"
:rules="nameRules"
:counter="10"
label="Last name"
required
></v-text-field>
</v-col>
<v-col
cols="12"
md="4"
>
<v-text-field
v-model="email"
:rules="emailRules"
label="E-mail"
required
></v-text-field>
</v-col>
</v-row>
</v-container>
</v-form>
</template>
<script>
export default {
data: () => ({
valid: false,
firstname: '',
lastname: '',
nameRules: [
v => !!v || 'Name is required',
v => v.length <= 10 || 'Name must be less than 10 characters',
],
email: '',
emailRules: [
v => !!v || 'E-mail is required',
v => /.+@.+/.test(v) || 'E-mail must be valid',
],
}),
}
</script>
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
实战场
template script
<template>
<v-row align="center">
<v-row justify="space-around">
<v-switch v-model="valid" class="ma-4" label="Valid" readonly></v-switch>
<v-switch v-model="lazy" class="ma-4" label="Lazy"></v-switch>
</v-row>
<v-form
ref="form"
v-model="valid"
:lazy-validation="lazy"
>
<v-text-field
v-model="name"
:counter="10"
:rules="nameRules"
label="Name"
required
></v-text-field>
<v-text-field
v-model="email"
:rules="emailRules"
label="E-mail"
required
></v-text-field>
<v-select
v-model="select"
:items="items"
:rules="[v => !!v || 'Item is required']"
label="Item"
required
></v-select>
<v-checkbox
v-model="checkbox"
:rules="[v => !!v || 'You must agree to continue!']"
label="Do you agree?"
required
></v-checkbox>
<v-btn
:disabled="!valid"
color="success"
class="mr-4"
@click="validate"
>
Validate
</v-btn>
<v-btn
color="error"
class="mr-4"
@click="reset"
>
Reset Form
</v-btn>
<v-btn
color="warning"
@click="resetValidation"
>
Reset Validation
</v-btn>
</v-form>
</v-row>
</template>
<script>
export default {
data: () => ({
valid: true,
name: '',
nameRules: [
v => !!v || 'Name is required',
v => (v && v.length <= 10) || 'Name must be less than 10 characters',
],
email: '',
emailRules: [
v => !!v || 'E-mail is required',
v => /.+@.+\..+/.test(v) || 'E-mail must be valid',
],
select: null,
items: [
'Item 1',
'Item 2',
'Item 3',
'Item 4',
],
checkbox: false,
lazy: false,
}),
methods: {
validate () {
this.$refs.form.validate()
},
reset () {
this.$refs.form.reset()
},
resetValidation () {
this.$refs.form.resetValidation()
},
},
}
</script>
示例
下面是一些简单到复杂的例子。
创建规则
规则允许您在所有表单组件上应用自定义验证。 这些将按顺序进行验证,并且一次将显示 maximum 或者 1 个错误,因此请确保您相应地创建规则。
template script
<template>
<v-container>
<v-row
justify="space-between"
>
<v-col
cols="12"
md="4"
>
<v-form ref="form">
<v-text-field
v-model="model"
:counter="max"
:rules="rules"
label="First name"
></v-text-field>
</v-form>
</v-col>
<v-col
cols="12"
md="6"
>
<v-slider
v-model="max"
label="Max characters"
>
</v-slider>
<v-checkbox
v-model="allowSpaces"
label="Allow spaces"
></v-checkbox>
<v-text-field
v-model="match"
label="Value must match"
></v-text-field>
</v-col>
</v-row>
</v-container>
</template>
<script>
export default {
data: () => ({
allowSpaces: false,
match: 'Foobar',
max: 0,
model: 'Foobar',
}),
computed: {
rules () {
const rules = []
if (this.max) {
const rule =
v => (v || '').length <= this.max ||
`A maximum of ${this.max} characters is allowed`
rules.push(rule)
}
if (!this.allowSpaces) {
const rule =
v => (v || '').indexOf(' ') < 0 ||
'No spaces are allowed'
rules.push(rule)
}
if (this.match) {
const rule =
v => (!!v && v) === this.match ||
'Values do not match'
rules.push(rule)
}
return rules
},
},
watch: {
match: 'validateField',
max: 'validateField',
model: 'validateField',
},
methods: {
validateField () {
this.$refs.form.validate()
},
},
}
</script>
验证与 提交 & 清除
v-form
组件具有 three 功能,可以通过在该组件上设置 ref 来访问它们。 ref 允许我们访问组件上的内部方法,例如 <v-form ref="form">
。this.$refs.form.validate() 将验证所有输入并返回所有输入是否有效。 this.$refs.form.reset() 将清除所有输入并重置其验证错误。this.$refs.form.resetValidation() 将仅重置输入验证,而不会更改其状态。
template script
<template>
<v-form
ref="form"
v-model="valid"
lazy-validation
>
<v-text-field
v-model="name"
:counter="10"
:rules="nameRules"
label="Name"
required
></v-text-field>
<v-text-field
v-model="email"
:rules="emailRules"
label="E-mail"
required
></v-text-field>
<v-select
v-model="select"
:items="items"
:rules="[v => !!v || 'Item is required']"
label="Item"
required
></v-select>
<v-checkbox
v-model="checkbox"
:rules="[v => !!v || 'You must agree to continue!']"
label="Do you agree?"
required
></v-checkbox>
<v-btn
:disabled="!valid"
color="success"
class="mr-4"
@click="validate"
>
Validate
</v-btn>
<v-btn
color="error"
class="mr-4"
@click="reset"
>
Reset Form
</v-btn>
<v-btn
color="warning"
@click="resetValidation"
>
Reset Validation
</v-btn>
</v-form>
</template>
<script>
export default {
data: () => ({
valid: true,
name: '',
nameRules: [
v => !!v || 'Name is required',
v => (v && v.length <= 10) || 'Name must be less than 10 characters',
],
email: '',
emailRules: [
v => !!v || 'E-mail is required',
v => /.+@.+\..+/.test(v) || 'E-mail must be valid',
],
select: null,
items: [
'Item 1',
'Item 2',
'Item 3',
'Item 4',
],
checkbox: false,
}),
methods: {
validate () {
this.$refs.form.validate()
},
reset () {
this.$refs.form.reset()
},
resetValidation () {
this.$refs.form.resetValidation()
},
},
}
</script>
Vue School
Learn Vue.js and modern and cutting-edge front-end technologies with our premium tutorials and video courses.
ads by Vuetify
](https://vueschool.io/?ref=vuetifyjs.com&friend=vuetify)
验证
vuelidate 是 Vue.js 的简单、轻量模型验证。Documentation
template script
<template>
<form>
<v-text-field
v-model="name"
:error-messages="nameErrors"
:counter="10"
label="Name"
required
@input="$v.name.$touch()"
@blur="$v.name.$touch()"
></v-text-field>
<v-text-field
v-model="email"
:error-messages="emailErrors"
label="E-mail"
required
@input="$v.email.$touch()"
@blur="$v.email.$touch()"
></v-text-field>
<v-select
v-model="select"
:items="items"
:error-messages="selectErrors"
label="Item"
required
@change="$v.select.$touch()"
@blur="$v.select.$touch()"
></v-select>
<v-checkbox
v-model="checkbox"
:error-messages="checkboxErrors"
label="Do you agree?"
required
@change="$v.checkbox.$touch()"
@blur="$v.checkbox.$touch()"
></v-checkbox>
<v-btn class="mr-4" @click="submit">submit</v-btn>
<v-btn @click="clear">clear</v-btn>
</form>
</template>
<script>
import { validationMixin } from 'vuelidate'
import { required, maxLength, email } from 'vuelidate/lib/validators'
export default {
mixins: [validationMixin],
validations: {
name: { required, maxLength: maxLength(10) },
email: { required, email },
select: { required },
checkbox: {
checked (val) {
return val
},
},
},
data: () => ({
name: '',
email: '',
select: null,
items: [
'Item 1',
'Item 2',
'Item 3',
'Item 4',
],
checkbox: false,
}),
computed: {
checkboxErrors () {
const errors = []
if (!this.$v.checkbox.$dirty) return errors
!this.$v.checkbox.checked && errors.push('You must agree to continue!')
return errors
},
selectErrors () {
const errors = []
if (!this.$v.select.$dirty) return errors
!this.$v.select.required && errors.push('Item is required')
return errors
},
nameErrors () {
const errors = []
if (!this.$v.name.$dirty) return errors
!this.$v.name.maxLength && errors.push('Name must be at most 10 characters long')
!this.$v.name.required && errors.push('Name is required.')
return errors
},
emailErrors () {
const errors = []
if (!this.$v.email.$dirty) return errors
!this.$v.email.email && errors.push('Must be valid e-mail')
!this.$v.email.required && errors.push('E-mail is required')
return errors
},
},
methods: {
submit () {
this.$v.$touch()
},
clear () {
this.$v.$reset()
this.name = ''
this.email = ''
this.select = null
this.checkbox = false
},
},
}
</script>
Vee-validate
vee-validate 是用于 Vue.js 的基于模板的验证框架。文档
template script
<template>
<ValidationObserver ref="observer" v-slot="{ validate, reset }">
<form>
<ValidationProvider 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>
</ValidationProvider>
<ValidationProvider v-slot="{ errors }" name="email" rules="required|email">
<v-text-field
v-model="email"
:error-messages="errors"
label="E-mail"
required
></v-text-field>
</ValidationProvider>
<ValidationProvider 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>
</ValidationProvider>
<ValidationProvider v-slot="{ errors, valid }" rules="required" name="checkbox">
<v-checkbox
v-model="checkbox"
:error-messages="errors"
value="1"
label="Option"
type="checkbox"
required
></v-checkbox>
</ValidationProvider>
<v-btn class="mr-4" @click="submit">submit</v-btn>
<v-btn @click="clear">clear</v-btn>
</form>
</ValidationObserver>
</template>
<script>
import { required, email, max } from 'vee-validate/dist/rules'
import { extend, ValidationObserver, ValidationProvider, setInteractionMode } from 'vee-validate'
setInteractionMode('eager')
extend('required', {
...required,
message: '{_field_} can not be empty',
})
extend('max', {
...max,
message: '{_field_} may not be greater than {length} characters',
})
extend('email', {
...email,
message: 'Email must be valid',
})
export default {
components: {
ValidationProvider,
ValidationObserver,
},
data: () => ({
name: '',
email: '',
select: null,
items: [
'Item 1',
'Item 2',
'Item 3',
'Item 4',
],
checkbox: null,
}),
methods: {
submit () {
this.$refs.observer.validate()
},
clear () {
this.name = ''
this.email = ''
this.select = null
this.checkbox = null
this.$refs.observer.reset()
},
},
}
</script>