1 Introduction
The element-ui framework has been used for two years, and it involves the verification of el-form components, which are only used according to the examples given on the official website. el-form
Bind the model, el-form-item
bind the prop attribute, and el-form-item
bind the v-model in the form. As long as the attributes bound by v-model are consistent with the prop attributes and various validation rules are added, the validation of a certain form can be realized.
Therefore, I subconsciously think that a certain value must be bound with v-model to achieve verification. When packaging a custom component, I will subconsciously use the component to realize the function of v-model binding value. But once when using a component encapsulated by a colleague, he did not use the v-model binding, but used :xxx.sync
a property value implemented to modify the parent-child component synchronously. But it can also realize the verification of the el-form form. This blows my mind. Although my colleague explained it for a long time, I still didn't understand it. So I decided to look at the source code by myself.
Although the source code is not completely understood, the principle of why the verification is not triggered by the attributes bound by v-model can be said to be basically understood.
2. Binding of blur and change event
First look at the composition of the el-form component:
the el-form component exists under node_modules/element-ui/packages/form
the path, index.js is the Vue registration file, form.vue
, form-item.vue
, are the component, the component, and the label of the form, label-wrap.vue
respectively . There are other methods that need to be used so I won't list them first. This article only talks about the basis for verifying form items, so you only need to look at the content of the component.el-form
el-form-item
form-item
First look at the following piece of code:
- When the component is mounted, two listening events ( change and blur ) will be added.
- These two listening events will trigger
el-form-item
the validation function of the current component.
This $on()
is used to trigger custom methods.
This $on
trigger is written in the various input forms of element-ui, that is, only the input form of element-ui can trigger the callback change
of and , and it cannot be triggered blur
by directly binding a v-model with a native box. <input />
Because the various input forms of element-ui (such as input, checkbox, radio, etc.) are written this.dispatch()
to el-form-item
trigger blur
and change
.
3. Verification when blur or change
The binding of blur and change event is roughly understood, and then we will see how to trigger the verification when blur or change.
In the first picture, you can see that a method will be executed when blur or change validate('blur'/'change')
. Next, look at this validate method:
- Since the verification rule rules field can be added to
el-form
bothel-form-item
and, box 1 is used to filter the verification rules. - descriptor is used to delete the attributes in the verification rules
trigger
(maybe because the third-party library async-validator does not need this field) - The third big red box is the process of the third-party library verifying the object
[async-validator](https://gitcode.net/mirrors/yiminghe/async-validator?utm_source=csdn_github_accelerator)
according to the verification rule object . When the verification fails, the prompt warning information of el-form-item is displayed according to these two fields.descriptor
model
validateState
validateMessage
However, the answer to the title of this article is mainly on line 234. this.fieldValue
Obviously, this fieldValue is the value bound to the current form item (el-form-item). There is a computed property in the computer fieldValue
:
it uses a getPropByPath
method in the utils of the element. It is really unclear what the specific function of this method is. However, what it finally outputs is an object containing objects, keys, and values. Pass in an object and an attribute name to get the corresponding value. Then do some kind of filtering operation. Here it is simply understood as taking a value from an object .
The method in this computer getPropByPath()
can be understood as taking the value from the object bound to the current property according to the el-form-item
current prop
field . Seeing this, the principle of el-form trigger verification should be roughly understandable.el-form标签
model
3. Summary
A simple summary is:
el-form-item
What is verified is the value ofprop
the field bound to the field and the corresponding field in the object, as shown in the age field in the figure below:el-form
when performing or verification, the verification is yes , even if the binding below you is yes . It also validates fields. Therefore, it needs to be consistent with the field you need to bind, which explains why my colleague can trigger the verification without binding the value.model
blur
change
form.age
el-input
v-model
form.name
form.age
prop
form
v-model
This is actually a very simple thing that I have never understood, it is too good.