Skip to content

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 ​

PropDescriptionTypeOptionsDefault
modelValueForm dataobject—{}
rulesValidation rulesobject—{}
labelWidthLabel widthstring | numberSupports pixel values and auto'auto'
inlineWhether to use inline layoutboolean—false
sizeForm sizestringlarge/medium/small/mini'medium'
disabledWhether to disable the formboolean—false

FormItem Props ​

PropDescriptionTypeOptionsDefault
labelLabel textstring—''
propField namestring—''
validateTriggerValidation triggerstringblur/change'blur'
rulesValidation rulesobject—{}
labelWidthLabel widthstring | number——
sizeForm item sizestringlarge/medium/small/mini'medium'
inlineWhether to use inline layoutboolean—false
disabledWhether to disableboolean—false

Form Methods ​

Method NameDescriptionParameters
validateValidate the entire form—
validateFieldValidate a specific fieldprop: string
resetFieldsReset the form—
clearValidateClear validation statefield?: 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;
}

Released under the MIT License.