Skip to content

FormGroup Form Component

This component is generally used for quickly building form scenarios. It can configure Input fields, Select popups, perform form validation, and more.

📌 Platform Differences

APP(vue)H5WeChat Mini ProgramAlipay Mini Program

🏯 Basic Usage Example

html
<!-- Global usage -->
<hy-form-group :columns="columns" :formData="formData"></hy-form-group>
ts
import { reactive, ref } from 'vue';
import { HyWarn, FormTypeEnum } from 'hy-app';
import type { FormColumnsType } from 'hy-app';

const formData: AnyObject = reactive({
    custom: 'Custom value',
    isShow: true,
    sex: '1',
});
const formRef = ref<InstanceType<typeof HyForm>>(null);

const columns: FormColumnsType[] = reactive([
    {
        field: 'name',
        label: 'Name',
        type: FormTypeEnum.TEXT,
        rules: {
            required: true,
            message: 'No content entered',
            trigger: ['blur'],
        },
    },
    {
        field: 'sex',
        label: 'Gender',
        type: FormTypeEnum.RADIO,
        actions: [
            { label: 'Female', value: '0' },
            { label: 'Male', value: '1' },
        ],
        rules: {
            required: true,
            message: 'No content entered',
            trigger: ['blur', 'change'],
        },
    },
    {
        field: 'phone',
        label: 'Phone Number',
        type: FormTypeEnum.TEXT,
        rules: [
            {
                required: true,
                message: 'Please enter your phone number',
                trigger: ['blur', 'change'],
            },
            {
                type: 'phone',
                trigger: ['blur', 'change'],
            },
        ],
    },
    {
        field: 'password',
        label: 'Password',
        type: FormTypeEnum.PASSWORD,
        rules: {
            type: 'password',
            trigger: ['blur', 'change'],
        },
    },
    {
        field: 'isShow',
        label: 'Disabled',
        type: FormTypeEnum.SWITCH,
    },
    {
        field: 'time',
        label: 'Date',
        type: FormTypeEnum.DATE,
        border: 'bottom',
        rules: {
            required: true,
            message: 'Please enter your date',
            trigger: ['blur', 'change'],
        },
    },
    {
        field: 'address',
        label: 'Address',
        type: FormTypeEnum.ADDRESS,
        rules: {
            required: true,
            message: 'Please enter your address',
            trigger: ['blur', 'change'],
        },
    },
    {
        field: 'select',
        label: 'Select Education',
        type: FormTypeEnum.SELECT,
        select: [
            [
                { text: 'Primary School', id: '1' },
                { text: 'Junior High', id: '2' },
                { text: 'Senior High', id: '3' },
                { text: 'University', id: '4' },
            ],
        ],
        rules: {
            required: true,
            message: 'Please select your education',
            trigger: ['blur', 'change'],
        },
    },
    {
        field: 'age',
        label: 'Age',
        type: FormTypeEnum.NUMBER,
        rules: [
            {
                required: true,
                message: 'Please enter your age',
                trigger: ['blur', 'change'],
            },
            {
                required: true,
                message: 'Cannot be less than the minimum value',
                min: 10,
                trigger: ['blur', 'change'],
            },
            {
                message: 'Cannot exceed the maximum value',
                max: 20,
                trigger: ['change'],
            },
        ],
    },
    {
        field: 'remark',
        label: 'Remarks',
        type: FormTypeEnum.TEXTAREA,
        rules: {
            required: true,
            message: 'Please enter your address',
            trigger: ['blur', 'change'],
        },
    },
]);

const handleSubmit = () => {
    formRef.value.submit().then((res) => {
        console.log(res);
    });
};

Custom Slots

Note

WeChat Mini Program difference: WeChat Mini Programs cannot dynamically set slots, so custom slots are not supported. The documentation will be updated if other solutions become available.

vue
<template>
    <hy-form ref="formRef" :columns="columns" :form-data="formData" labelWidth="90">
        <template #custom="{ record, errorStyle }">
            <hy-input v-model="formData[record.field]" :custom-style="errorStyle"></hy-input>
        </template>
    </hy-form>
</template>
ts
import { reactive } from 'vue';
import { FormTypeEnum, HyForm, HyInput } from 'hy-app';
import type { FormColumnsType } from 'hy-app';

const columns: FormColumnsType[] = reactive([
    {
        field: 'custom',
        label: 'Custom Content',
        type: FormTypeEnum.CUSTOM,
        rules: {
            required: true,
            message: 'Please enter your custom content',
            trigger: ['blur', 'change'],
        },
    },
]);

API

ParameterDescriptionTypeDefault
columnsForm field configurationarray-
formDataForm valuesobject-
labelPositionPosition of the form labelleft|topleft
labelWidthLabel widthstring|numberauto
rightWhether input values are right-alignedbooleanfalse
labelAlignAlignment of the label textleft|center|rightleft
borderShow bottom underline for each form rowbooleanfalse
disabledDisable the form[1]booleanfalse
inputCollection of Input API propertiesHyInputProps-
textareaCollection of Textarea API propertiesHyTextareaProps-
pickerCollection of Picker API propertiesHyPickerProps-
switchItemCollection of Switch API propertiesHySwitchProps-
radioCollection of Radio API propertiesHyRadioProps-
checkButtonCollection of CheckButton API propertiesHyCheckButtonProps-

columns

ParameterDescriptionTypeDefault
labelText descriptionstring-
fieldField namestring-
rightPin content to the rightboolean-
typeForm type (see the enumerated fields below).enum-
maxCountMaximum number of file uploads (when type is upload)number-
selectPicker configuration data collection[2]string[][]|{text:string;id:string}[][]-
actionsRadio configuration data collection[3]string-
modeDate display format (optional when type is date)enum-
rulesValidation rulesobject|array-
inputCollection of Input API propertiesHyInputProps-
textareaCollection of Textarea API propertiesHyTextareaProps-
pickerCollection of Picker API propertiesHyPickerProps-
switchItemCollection of Switch API propertiesHySwitchProps-
radioCollection of Radio API propertiesHyRadioProps-
checkButtonCollection of CheckButton API propertiesHyCheckButtonProps-

actions

ParameterDescriptionTypeDefault
labelDisplayed text contentstring-
valueRetrieved valuestring|number-
checkedWhether selectedboolean-
disabledWhether disabledboolean-

rules

ParameterDescriptionTypeDefault
requiredWhether requiredboolean-
messageMessage shown when validation failsstring-
triggerForm event validation(blur|change)[]-
minMinimum number of input charactersnumber-
maxMaximum number of input charactersnumber-
typePhone number, email, or complex password validationphone|email|password-
validatorCustom validation ruleFunction-

type Values

  • UPLOAD File upload
  • TEXT Regular text input
  • NUMBER Number input
  • PASSWORD Password input
  • ID_CARD ID card input
  • RADIO Radio
  • DATE Time picker
  • SELECT Selector
  • ADDRESS Address picker
  • SWITCH Switch
  • DETAIL Detail
  • TEXTAREA Textarea
  • CUSTOM Custom slot

mode Values

  • DATETIME yyyy-MM-dd HH:mm:SS
  • DATE yyyy-MM-dd
  • TIME hh:MM:ss
  • YEAR_MONTH yyyy-MM
  • MONTH_DAY MM-dd
  • HOUR_MINUTE HH:mm
  • MINUTE_SECOND mm:SS

Methods

Method NameDescriptionParameters
validateValidate the form-
resetFieldsReset the form-
clearValidateClear validation(fields?: string[])
submitSubmit the form-
03:29

  1. This disabled option takes priority over the disabled setting in columns ↩︎

  2. Required when type = select (see the HyPicker component) ↩︎

  3. Required when type = radio (see the HyRadio component) ↩︎