


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>
Verification rules:
rules: { username: [{ required: true, message: 'Please enter the username', trigger: 'blur' }], password: [{ required: true, message: 'Please enter password', trigger: 'blur' }] },
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; },
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:
-
Ensure
loginform.username
responsiveness: Ifloginform
is a normal JavaScript object, direct assignment will not trigger Vue responsive updates. Updateloginform.username
using theVue.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 loginor:
selection(params) { this.loginForm = { ...this.loginForm, username: params.username }; // Object expansion operator this.loginForm.password = atob(params.password); }
Copy after login Check
trigger
attribute:trigger: 'blur'
triggers only if the input box loses focus. The selection operation ofel-autocomplete
may not triggerblur
event. Try modifyingtrigger
attribute to'change'
or using'blur'
and'change'
at the same time, or selecting the appropriate trigger event based on the actual situation.Check
v-model
binding andloginform
initialization: Make sure thatv-model
binding data is correct and thatloginform
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

JDBC...

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

In processing next-auth generated JWT...

Why is the return value empty when using RedisTemplate for batch query? When using RedisTemplate for batch query operations, you may encounter the returned results...

The optimization solution for SpringBoot timing tasks in a multi-node environment is developing Spring...

How to solve the problem of printing spaces in IDEA console logs? When using IDEA for development, many developers may encounter a problem: the console printed...
