Skip to content

Generation Rules

In FormCreate, component generation rules define how to generate form components through JSON configuration. By understanding and using these rules, you can flexibly create and control various form elements.

The following is a simple example of how to generate an input component through JSON:

Component Generation Rules Explanation

JSON Rules and Component Mapping Relationship

Note

Form parameters need to be wrapped with ref to achieve reactivity and cannot be directly placed in computed properties. rule cannot be placed in computed properties!!!

When rendering forms in dialogs, it is recommended to deep copy the rules before use to avoid data pollution issues caused by multiple references.

You can force component re-rendering by modifying the key field.

Generation rules cannot be mixed between different UI frameworks, as there may be compatibility issues.

Data Structure

ts
type Rule = {
    // Component name to generate, e.g., 'input', 'select', etc.
    type: string;
    // Form field name for data binding
    field?: string;
    // Unique identifier for the component
    name?: string;
    // Field label
    title?: string;
    // Component hint information
    info?: string;
    // Default value of the component
    value?: any;
    // Whether to generate the component as-is, not nested in `FormItem`
    native?: boolean;
    // Component property configuration
    props?: Object;
    // Component inline styles
    style?: string | Object;
    // Component class style classes
    class?: string | Array<string>;
    // Set component id
    id?: string | Array<string>;
    // Component event handler functions
    on?: { [key: string]: Function | Function[] };
    // Component generation lifecycle callbacks
    hook?: { [key: string]: Function | Function[] };
    // Slot name for component nesting
    slot?: string;
    // Component key, usually used as a unique identifier in list rendering
    key?: string;
    // Whether required
    $required?: boolean | string | Object;
    // Component options list, applicable to `radio`, `select`, `checkbox`, etc.
    options?: Array<any>;
    // Target property where options are inserted, defaults to `props.data`
    optionsTo?: string;
    // Whether to generate the component as-is without wrapping in FormItem
    native?: boolean;
    // Whether to hide the component (DOM element will not be rendered)
    hidden?: boolean;
    // Whether to display the component (DOM is rendered but may not be visible)
    display?: boolean;
    // Whether to enable event injection
    inject?: boolean|Object;
    // Whether to ignore this field when submitting the form, when equal to 'hidden', ignore when component is hidden
    ignore?: boolean| 'hidden';
    // Component validation rules
    validate?: Object[];
    // Child component list for nesting child components
    children?: Rule[];
    // Component linkage control, controls the display of other components
    control?: Array;
    // Dynamically calculate specified fields of the component, automatically recalculate and update results when dependent values change
    computed?: Object;
    // FormItem configuration
    wrap?: Object;
    // Set component layout rules
    col?: Object;
    // Custom properties, such as remote data fetching, etc.
    effect?: {
        // Load remote data
        fetch?: Object,
    };
    // Set component prefix content, usually used to display icons or text before input box
    prefix?: string|Rule;
    // Set component suffix content, usually used to display icons or text after input box
    suffix?: string|Rule;
    // Set component custom directives
    directives?: object;
    // Whether to cache the component, only trigger rendering once
    cache?: boolean;
    // Set callback function for dynamically updating component content
    update?: (value:any, api:Api, origin:{
        // init triggered on initialization, link triggered on association, value triggered on value change
        origin: 'change' | 'init' | 'link';
        // Field name that triggered the association
        linkField?: string;
    }) => bool|undefined;
    // Render slots through functions
    renderSlots?: {
        default?: (scope) => Vnode | Vnode[];
        [slot: string]: (scope) => Vnode | Vnode[];
    }
    // Configure which field changes will trigger the current component's `update` callback
    link?: string[];
    // Set the names of properties in `props` that need two-way binding
    sync?: string[];
    // Event names to listen to using `emit` method
    emit?: string[];
    // Custom component `emit` event prefix, default is the component's `field` field
    emitPrefix?: string;
    // Define custom component for rendering current rule
    component?: boolean;
    // Other extended properties
    [key: string]: any;
}

Basic Configuration

Property NameTypeDescription
typestringSet the component type name to generate, e.g., 'input', 'select', etc. It supports configuring standard form components, custom Vue components, and HTML tags.
fieldstringForm component field name, usually used for data binding. Each form item's field should be unique, used to associate the component's value with form data.
titlestring|RuleField title, usually displayed as form label.
namestringSet component alias for retrieving rules through API.
valueanyComponent default value.
infostring|RuleSet component hint information Configuration Guide
nativebooleanWhether to generate component as-is, not nested in FormItem.
hiddenboolWhether to hide the component (DOM element will not be rendered).
displayboolWhether to display the component (DOM is rendered but may not be visible), hiding will also trigger form validation.
prefixstring|RuleSet component prefix content, usually used to display icons or text before input box Configuration Guide
suffixstring|RuleSet component suffix content, usually used to display icons or text after input box Configuration Guide
ignoreboolean|'hidden'Whether to ignore this field when submitting the form, when equal to 'hidden', ignore when component is hidden

General Configuration

Property NameTypeDescription
propsobjectSet component props properties to control component behavior. The configuration of props depends on the specific component's properties, such as placeholder, maxlength, etc. for input components.
classobject|string|ArraySet component class for custom styling.
idstringSet component id
styleobject|stringSet component inline styles.
onobjectSet component event listeners, e.g., input event, change event, etc.
directivesobjectSet component custom directives.
slotstringSet component slot name, applicable to nested components.

Extended Configuration

Property NameTypeDescription
cacheboolWhether to cache the component, only trigger rendering once. When set to true, the component will only render once and will not re-render due to data changes.
computedobjectDynamically calculate specified fields of the component, automatically recalculate and update results when dependent values change
componentobject|ComponentDefine custom component for rendering current rule. You can specify custom components in rules without global registration
renderSlotsObjectRender component slots through functions
validateArraySet form component validation rules. You can specify multiple validation rules to ensure the correctness of form input
optionsArray|FunctionSet options selection items for components like radio, select, checkbox, etc.
optionsTostringSet the position where options configuration item is inserted
injectbool|ObjectWhether to enable event injection
effectobjectSet custom properties for extending component functionality or interacting with external systems
updatefunctionSet update callback function for dynamically updating component content Configuration Guide
linkArrayConfigure which field changes will trigger the current component's update callback
colobjectSet component layout rules, usually used in grid systems
wrapobjectSet component FormItem configuration to control FormItem behavior and styles
syncArrayConfigure prop.sync, set the names of properties in props that need two-way binding
controlobjectControl the display and hiding of other components. Applicable to form linkage scenarios Configuration Guide
childrenArray<rule|string|maker>Set component slot content, default is default
emitArrayEvent names to listen to using emit method, can be used with emitPrefix parameter
emitPrefixobjectCustom component emit event prefix, default is the component's field field
hookobjectSet component lifecycle events

Examples

Slot Configuration

Configure the prepend slot of the input component

js
const rule = {
    type: 'input',
    children: [
        {
            type: 'span',
            slot: 'prepend',
            children: ['https://']
        }
    ]
}

Style Configuration

Configure text styles

js
const rule = {
    type: 'span',
    style: {
        color: 'red',  
    },
    class: 'text-red',
    children: ['https://']
}

Directive Configuration

Configure the pin directive of the component

js
// Custom directive
const pin = {
    mounted() { /* ... */ },
    updated() { /* ... */ }
}


// <div v-pin:top.animate="200"></div>
const rule = {
    type: 'div',
    directives: {
        pin: {
            value: 200,
            arg: 'top',
            modifiers: { animate: true }
        }
    }
}

Event Configuration

Configure the change event of the input component

js
const rule = {
    type: 'input',
    on: {
       change() {
           // todo
       } 
    }
}

Hide Component

js
// <el-input v-if="false" />
const rule = {
    type: 'input',
    hidden: true
}
js
// <el-input v-show="false" />
const rule = {
    type: 'input',
    display: false
}

Configure Label Style

Change input component label text to red

js
const rule = {
    type: 'input',
    wrap: {
        class: 'text-red',
        style: {
            color: 'red'
        }
    }
}

Component Validation

Configure input component to be required and must be 8 digits

js
const rule = {
    type: 'input',
    field: 'ID',
    validate: [
        {reuqired: true, message: 'Enter ID'},
        {len: 8, message: 'Must enter 8-digit ID'}
    ]
}

v-model

When the component supports modelValue and update:modelValue, component data can be obtained through the combination of field and value.

js
// <el-input v-model="formData.ID" />
const rule = {
    type: 'el-input',
    field: 'ID',
    value: ''
}

Configure Component Options via options

Configure selection component option data through the options field

js
// <fc-select options="[]" />
const rule = {
    type: 'fc-select',
    options:  [],
    optionsTo: 'props.options'
}

Modify Component Layout

Adjust component layout configuration to achieve a layout with two input boxes displayed side by side per row.

js
// <el-col :span="12"><elInput v-model="formData.input1"></el-col>
// <el-col :span="12"><elInput v-model="formData.input2"></el-col>
const rule = [
    {
        type: 'input',
        field: 'input1',
        col: {
            span: 12,
        }
    },
    {
        type: 'input',
        field: 'input2',
        col: {
            span: 12,
        }
    }
]

FormCreate is an open-source project released under the MIT License. Free for personal and commercial use.