Skip to content

Form Configuration (option)

When using FormCreate to generate forms, you can manage form behavior and appearance through global configuration. This guide details how to set global configuration and how to use these configurations to enhance form flexibility and controllability.

Setting Global Configuration

Component Mode

In component mode, pass global configuration through the option property:

html
<form-create :option="option"></form-create>

Global Method

When creating forms using global methods, you can also pass global configuration:

js
formCreate.create(rule, option)

Example Demo

The following is a simple example showing how to set form behavior using global configuration:

Option Configuration List

The table details various Option configuration properties of the FormCreate component, including global component configuration, event injection, form validation, multi-language support, layout control, and other core functions, helping developers comprehensively customize form behavior and appearance.

Property NameTypeDefault ValueDescription
global{ '*': VNodeRule; [componentName: string]: VNodeRule }-Set component default configuration: * is global common configuration, key names support specifying component names (such as input) to override specific component configuration.
injectEventObject | BooleanfalseGlobally enable event injection (such as onChange), if it's an object, you can customize event names and handling logic.
previewBooleanfalseWhether to enable preview mode
forceCoverValueBooleanfalseWhether v-model forcibly overwrites undefined field values (if false, original values of undefined fields are retained).
appendValueBooleanfalseWhether to retain field values not defined in rules in v-model (if false, these fields are filtered when submitting).
ignoreHiddenFieldsBooleantrueWhether to ignore fields in hidden state when submitting forms (if false, hidden field values are included).
validateOnSubmitBooleantrueControl whether to automatically trigger form validation before form submission.
formDataObject{}Set form initial values (not two-way binding, only effective during initialization).
language{ [name: string]: { [id: string]: string } }-Configure multi-language content, keys are language names (such as zh-CN), values are text ID and translation key-value pairs.
elElement-Specify DOM element for form mounting (required in non-component mode).
onSubmit(formData: Object, api: Api) => void-Form submit callback, receives form data and API object.
beforeSubmit(formData: Object, form: {api: Api}) => void| boolean | Promise<any>-Triggered before form submission.
beforeFetch(config: FetchEffectOption, ctx: { api: Api, rule: Rule }) => void-Modify request configuration before fetch request (such as adding request headers).
mounted(api: Api) => void-Triggered after form creation is successful, used to get API instance or execute initialization logic.
reload(api: Api) => void-Triggered after form reload (such as after calling api.reload()).
onValidateFail(e: Object, form: {api: Api}) => void-Triggered when form validation fails.
colBoolean | (ColProps & { show?: Boolean })trueConfigure Col layout component (grid system), show: false can disable layout.
rowBoolean | (RowProps & { show?: Boolean })trueConfigure Row layout component, control form row arrangement.
infoBoolean | (VNodeRule & { show?: Boolean, native?: Boolean, ... })trueConfigure floating hint component, icon can customize icon, align controls alignment direction.
wrapBoolean | (VNodeRule & FormItemProps & { show?: Boolean })trueConfigure FormItem container style, supports overriding label width, validation hint style, etc.
formFormProps-Set form root component properties (such as labelWidth, size, etc., depends on specific UI framework).
titleBoolean | (VNodeRule & { show?: Boolean, native?: Boolean, ... })trueConfigure form item label (label), native: false can disable native label style.
submitBtnBoolean | (ButtonProps & { click?: Function, show?: Boolean, ... })trueConfigure submit button, innerText can modify button text, show: false hides button.
resetBtnBoolean | (ButtonProps & { click?: Function, show?: Boolean, ... })trueConfigure reset button, supports custom click events or hiding button.

UI Framework Configuration

FormCreate supports multiple UI frameworks, and each framework's configuration items are slightly different. The following are global configuration guides for each framework:

Examples

Configure Form Styles

Control form styles through form field

vue
<template>
    <div>
        <form-create :option="options"/>
    </div>
</template>
<script setup>
    import {ref} from 'vue';
    const options = ref({
        form:  {
            labelPosition: 'top',
            labelWidth: '150px'
        },
    })
</script>

Configure Form Default Values

Control default values through formData field, priority is lower than v-model

vue
<template>
    <div>
        <form-create :option="options"/>
    </div>
</template>
<script setup>
    import {ref} from 'vue';
    const options = ref({
        formData: {
            goods_name: 'default value'
        }
    })
</script>

Ignore Hidden Form Component Fields When Submitting Form

Control whether to submit hidden fields when submitting form through ignoreHiddenFields field

vue
<template>
    <div>
        <form-create :option="options"/>
    </div>
</template>
<script setup>
    import {ref} from 'vue';
    const options = ref({
        ignoreHiddenFields: true,
    })
</script>

Disable Default Form Validation Before Form Submission

Control whether to submit hidden fields when submitting form through validateOnSubmit field

vue
<template>
    <div>
        <form-create :option="options"/>
    </div>
</template>
<script setup>
    import {ref} from 'vue';
    const options = ref({
        validateOnSubmit: true,
    })
</script>

Component Default Configuration

Configure component common configuration through global field

vue
<template>
    <div>
        <form-create :option="options"/>
    </div>
</template>
<script setup>
    import {ref} from 'vue';
    const options = ref({
         global: {
             upload: {
                 props: {
                     headers: {
                        token: getToken()  
                     },
                     onSuccess(file, res) {
                         file.url = res.data.url
                     }
                 }
             }
         },
    })
</script>

Data Structure

The following is an example of the data structure of the Option configuration item in the ElementPlus version:

ts
type Option = {
    //Set component default configuration
    global?: {
      '*':VNodeRule;
      [componentName:string]: VNodeRule;
    };
    //Globally enable event injection
    injectEvent?: Object|Boolean;
    //Whether to enable preview mode
    preview?: Boolean;
    //Whether v-model updates undefined field values
    forceCoverValue?: Boolean;
    //Whether to save fields not in rules in v-model
    appendValue?: Boolean;
    //Whether to ignore fields in hidden state when submitting forms
    ignoreHiddenFields?: Boolean;
    //Control whether to trigger form validation before form submission
    validateOnSubmit?: Boolean;
    //Set form initial values (not two-way binding)
    formData?: Object;
    //Configure form multi-language content
    language: {
        //Language name: {[ID]: text}
        [name: string] : {
            [id: string]: string;
        };
    },
    //Used to specify the DOM element for form mounting. Not needed in component mode.
    el?: Element;
    //Callback function when form is submitted.
    onSubmit?: (formData: Object, api: Api) => void;
    //Callback triggered before fetch request, used to modify request configuration.
    beforeFetch?: (config: FetchEffectOption, form: {
      api: Api,
      rule: Rule
    }) => void;
    //Triggered before form submission
    beforeSubmit?: (formData: Object, form: {
        api: Api
    }) => void | boolean | Promise<any>;
    //Triggered when form validation fails
    onValidateFail?: (e: Object, form: {api: Api}) => void;
    //Callback function after form creation is successful.
    mounted?: (api: Api) => void;
    //Callback function after form reload.
    reload?: (api: Api) => void;
    //Set overall configuration of Col component.
    col?: Boolean | ColProps & {
        show?: Boolean;
    };
    //Set overall configuration of Row component.
    row?: Boolean | RowProps & {
        show?: Boolean;
    };
    //Set configuration of floating hint component.
    info?: Boolean | VNodeRule & {
        show?: Boolean;
        native?: Boolean;
        icon?: string;
        align?: 'left' | 'right';
        info?: string;
    };
    //Set overall configuration of FormItem component.
    wrap?: Boolean | (VNodeRule & FormItemProps & {show?: Boolean});
    //Set overall configuration of form, corresponding to UI framework's form component.
    form?: FormProps;
    //Set configuration of submit button (Button component).
    submitBtn?: Boolean | ButtonProps & {
        click?: Function;
        innerText?: string;
        show?: Boolean;
    };
    //Set configuration of reset button (Button component).
    resetBtn?: Boolean | ButtonProps & {
        click?: Function;
        innerText?: string;
        show?: Boolean;
    };
    //Set configuration of formItem component's label area.
    title?: Boolean | VNodeRule & {
        show?: Boolean;
        native?: Boolean;
        title: string;
    };
}

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