表单

当涉及到表单验证时,Vuetify 具有大量的集成和功能。 是否要使用第三方验证插件? 开箱即用,您可以使用 Vee-validatevuelidate

用例

内部的 v-form 组件使添加验证到表单输入变得容易。 所有输入组件都有一个“ rules 属性,该属性带有一系列功能。 这些函数允许您指定字段为validinvalid 的条件。 每当输入值更改时,数组中的每个函数都将接收新值。 如果函数返回 false 或字符串,则验证失败。

template script


  1. <template>
  2. <v-form v-model="valid">
  3. <v-container>
  4. <v-row>
  5. <v-col
  6. cols="12"
  7. md="4"
  8. >
  9. <v-text-field
  10. v-model="firstname"
  11. :rules="nameRules"
  12. :counter="10"
  13. label="First name"
  14. required
  15. ></v-text-field>
  16. </v-col>
  17. <v-col
  18. cols="12"
  19. md="4"
  20. >
  21. <v-text-field
  22. v-model="lastname"
  23. :rules="nameRules"
  24. :counter="10"
  25. label="Last name"
  26. required
  27. ></v-text-field>
  28. </v-col>
  29. <v-col
  30. cols="12"
  31. md="4"
  32. >
  33. <v-text-field
  34. v-model="email"
  35. :rules="emailRules"
  36. label="E-mail"
  37. required
  38. ></v-text-field>
  39. </v-col>
  40. </v-row>
  41. </v-container>
  42. </v-form>
  43. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. valid: false,
  5. firstname: '',
  6. lastname: '',
  7. nameRules: [
  8. v => !!v || 'Name is required',
  9. v => v.length <= 10 || 'Name must be less than 10 characters',
  10. ],
  11. email: '',
  12. emailRules: [
  13. v => !!v || 'E-mail is required',
  14. v => /.+@.+/.test(v) || 'E-mail must be valid',
  15. ],
  16. }),
  17. }
  18. </script>

Forms(表单) - 图1

API

从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。

Forms(表单) - 图2

实战场

template script


  1. <template>
  2. <v-row align="center">
  3. <v-row justify="space-around">
  4. <v-switch v-model="valid" class="ma-4" label="Valid" readonly></v-switch>
  5. <v-switch v-model="lazy" class="ma-4" label="Lazy"></v-switch>
  6. </v-row>
  7. <v-form
  8. ref="form"
  9. v-model="valid"
  10. :lazy-validation="lazy"
  11. >
  12. <v-text-field
  13. v-model="name"
  14. :counter="10"
  15. :rules="nameRules"
  16. label="Name"
  17. required
  18. ></v-text-field>
  19. <v-text-field
  20. v-model="email"
  21. :rules="emailRules"
  22. label="E-mail"
  23. required
  24. ></v-text-field>
  25. <v-select
  26. v-model="select"
  27. :items="items"
  28. :rules="[v => !!v || 'Item is required']"
  29. label="Item"
  30. required
  31. ></v-select>
  32. <v-checkbox
  33. v-model="checkbox"
  34. :rules="[v => !!v || 'You must agree to continue!']"
  35. label="Do you agree?"
  36. required
  37. ></v-checkbox>
  38. <v-btn
  39. :disabled="!valid"
  40. color="success"
  41. class="mr-4"
  42. @click="validate"
  43. >
  44. Validate
  45. </v-btn>
  46. <v-btn
  47. color="error"
  48. class="mr-4"
  49. @click="reset"
  50. >
  51. Reset Form
  52. </v-btn>
  53. <v-btn
  54. color="warning"
  55. @click="resetValidation"
  56. >
  57. Reset Validation
  58. </v-btn>
  59. </v-form>
  60. </v-row>
  61. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. valid: true,
  5. name: '',
  6. nameRules: [
  7. v => !!v || 'Name is required',
  8. v => (v && v.length <= 10) || 'Name must be less than 10 characters',
  9. ],
  10. email: '',
  11. emailRules: [
  12. v => !!v || 'E-mail is required',
  13. v => /.+@.+\..+/.test(v) || 'E-mail must be valid',
  14. ],
  15. select: null,
  16. items: [
  17. 'Item 1',
  18. 'Item 2',
  19. 'Item 3',
  20. 'Item 4',
  21. ],
  22. checkbox: false,
  23. lazy: false,
  24. }),
  25. methods: {
  26. validate () {
  27. this.$refs.form.validate()
  28. },
  29. reset () {
  30. this.$refs.form.reset()
  31. },
  32. resetValidation () {
  33. this.$refs.form.resetValidation()
  34. },
  35. },
  36. }
  37. </script>

Forms(表单) - 图3

示例

下面是一些简单到复杂的例子。

创建规则

规则允许您在所有表单组件上应用自定义验证。 这些将按顺序进行验证,并且一次将显示 maximum 或者 1 个错误,因此请确保您相应地创建规则。

template script


  1. <template>
  2. <v-container>
  3. <v-row
  4. justify="space-between"
  5. >
  6. <v-col
  7. cols="12"
  8. md="4"
  9. >
  10. <v-form ref="form">
  11. <v-text-field
  12. v-model="model"
  13. :counter="max"
  14. :rules="rules"
  15. label="First name"
  16. ></v-text-field>
  17. </v-form>
  18. </v-col>
  19. <v-col
  20. cols="12"
  21. md="6"
  22. >
  23. <v-slider
  24. v-model="max"
  25. label="Max characters"
  26. >
  27. </v-slider>
  28. <v-checkbox
  29. v-model="allowSpaces"
  30. label="Allow spaces"
  31. ></v-checkbox>
  32. <v-text-field
  33. v-model="match"
  34. label="Value must match"
  35. ></v-text-field>
  36. </v-col>
  37. </v-row>
  38. </v-container>
  39. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. allowSpaces: false,
  5. match: 'Foobar',
  6. max: 0,
  7. model: 'Foobar',
  8. }),
  9. computed: {
  10. rules () {
  11. const rules = []
  12. if (this.max) {
  13. const rule =
  14. v => (v || '').length <= this.max ||
  15. `A maximum of ${this.max} characters is allowed`
  16. rules.push(rule)
  17. }
  18. if (!this.allowSpaces) {
  19. const rule =
  20. v => (v || '').indexOf(' ') < 0 ||
  21. 'No spaces are allowed'
  22. rules.push(rule)
  23. }
  24. if (this.match) {
  25. const rule =
  26. v => (!!v && v) === this.match ||
  27. 'Values do not match'
  28. rules.push(rule)
  29. }
  30. return rules
  31. },
  32. },
  33. watch: {
  34. match: 'validateField',
  35. max: 'validateField',
  36. model: 'validateField',
  37. },
  38. methods: {
  39. validateField () {
  40. this.$refs.form.validate()
  41. },
  42. },
  43. }
  44. </script>

Forms(表单) - 图4

验证与 提交 & 清除

v-form 组件具有 three 功能,可以通过在该组件上设置 ref 来访问它们。 ref 允许我们访问组件上的内部方法,例如 <v-form ref="form">this.$refs.form.validate() 将验证所有输入并返回所有输入是否有效。 this.$refs.form.reset() 将清除所有输入并重置其验证错误。this.$refs.form.resetValidation() 将仅重置输入验证,而不会更改其状态。

template script


  1. <template>
  2. <v-form
  3. ref="form"
  4. v-model="valid"
  5. lazy-validation
  6. >
  7. <v-text-field
  8. v-model="name"
  9. :counter="10"
  10. :rules="nameRules"
  11. label="Name"
  12. required
  13. ></v-text-field>
  14. <v-text-field
  15. v-model="email"
  16. :rules="emailRules"
  17. label="E-mail"
  18. required
  19. ></v-text-field>
  20. <v-select
  21. v-model="select"
  22. :items="items"
  23. :rules="[v => !!v || 'Item is required']"
  24. label="Item"
  25. required
  26. ></v-select>
  27. <v-checkbox
  28. v-model="checkbox"
  29. :rules="[v => !!v || 'You must agree to continue!']"
  30. label="Do you agree?"
  31. required
  32. ></v-checkbox>
  33. <v-btn
  34. :disabled="!valid"
  35. color="success"
  36. class="mr-4"
  37. @click="validate"
  38. >
  39. Validate
  40. </v-btn>
  41. <v-btn
  42. color="error"
  43. class="mr-4"
  44. @click="reset"
  45. >
  46. Reset Form
  47. </v-btn>
  48. <v-btn
  49. color="warning"
  50. @click="resetValidation"
  51. >
  52. Reset Validation
  53. </v-btn>
  54. </v-form>
  55. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. valid: true,
  5. name: '',
  6. nameRules: [
  7. v => !!v || 'Name is required',
  8. v => (v && v.length <= 10) || 'Name must be less than 10 characters',
  9. ],
  10. email: '',
  11. emailRules: [
  12. v => !!v || 'E-mail is required',
  13. v => /.+@.+\..+/.test(v) || 'E-mail must be valid',
  14. ],
  15. select: null,
  16. items: [
  17. 'Item 1',
  18. 'Item 2',
  19. 'Item 3',
  20. 'Item 4',
  21. ],
  22. checkbox: false,
  23. }),
  24. methods: {
  25. validate () {
  26. this.$refs.form.validate()
  27. },
  28. reset () {
  29. this.$refs.form.reset()
  30. },
  31. resetValidation () {
  32. this.$refs.form.resetValidation()
  33. },
  34. },
  35. }
  36. </script>

Forms(表单) - 图5

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


  1. <template>
  2. <form>
  3. <v-text-field
  4. v-model="name"
  5. :error-messages="nameErrors"
  6. :counter="10"
  7. label="Name"
  8. required
  9. @input="$v.name.$touch()"
  10. @blur="$v.name.$touch()"
  11. ></v-text-field>
  12. <v-text-field
  13. v-model="email"
  14. :error-messages="emailErrors"
  15. label="E-mail"
  16. required
  17. @input="$v.email.$touch()"
  18. @blur="$v.email.$touch()"
  19. ></v-text-field>
  20. <v-select
  21. v-model="select"
  22. :items="items"
  23. :error-messages="selectErrors"
  24. label="Item"
  25. required
  26. @change="$v.select.$touch()"
  27. @blur="$v.select.$touch()"
  28. ></v-select>
  29. <v-checkbox
  30. v-model="checkbox"
  31. :error-messages="checkboxErrors"
  32. label="Do you agree?"
  33. required
  34. @change="$v.checkbox.$touch()"
  35. @blur="$v.checkbox.$touch()"
  36. ></v-checkbox>
  37. <v-btn class="mr-4" @click="submit">submit</v-btn>
  38. <v-btn @click="clear">clear</v-btn>
  39. </form>
  40. </template>
  1. <script>
  2. import { validationMixin } from 'vuelidate'
  3. import { required, maxLength, email } from 'vuelidate/lib/validators'
  4. export default {
  5. mixins: [validationMixin],
  6. validations: {
  7. name: { required, maxLength: maxLength(10) },
  8. email: { required, email },
  9. select: { required },
  10. checkbox: {
  11. checked (val) {
  12. return val
  13. },
  14. },
  15. },
  16. data: () => ({
  17. name: '',
  18. email: '',
  19. select: null,
  20. items: [
  21. 'Item 1',
  22. 'Item 2',
  23. 'Item 3',
  24. 'Item 4',
  25. ],
  26. checkbox: false,
  27. }),
  28. computed: {
  29. checkboxErrors () {
  30. const errors = []
  31. if (!this.$v.checkbox.$dirty) return errors
  32. !this.$v.checkbox.checked && errors.push('You must agree to continue!')
  33. return errors
  34. },
  35. selectErrors () {
  36. const errors = []
  37. if (!this.$v.select.$dirty) return errors
  38. !this.$v.select.required && errors.push('Item is required')
  39. return errors
  40. },
  41. nameErrors () {
  42. const errors = []
  43. if (!this.$v.name.$dirty) return errors
  44. !this.$v.name.maxLength && errors.push('Name must be at most 10 characters long')
  45. !this.$v.name.required && errors.push('Name is required.')
  46. return errors
  47. },
  48. emailErrors () {
  49. const errors = []
  50. if (!this.$v.email.$dirty) return errors
  51. !this.$v.email.email && errors.push('Must be valid e-mail')
  52. !this.$v.email.required && errors.push('E-mail is required')
  53. return errors
  54. },
  55. },
  56. methods: {
  57. submit () {
  58. this.$v.$touch()
  59. },
  60. clear () {
  61. this.$v.$reset()
  62. this.name = ''
  63. this.email = ''
  64. this.select = null
  65. this.checkbox = false
  66. },
  67. },
  68. }
  69. </script>

Forms(表单) - 图6

Vee-validate

vee-validate 是用于 Vue.js 的基于模板的验证框架。文档

template script


  1. <template>
  2. <ValidationObserver ref="observer" v-slot="{ validate, reset }">
  3. <form>
  4. <ValidationProvider v-slot="{ errors }" name="Name" rules="required|max:10">
  5. <v-text-field
  6. v-model="name"
  7. :counter="10"
  8. :error-messages="errors"
  9. label="Name"
  10. required
  11. ></v-text-field>
  12. </ValidationProvider>
  13. <ValidationProvider v-slot="{ errors }" name="email" rules="required|email">
  14. <v-text-field
  15. v-model="email"
  16. :error-messages="errors"
  17. label="E-mail"
  18. required
  19. ></v-text-field>
  20. </ValidationProvider>
  21. <ValidationProvider v-slot="{ errors }" name="select" rules="required">
  22. <v-select
  23. v-model="select"
  24. :items="items"
  25. :error-messages="errors"
  26. label="Select"
  27. data-vv-name="select"
  28. required
  29. ></v-select>
  30. </ValidationProvider>
  31. <ValidationProvider v-slot="{ errors, valid }" rules="required" name="checkbox">
  32. <v-checkbox
  33. v-model="checkbox"
  34. :error-messages="errors"
  35. value="1"
  36. label="Option"
  37. type="checkbox"
  38. required
  39. ></v-checkbox>
  40. </ValidationProvider>
  41. <v-btn class="mr-4" @click="submit">submit</v-btn>
  42. <v-btn @click="clear">clear</v-btn>
  43. </form>
  44. </ValidationObserver>
  45. </template>
  1. <script>
  2. import { required, email, max } from 'vee-validate/dist/rules'
  3. import { extend, ValidationObserver, ValidationProvider, setInteractionMode } from 'vee-validate'
  4. setInteractionMode('eager')
  5. extend('required', {
  6. ...required,
  7. message: '{_field_} can not be empty',
  8. })
  9. extend('max', {
  10. ...max,
  11. message: '{_field_} may not be greater than {length} characters',
  12. })
  13. extend('email', {
  14. ...email,
  15. message: 'Email must be valid',
  16. })
  17. export default {
  18. components: {
  19. ValidationProvider,
  20. ValidationObserver,
  21. },
  22. data: () => ({
  23. name: '',
  24. email: '',
  25. select: null,
  26. items: [
  27. 'Item 1',
  28. 'Item 2',
  29. 'Item 3',
  30. 'Item 4',
  31. ],
  32. checkbox: null,
  33. }),
  34. methods: {
  35. submit () {
  36. this.$refs.observer.validate()
  37. },
  38. clear () {
  39. this.name = ''
  40. this.email = ''
  41. this.select = null
  42. this.checkbox = null
  43. this.$refs.observer.reset()
  44. },
  45. },
  46. }
  47. </script>

Forms(表单) - 图7