Home Web Front-end JS Tutorial Element UI autocomplete component and form verification conflict: How to solve the problem of form verification failure?

Element UI autocomplete component and form verification conflict: How to solve the problem of form verification failure?

Apr 04, 2025 pm 07:03 PM
vue Solution red

Element UI autocomplete component and form verification conflict: How to solve the problem of form verification failure?

Element UI auto-completion solution for component-form verification conflict

When using the el-autocomplete component of Element UI, you often encounter the problem of form verification failure: even if the user has selected the drop-down option and the input box displays the correct value, the form still prompts that it has not been filled in. This article analyzes this problem and provides solutions.

Problem description:

The form uses el-autocomplete component to implement username selection and uses el-form-item and prop properties to verify. select event of el-autocomplete binds a function that handles user selection logic. However, after the user selects, the form verification still fails, prompting "Please enter the user name".

Code example:

Component code:

<el-form-item label="用户名" prop="username">
  <el-autocomplete :fetch-suggestions="querysearch" class="usernameinput" placeholder="选择或输入用户名" v-model="selectuserinfo">
  </el-autocomplete>
</el-form-item>
Copy after login

Verification rules:

 rules: {
  username: [{ required: true, message: 'Please enter the username', trigger: 'blur' }],
  password: [{ required: true, message: 'Please enter password', trigger: 'blur' }]
},
Copy after login

Related functions:

 selection(params) {
  console.log(this.selectuserinfo);
  this.loginform.username = params.username;
  this.loginform.password = atob(params.password);
},
onblur() {
  console.log('blur');
  console.log(this.loginform.username, this.selectuserinfo);
  this.loginform.username = this.selectuserinfo;
},
Copy after login

Problem analysis and solution:

The root of the problem is that directly assigning this.loginform.username = params.username may destroy Vue's responsive mechanism. Vue's responsive system relies on data changes to trigger view updates and form verification. Directly modify the object properties, Vue cannot track changes, resulting in form verification that cannot be updated.

Solution:

  1. Ensure loginform.username responsiveness: If loginform is a normal JavaScript object, direct assignment will not trigger Vue responsive updates. Update loginform.username using the Vue.set method or object expansion operator to ensure that Vue tracks data changes.

     selection(params) {
      this.$set(this.loginform, 'username', params.username); // Use Vue.set
      this.loginform.password = atob(params.password);
    }
    Copy after login

    or:

     selection(params) {
      this.loginForm = { ...this.loginForm, username: params.username }; // Object expansion operator this.loginForm.password = atob(params.password);
    }
    Copy after login
  2. Check trigger attribute: trigger: 'blur' triggers only if the input box loses focus. The selection operation of el-autocomplete may not trigger blur event. Try modifying trigger attribute to 'change' or using 'blur' and 'change' at the same time, or selecting the appropriate trigger event based on the actual situation.

  3. Check v-model binding and loginform initialization: Make sure that v-model binding data is correct and that loginform object is correctly initialized as a responsive object.

Through the above methods, the Element UI can solve the problem of conflict between components and form verification by automatically completing the Element UI to ensure the accuracy of form verification.

The above is the detailed content of Element UI autocomplete component and form verification conflict: How to solve the problem of form verification failure?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use vue pagination How to use vue pagination Apr 08, 2025 am 06:45 AM

Pagination is a technology that splits large data sets into small pages to improve performance and user experience. In Vue, you can use the following built-in method to paging: Calculate the total number of pages: totalPages() traversal page number: v-for directive to set the current page: currentPage Get the current page data: currentPageData()

How to use the vue render function How to use the vue render function Apr 08, 2025 am 06:48 AM

The render function in Vue.js is an advanced rendering API that allows developers to control the generation of virtual DOMs (VDOMs) through pure JavaScript functions (h functions). The benefits of using the render function include higher performance, greater flexibility, better testability, and compatibility with JSX.

How to introduce css in vue file How to introduce css in vue file Apr 08, 2025 am 06:36 AM

Methods to introduce CSS into Vue files include: inline styles, scoped styles, external CSS, CSS preprocessors, and style bindings. The right method depends on the situation, such as inline styles suitable for small styles, scoped styles are used for component-specific styles, external CSS is suitable for large styles, CSS preprocessors provide advanced features, and style binding is used for dynamic styles.

How to traverse arrays of vue arrays How to traverse arrays of vue arrays Apr 08, 2025 am 06:39 AM

To iterate through the array in Vue.js, you can use the v-for directive. The syntax of this directive is &lt;template v-for=&quot;item in items&quot;&gt;, where item is the current element in the array and items is the array to be traversed. In addition, the v-for directive also provides the following other functions: v-for.of is used to specify the array to be traversed, v-for.in is used to specify the object properties to be traversed, v-for.index is used to obtain the array index currently traversed, and v-for.key is used to specify the unique key of each element to optimize list rendering.

How to use function intercept vue How to use function intercept vue Apr 08, 2025 am 06:51 AM

Function interception in Vue is a technique used to limit the number of times a function is called within a specified time period and prevent performance problems. The implementation method is: import the lodash library: import { debounce } from 'lodash'; Use the debounce function to create an intercept function: const debouncedFunction = debounce(() =&gt; { / Logical / }, 500); Call the intercept function, and the control function is called at most once in 500 milliseconds.

How to solve mysql cannot be started How to solve mysql cannot be started Apr 08, 2025 pm 02:21 PM

There are many reasons why MySQL startup fails, and it can be diagnosed by checking the error log. Common causes include port conflicts (check port occupancy and modify configuration), permission issues (check service running user permissions), configuration file errors (check parameter settings), data directory corruption (restore data or rebuild table space), InnoDB table space issues (check ibdata1 files), plug-in loading failure (check error log). When solving problems, you should analyze them based on the error log, find the root cause of the problem, and develop the habit of backing up data regularly to prevent and solve problems.

How to use foreach loop in vue How to use foreach loop in vue Apr 08, 2025 am 06:33 AM

The foreach loop in Vue.js uses the v-for directive, which allows developers to iterate through each element in an array or object and perform specific operations on each element. The syntax is as follows: &lt;template&gt; &lt;ul&gt; &lt;li v-for=&quot;item in items&gt;&gt;{{ item }}&lt;/li&gt; &lt;/ul&gt; &lt;/template&gt;&am

Solutions to the errors reported by MySQL on a specific system version Solutions to the errors reported by MySQL on a specific system version Apr 08, 2025 am 11:54 AM

The solution to MySQL installation error is: 1. Carefully check the system environment to ensure that the MySQL dependency library requirements are met. Different operating systems and version requirements are different; 2. Carefully read the error message and take corresponding measures according to prompts (such as missing library files or insufficient permissions), such as installing dependencies or using sudo commands; 3. If necessary, try to install the source code and carefully check the compilation log, but this requires a certain amount of Linux knowledge and experience. The key to ultimately solving the problem is to carefully check the system environment and error information, and refer to the official documents.

See all articles