# Form
This component is generally used in form scenarios. You can configure any form item and perform form validation.
# Import
import PressForm from 'press-ui/press-form/press-form.vue';
import PressFormItem from 'press-ui/press-form-item/press-form-item.vue';
export default {
components: {
PressForm,
PressFormItem,
}
}
# Code demonstration
# Basic usage
Each form field consists of a press-form-item, in which press-field, press-checkbox, press-radio, press-switch, etc. can be placed.
- In the form group, an object is bound through the model parameter. The properties of this object are the corresponding variables of the components in each
press-form-item. - Since
refis required for form validation and binding form rules, theformcomponent needs to be declared with theref="form"attribute. - For other components that may be included in
press-from-item, such asfieldandradio, please refer to the relevant documentation of each component.
Note: The attribute names in rules are the same as those in form, and the prop parameter passed to press-form-item is also bound to the same attribute name. Note that the prop parameter here is bound to a string (attribute name), not a variable.
<PressForm
ref="form1"
label-position="left"
:rules="rules"
:model="model1"
>
<PressFormItem
ref="item1"
label="Name"
prop="userInfo.name"
border-bottom
>
<PressField
:value="model1.userInfo.name"
@change="onChange"
/>
</PressFormItem>
<PressFormItem
ref="item1"
label="Gender"
prop="userInfo.sex"
border-bottom
@click="showSex = true; hideKeyboard()"
>
<PressField
v-model="model1.userInfo.sex"
disabled-color="#ffffff"
placeholder="Please select gender"
@click-input="showSex = true; hideKeyboard()"
/>
<PressIcon
slot="right"
name="arrow"
/>
</PressFormItem>
<PressFormItem
ref="item2"
label="fruit"
prop="radiovalue1"
border-bottom
>
<PressRadioGroup
v-model="model1.radiovalue1"
direction="horizontal"
>
<PressRadio
v-for="(item, index) in radiolist1"
:key="index"
custom-style="margin-right: 16px"
:label="item.name"
:name="item.name"
>
{{ item.name }}
</PressRadio>
</PressRadioGroup>
</PressFormItem>
<PressFormItem
ref="item3"
label="interests and hobbies"
prop="checkboxValue1"
border-bottom
label-width="80"
>
<PressCheckboxGroup
v-model="model1.checkboxValue1"
shape="square"
direction="horizontal"
@change="change"
>
<PressCheckbox
v-for="(item, index) in checkboxList1"
:key="index"
custom-style="margin-right: 16px"
:name="item.name"
>
{{ item.name }}
</PressCheckbox>
</PressCheckboxGroup>
</PressFormItem>
<PressFormItem
ref="item3"
label="Introduction"
prop="intro"
border-bottom
>
<PressField
v-model="model1.intro"
placeholder="No less than 3 words"
count
type="textarea"
show-word-limit
:always-embed="true"
:maxlength="100"
autosize
wrapper-style="width: 100%"
/>
</PressFormItem>
<PressFormItem
label="Verification code"
prop="code"
label-width="80"
border-bottom
>
<PressField
v-model="model1.code"
placeholder="Please fill in the verification code"
/>
<PressButton
slot="right"
type="primary"
size="mini"
:disabled="disabled1"
@click="getCode"
>
<press-count-down
use-slot
:time="time"
@change="onChangeTime"
>
<span class="countdown-wrap">
<template v-if="!Object.keys(timeData).length">Get information</template>
<template v-else-if="timeData.seconds <= 0">Re-acquire</template>
<template v-else>
<span class="item">{{ timeData.seconds }}</span>Re-acquire in seconds
</template>
</span>
</press-count-down>
</PressButton>
</PressFormItem>
</PressForm>
# Form-item component description
This component generally needs to be used with the Form component, and can also be used alone with components such as Field.
propis the attribute field in themodelpassed to theFormcomponent. If form validation is required, this attribute is required.labelPositioncan configure the alignment of the "label" on the left, and the options areleftandtop.borderBottomwhether to display the underline of the form field. If theFieldcomponent is configured with a border, this property can be set tofalseto hide the default underline.- If you want to configure left and right icons or small pictures in the form field, you can use the
leftIconandrightIconparameters to achieve it.
# Validation rules
The component validation part uses async-validator (opens new window). A field can set multiple built-in rules, as well as custom rules, trigger methods, etc. The validation rules for each field are an array, and each element object of the array is one of the rules (a field validation can be configured with multiple rules), as follows:
rules: {
'userInfo.name': {
type: 'string',
required: true,
message: 'Please fill in your name',
trigger: ['blur', 'change'],
},
code: {
type: 'string',
required: true,
len: 4,
message: 'Please fill in a 4-digit verification code',
trigger: ['blur'],
},
'userInfo.sex': {
type: 'string',
max: 1,
required: true,
message: 'Please select male or female',
trigger: ['blur', 'change'],
},
radiovalue1: {
type: 'string',
min: 1,
max: 2,
message: 'Life is beautiful, please don\'t give up treatment',
trigger: ['change'],
},
checkboxValue1: {
type: 'array',
min: 2,
required: true,
message: 'Don\'t stay at home too much, select at least two',
trigger: ['change'],
},
intro: {
type: 'string',
min: 3,
required: true,
message: 'No less than 3 words',
trigger: ['change'],
},
hotel: {
type: 'string',
min: 2,
required: true,
message: 'Please select the time of your stay',
trigger: ['change'],
},
'userInfo.birthday': {
type: 'string',
required: true,
message: 'Please select your birthday',
trigger: ['change'],
},
},
# Validation rule attributes
Multiple attributes can be configured in each validation rule. The following is an explanation of the commonly used attributes. For more details, please refer to the documentation of async-validator (opens new window):
trigger{String | Array}: There are two ways to trigger validation:change: Validate when the field value changes
blur: Trigger when the input box loses focus
If you monitor two methods at the same time, you need to write it in array form:
['change', 'blur']type{String} Built-in validation rules. If these rules cannot meet the needs, you can use regular matching or use thevalidatorcustom method.string: must be
stringtype, default typenumber: must be
numbertypeboolean: must be
booleantypemethod: must be
functiontyperegexp: must be
regexptype, the regular expression here refers to whether the content of the field is a regular expression, rather than using this regular expression to match the field valueinteger: must be
integertypefloat: must be
floating pointtypearray: must be
arraytypeobject: must be
objecttypeenum: must appear in the value specified by
enmudate: must be
datetypeurl: must be
urltypehex: must be
hexadecimaltypeemail: must be
emailtypeany: any type
requiredBoolean value, whether it is required or not. Configuring this parameter will not display the required asterisk on the left side of the input box. If necessary, please configurerequiredofpress-form-itemtotrue. Note: If you need to display an asterisk in theswipertag, you need to give the first root node ofswiper-itema certainmarginstylepatternThis parameter value is required to be a regular expression, such as:/\d+/, cannot have quotation marks, such as:"/\d+/". The component will perform regular judgment on the field and return the result.minMinimum value. If the field type is a string or array, the string length and the array length (length) will be compared withmin. If the field is a numeric value, it will be directly compared withmin.maxMaximum value, same rules asminparameterlenSpecified length, same rules asmin, higher priority thanminandmaxenum{Array} Specified value, used withtype: 'enum'whitespace{Boolean} If the field value is all spaces, it cannot pass therequired: truevalidation by default. If you want to allow it to pass, you need to set this parameter totruetransform{Function}, transform the value before validation, the function parameter is the current value, and the return value is the changed value. The parameters are as follows:value: the value of the current validation fieldmessagePrompt message when validation failsvalidator{Function}: custom synchronous validation function, parameters are as follows:rule: the validation rule corresponding to the current validation field inrulesvalue: the value of the current validation field
callback: callback when verification is completed. Generally, callback does not need to be executed. Return true (verification passed) or false (verification failed).
asyncValidator{Function}: custom asynchronous verification function, parameters are as follows:rule: verification rule corresponding to the current verification field inrulesvalue: value of the current verification fieldcallback: callback when verification is completed. After executing asynchronous operations (such as requesting data verification from the backend), if it fails,callback(new Error('prompt error message'))is required. If the verification passes, executecallback().
# Comprehensive practice
The above describes the rules and configuration of async-validator (opens new window). Let's do a comprehensive practical example. We need to enter the following verification for a certain field (there are many ways to implement verification. The following is only a guide example, not the only one, or the best practice):
- Required, can accept
changeandblurtrigger validation: configurerequiredparameter totrue, and configuretriggerto[change, blur] - Must be a letter or string, convert the field value to string type before validation: configure regular expression through
patternparameter:/^[0-9a-zA-Z]*$/g, and convert the field value to string before validation throughtransformparameter - Length between 6-8 characters: configure
minto6,maxto8 - Need to contain the letter
A: useincludesmethod, combined withvalidatorcustom function to implement - Asynchronous validation, after entering the account, when the input box loses focus, request the backend whether the account already exists: verify through the
asyncValidatorasynchronous function above
In summary, we can come up with the following configuration rule (only for comprehensive demonstration, not the best practice):
rules: {
name: [
// Required rule
{
required: true,
message: 'This is a required field',
// Blur and change event trigger test
trigger: ['blur', 'change'],
},
// Regular expression judgment is letters or numbers
{
pattern: /^[0-9a-zA-Z]*$/g,
// Convert the value to a string before regular expression test
transform(value) {
return String(value);
},
message: 'Can only contain letters or numbers',
},
// Judgment between 6-8 characters
{
min: 6,
max: 8,
message: 'The length is between 6-8 characters',
},
// Custom rule to determine whether it contains the letter "A"
{
validator: (rule, value, callback) => value.includes('A'),
message: 'Must include the letter "A"',
},
// Check if the user already exists
{
asyncValidator: (rule, value, callback) => {
post('/xxx/xxx', { name: value }).then((res) => {
// If the validation fails, you need to throw new Error('error message') in callback()
if (res.error) {
callback(new Error('name duplicate'));
} else {
// If the validation passes, you also need to execute callback() callback
callback();
}
});
},
// If it is asynchronous validation, there is no need to write the message attribute, and the error information can be thrown through Error
// message: 'xxx'
},
],
},
# Validation error prompt method
It provides a variety of validation error prompt methods, which are passed to the errorType parameter of the Form component:
message: By default, the text below the input box is used for promptingnone: no prompt will be givenborder-bottom: the underline at the bottom of the configuration scope is displayed in red, andborderBottom=truemust be set forform-itemto be effectivetoast: pop up error information in the form oftoastprompt, and only the error information of the first form field will be popped up each time
<template>
<press-form :errorType="errorType">
......
</press-form>
</template>
<script>
export default {
data() {
return {
// text prompt
errorType: 'message',
// no prompt
// errorType: 'none',
// underline prompt, it is required to set borderBottom=true for form-item to be effective
// errorType: 'border-bottom'
};
}
};
</script>
# Verification
After the above configuration and explanation, enter the last step and perform verification:
You need to call the validate method of the Form component through ref. The parameter of the callback function of this method is a Boolean value. true means the validation is passed, otherwise it is not.
<template>
<view>
<press-form :model="form" ref="form">
<press-form-item label="姓名" prop="name">
<press-field v-model="form.name" />
</press-form-item>
</press-form>
<press-button @click="submit">Submit</press-button>
</view>
</template>
<script>
export default {
data() {
return {
form: {
name: '',
},
rules: {
name: [
{
required: true,
message: 'Please enter your name',
trigger: ['blur', 'change'],
},
],
},
};
},
methods: {
submit() {
this.$refs.form.validate().then((res) => {
Toast('Validation passed');
})
.catch((errors) => {
Toast('Verification failed');
});
},
},
};
</script>
# API
# Form Props
| Parameters | Description | Type | Default value |
|---|---|---|---|
| model | Form data object | object | - |
| rules | Set by ref. If rules has custom methods, etc., you need to use the setRules method to set the rules | object|function|array | - |
| error-type | Error prompt method, optional none/toast/border-bottom/none | string | message |
| border-bottom | Whether to display the underline border of the form field | boolean | true |
| label-position | Position of the form field prompt text, left left, top top | string | left |
| label-width | Prompt text width, optional value or auto, unit px | string | number | 45 |
| label-align | label font alignment, optional left/center/right | string | left |
| label-style | label style, object form | object | - |
# Form Methods
This method needs to be called manually through ref
| Name | Description | Parameters |
|---|---|---|
| validate | Method for validating the entire form | - |
| setRules | If there are custom methods in rules, this method needs to be used to set rules rules, otherwise the WeChat applet will be invalid | function(rules) |
| validateField | Validate some form fields | function(value, function(errorsRes)) |
| resetFields | Reset the entire form, reset all field values to initial values and remove validation results | - |
| clearValidate | Clear validation results | function(props) |
# Form Item Props
| Parameters | Description | Type | Default value |
|---|---|---|---|
| label | Left prompt text | string | - |
| prop | The property name of the form field model object. This property is required when using the validate and resetFields methods. | string | - |
| border-bottom | Whether to display the bottom border. If you do not need the bottom border, you need to set the parameter of the same name of press-form to false. | string | boolean | true |
| label-width | The width of the prompt text, in rpx. If set, it will override the parameter of the same name of press-form. | string | number | - |
| label-position | The position of label, optional left/top | string | - |
| right-icon | Custom font icon on the right (limited to built-in icons) or image address | string | - |
| left-icon | Custom font icon on the left (limited to built-in icons) or image address | string | - |
| left-icon-style | Style of custom font icon on the left | string | object | - |
| required | Whether to display the * on the left, which is only for display. If you need to verify the required field, please configure the required field rule through rules | boolean | false |
# Form Item Slot
| Name | Description |
|---|---|
| - | Content of Form Item |
| right | Custom content on the right, you can pass in a button here to get verification code and other scenarios |
| error | Customize the information displayed when the verification error is passed in, and expose the internal current message error information through the scope slot |
# Form Item Events
| Event name | Description | Callback parameter | Version |
|---|---|---|---|
| click | Triggered when clicked | - | - |
# Online debugging
# Frequently asked questions
# setRules
To be compatible with WeChat Mini Programs, and if the validation rules contain methods, etc., you can only set the rules through the setRules method.
onReady() {
//onReady is one of the life cycles supported by uni-app
this.$refs.form.setRules(this.rules)
},
← Field NumberKeyboard →