Simple use of Vue vee-validate plug-in

Simple use of Vue vee-validate plug-in

1. Installation

2. Import

import { Form, Field } from 'vee-validate'

3. Define validation rules (it is best to package the js file separately in the utils folder for export)

// Create a js file for export export default {
  // Verify item account
  account(value) {
    if (!value) return 'cannot be empty' // conditional judgment,
    return true // Finally, all passed must return true
  },
  password (value) {
    if (!value) return 'Please enter your password'
    if (!/^\w{6,24}$/.test(value)) return 'The password is 6-24 characters'
    return true
  },
  mobile (value) {
    if (!value) return 'Please enter your phone number'
    if (!/^1[3-9]\d{9}$/.test(value)) return 'The phone number format is incorrect'
    return true
  },
  code (value) {
    if (!value) return 'Please enter the verification code'
    if (!/^\d{6}$/.test(value)) return 'The verification code is 6 digits'
    return true
  },
  isAgree (value) {
    if (!value) return 'Please check to agree to the user agreement'
    return true
  }
}

4. Use the Form component to configure validation rules and error objects (both form and Field are exported from the plugin on demand)

// validation-schema="mySchema" configures validation rules // v-slot: export error object <Form
  :validation-schema="mySchema"
  v-slot="{ errors }"
>
 <!-- Form elements -->
</Form>

<script>
  import schema from '@/utils/vee-validate-schema'
  setup () {
    // Form object data const form = reactive({
      account: null, // account password: null // password })
    // Validation rule object const mySchema = {
      account: schema.account,
      password: schema.password
    }
    return { form, mySchema }
 } 
</script>

5. Use the Field component to add form item verification

//1. Change input to `Field` component, which is parsed as input by default
//2. `Field` adds the name attribute to specify which validation rule in the schema to use //3. `Field` adds v-model to provide two-way binding of form data //4. When a form validation error occurs, the error class name `error` is displayed, and a red border <Field is displayed
      v-model="form.account"
      name="account" 
      type="text"
      placeholder="Please enter your username"
      :class="{ error: errors.account }" // If an error message is returned, it is true to display the class error
    />
    <!-- <input type="text" placeholder="Please enter your username" /> -->

6. Supplement form data and validation rule data

// Data bound to the form const form = reactive({
  account: null, // account password: null, // password isAgree: true // whether it is selected})

// Declare the validation data rules required for the current form const curSchema = reactive({
  account: schema.account, // account password: schema.password, // password isAgree: schema.isAgree // whether selected})

The above is the detailed content of the simple use of Vue vee-validate plug-in. For more information about Vue vee-validate plug-in, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed tutorial on using the vue.js form validation plugin (vee-validate)
  • Using Vee-validate form validation in Vue.js + Nuxt.js project
  • vee-validate vue 2.0 custom form validation example
  • How to use vee-validate form validation in Vue
  • Detailed explanation of the use of Vue2.0 form validation component vee-validate

<<:  SQL statements in Mysql do not use indexes

>>:  web.config (IIS) and .htaccess (Apache) configuration

Recommend

How to use skeleton screen in vue project

Nowadays, application development is basically se...

Before making a web page, let’s take a look at these so-called specifications

This article has compiled some so-called specific...

How to use javascript to do simple algorithms

Table of contents 1 Question 2 Methods 3 Experime...

Simple steps to encapsulate components in Vue projects

Table of contents Preface How to encapsulate a To...

How to remove inline styles defined by the style attribute (element.style)

When modifying Magento frequently, you may encount...

MySQL uninstall and install graphic tutorial under Linux

This is my first time writing a blog. I have been...

A complete guide to Linux environment variable configuration

Linux environment variable configuration When cus...

JS Easy to understand Function and Constructor

Table of contents 1. Overview 1.1 Creating a func...

html opens a new window with a hyperlink and can control window properties

1. The window size opened by the HTML hyperlink C...

Html tips to make your code semantic

Html semantics seems to be a commonplace issue. G...

Implementation of fastdfs+nginx cluster construction

1. Introduction to fastdfs 1. What is fastdfs Fas...

How to backup and restore the mysql database if it is too large

Command: mysqlhotcopy This command will lock the ...

Solution to input cursor misalignment in Chrome, Firefox, and IE

Detailed explanation of the misplacement of the in...

How to solve the mysql ERROR 1045 (28000)-- Access denied for user problem

Problem description (the following discussion is ...