Form
Brief Introduction
The Form component is used to collect user input data and perform validation.
The form can be implemented using two tags: <MYForm></MYForm> or <MYForm /> together with <MYForm-item></MYForm-item> or <MYForm-item />.
Basic Usage
Use v-model to bind form data and the rules prop to set validation rules.
Code Demo
展开代码
Multiple Form Controls
The form supports various control types including input, switch, radio, checkbox, etc.
Code Demo
展开代码
Inline Layout
Use the inline prop to set the form to an inline layout.
Code Demo
展开代码
Custom Label Width
Use the labelWidth prop to customize the width of labels.
Code Demo
展开代码
Different Sizes
Use the size prop to set the form size.
Code Demo
展开代码
Validation Trigger
Use the validateTrigger prop to set when validation should be triggered.
Code Demo
展开代码
Disabled State
Use the disabled prop to disable the entire form.
Code Demo
展开代码
Combined Usage
Finally, here is a combination of multiple APIs to create a complete form:
Code Demo
展开代码
API Reference
Form Props
| Prop | Description | Type | Options | Default |
|---|---|---|---|---|
| modelValue | Form data | object | — | {} |
| rules | Validation rules | object | — | {} |
| labelWidth | Label width | string | number | Supports pixel values and auto | 'auto' |
| inline | Whether to use inline layout | boolean | — | false |
| size | Form size | string | large/medium/small/mini | 'medium' |
| disabled | Whether to disable the form | boolean | — | false |
FormItem Props
| Prop | Description | Type | Options | Default |
|---|---|---|---|---|
| label | Label text | string | — | '' |
| prop | Field name | string | — | '' |
| validateTrigger | Validation trigger | string | blur/change | 'blur' |
| rules | Validation rules | object | — | {} |
| labelWidth | Label width | string | number | — | — |
| size | Form item size | string | large/medium/small/mini | 'medium' |
| inline | Whether to use inline layout | boolean | — | false |
| disabled | Whether to disable | boolean | — | false |
Form Methods
| Method Name | Description | Parameters |
|---|---|---|
| validate | Validate the entire form | — |
| validateField | Validate a specific field | prop: string |
| resetFields | Reset the form | — |
| clearValidate | Clear validation state | field?: string |
Type Definitions
typescript
interface FormRule {
required?: boolean;
message?: string;
validator?: (rule: FormRule, value: string) => boolean | Promise<boolean>;
len?: number;
}
interface FormProps {
modelValue?: Record<string, any>;
rules?: Record<string, FormRule[]>;
labelWidth?: string | number;
inline?: boolean;
size?: 'large' | 'medium' | 'small' | 'mini';
disabled?: boolean;
}
interface FormItemProps {
label?: string;
prop?: string;
validateTrigger?: 'blur' | 'change';
rules?: Record<string, FormRule[]>;
labelWidth?: string | number;
size?: 'large' | 'medium' | 'small' | 'mini';
inline?: boolean;
disabled?: boolean;
}