SoFunction
Updated on 2025-04-04

Vue3 Element Plus el-form form component example detailed explanation

Vue3 Element Plus el-form form component example detailed explanation

Updated: April 21, 2023 11:15:50 Author: Chengnan
This article mainly introduces the Vue3 Element Plus el-form form component. Element Plus is an upgraded version of ElementUI, providing more form controls and functions, and also improving some details and styles. This article combines sample code to explain in detail. Friends who need it can refer to it.

In Element Plus,el-formis a form component used to create forms for users to fill in and submit data. It provides many built-in validation rules and verification methods to make form validation easier.

useel-formComponents, you can organize form controls together and verify forms to ensure that the submitted data meets the expected format and requirements. This component has the following characteristics:

  • Supports built-in verification rules and custom verification functions.
  • You can set itmodelProperties bind form data to form components.
  • Supports callback functions before and after form verification.
  • Some common form controls are provided, such as input boxes, drop-down boxes, radio boxes, check boxes, etc.

In terms of functions and usage,el-formComponents are similar in Element Plus and ElementUI, but there are some changes in some details.

The following are Element Plus and ElementUIel-formSome major changes to the component:

  • Introduce method: Use ElementUI(ElementUI)Introduce components in a way, while Element Plus usesimportImport components. For example, using Element Plus in Vue 3, we need to import it like thisel-formComponents:
import { ElForm } from 'element-plus'
  • Style: Element Plus uses the new default themes and styles, different from the default themes and styles of ElementUI. You can use the theme styles provided by Element Plus or customize the theme styles.

  • Form Verification: In Element Plus, form verification passesthis.$()Method execution. In ElementUI, form verification passesthis.$((valid) => {})Method execution. This is because in Element Plus, the form validation callback function is an optional parameter.

  • Form control: Some new form controls have been added to Element Plus, such asTimePickerDatePickerTreeSelectwait. And in ElementUI, these form controls areel-date-pickerel-time-pickerel-cascaderetc. provided in components.

  • Translation: Element Plus supports more language translation and can support more languages ​​through custom translation objects. In ElementUI, only the default language translation and several language packs are available.

In short, Element Plus is an upgraded version of ElementUI, providing more form controls and features, while also improving some details and styles. Although there are some changes between the two, if you are already familiar with ElementUI'sel-formComponents, then you will quickly adapt to the use of Element Plus.

el-formIt is a form component in Element Plus, the following isel-formCommon properties and methods:

Common properties

  • model: Used to bind form data objects, can be usedv-modelBind to form element. For example,<el-input v-model=""></el-input>
  • rules: Used to set form validation rules. A rule is an array where each object represents a validation rule. For example,rules: { username: [ { required: true, message: 'Please enter the username', trigger: 'blur' } ] }
  • label-width: Used to set the label width of the form element.
  • label-position: Used to set the position of form element labels, optional values ​​include'right''left''top''bottom'
  • inline: Used to set whether it is an inline form.
  • disabled: Used to set whether to disable the form.

Common methods

  • validate: Used to trigger form verification. If the verification is successful, execute the callback function and pass it.true, otherwise passfalse. For example,((valid) => { if (valid) { // Form verification succeeded } else { // Form verification failed } })
  • resetFields: Used to reset form data and verify status.
  • clearValidate: Used to clear form verification status.
  • validateField: Used to trigger verification of specified form elements. For example,('username', (errorMessage) => { if (errorMessage) { // Verification failed } else { // Verification succeeded } })
  • submit: Used to submit form data, you need to specify a callback function that is called when the submission is successful or failed. For example,((formData) => { // Form submission was successful }, (error) => { // Form submission failed })

These areel-formCommonly used properties and methods, of course, there are other properties and methods that can be used when needed. In the official documentation of Element Plus, you can find more detailed documentation and examples.

Here is a simple oneel-formExample, including an input box and a submit button:

<template>
  <el-form ref="form" :model="formData" :rules="rules">
    <el-form-item label="Username" prop="username">
      <el-input v-model=""></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">Submit</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { ref } from 'vue'
import { ElForm, ElFormItem, ElInput, ElButton } from 'element-plus'

export default {
  components: {
    ElForm,
    ElFormItem,
    ElInput,
    ElButton,
  },
  setup() {
    const formData = ref({
      username: '',
    })

    const rules = ref({
      username: [
        { required: true, message: 'Username is required', trigger: 'blur' },
        { min: 3, max: 16, message: 'Length should be between 3 and 16', trigger: 'blur' }
      ]
    })

    const submitForm = () => {
      (valid => {
        if (valid) {
          // Submit form data
        } else {
          ('Validation failed')
          return false
        }
      })
    }

    const formRef = ref(null)

    return {
      formData,
      rules,
      submitForm,
      formRef,
    }
  }
}
</script>

This is the end of this article about the Vue3 Element Plus el-form form component. For more related contents of the Vue3 Element Plus el-form form component, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!

  • Vue3
  • Element
  • Plus
  • el-form
  • Form Components

Related Articles

  • Learning of Vue instructions

    This article mainly introduces Vue instructions. The Vue official website provides a total of 14 instructions. Let’s explain the detailed content of each instruction in detail. Friends who need it can refer to it.
    2021-10-10
  • How to import swiper plug-in in vue project

    This article mainly introduces the method of importing the swiper plug-in in the vue project. The editor thinks it is quite good. I will share it with you now and give you a reference. Let's take a look with the editor
    2018-01-01
  • Examples of basic usage of listeners in Vue

    As Vue is used more and more, I have gradually learned more about other knowledge points of Vue. This time I did the calculation on the page and used the Watch listener. This article mainly introduces relevant information about the basic usage of listeners in Vue. Friends who need it can refer to it.
    2021-08-08
  • vue implements search box with magnifying glass

    This article mainly introduces the search box with a magnifying glass in detail for you. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.
    2021-08-08
  • Solve the root cause of vue hot replacement failure

    Today, the editor will share with you an article to solve the fundamental cause of vue hot replacement failure. It has good reference value and hope it will be helpful to everyone. Let's take a look with the editor
    2018-09-09
  • Specific steps to use vuex in vue3+vite

    Use vuex in projects created by vue3+vite. It should be noted that some of the writing methods of vite are different from previous webpack. This article mainly introduces the specific steps of using vuex in vue3+vite. Friends who need it can refer to it.
    2022-11-11
  • How to dynamically assign img values ​​to vue

    This article mainly introduces how vue dynamically assigns values ​​to img, which has good reference value and hopes it will be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice
    2022-06-06
  • vue gets token (set token, clear token) to realize login method

    This article mainly introduces the login method of obtaining tokens (setting tokens, clearing tokens), which is of good reference value and hopes to be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice
    2022-09-09
  • vue list automatic scrolling instance

    This article mainly introduces the automatic scrolling example code of vue list, which has good reference value. I hope it will be helpful to everyone. If there are any errors or no complete considerations, I hope you will be very encouraged.
    2023-10-10
  • Use Vue and React to implement anchor point positioning function respectively

    This article mainly introduces in detail how to use Vue and React to implement anchor positioning functions respectively. The sample code in the article is explained in detail, which has certain reference value. Friends who need it can learn it.
    2024-01-01

Latest Comments