Skip to content

API Documentation

Component Props

el-form-renderer

The el-form-renderer component supports all el-form attributes and extends with the following:

PropDescriptionTypeDefault
contentForm item configuration array, each item represents an atomic form itemContent[]-
disabledDisable all form itemsbooleanfalse
FormData (v-model)Form data objectobject-

Content Configuration

Configuration object for each form item, supports the following properties:

Basic Properties

PropertyDescriptionTypeRequiredDefault
idUnique identifier for the form item, used to store the valuestringYes-
typeForm component type, such as input, select, date-picker, etc.stringYes-
labelForm item label textstringNo-
defaultDefault valueanyNo-

Component Configuration

PropertyDescriptionTypeDefault
elProperties of the specific form component, such as placeholder for el-inputobject-
attrsNative HTML attributesobject-
componentCustom component for rendering locally registered componentsVue Component-

Options Configuration

PropertyDescriptionTypeDefault
optionsOptions configuration for select-type componentsArray<{label: string, value?: any}>-
remoteRemote data configurationRemoteConfig-

Display Control

PropertyDescriptionTypeDefault
hiddenFunction to control whether the form item is hidden(formValue: Object, item: Content) => boolean-
readonlyRead-only modebooleanfalse
disabledDisabled statebooleanfalse

Data Processing

PropertyDescriptionTypeDefault
inputFormatFunction to process input values, used to format incoming data(obj: any) => any-
outputFormatFunction to process output values, used to format component return data(val: any) => any-
trimWhether to automatically trim whitespace on change eventbooleantrue

Validation Rules

PropertyDescriptionTypeDefault
rulesForm validation rules, same as el-form-item's rulesobject | array-
overrideRulesWhether to override built-in validation rules in custom componentsbooleanfalse

Event Listeners

PropertyDescriptionType
onListen to events emitted by form item components{ [eventName: string]: (args: any[], updateForm: function) => void }

Group Configuration

PropertyDescriptionType
itemsUsed when type="group", defines child form itemsContent[]

Remote Configuration

Remote data configuration object for dynamically loading form item data:

PropertyDescriptionTypeDefault
urlRemote API addressstring-
propProperty name to injectstring'options'
requestCustom request method() => Promise() => this.$axios.get(url).then(resp => resp.data)
dataPathData path in response bodystring''
onResponseFunction to process response data(resp: any) => any-
onErrorFunction to handle request errors(error: Error) => voiderror => console.error(error.message)
labelKey name for label in remote data (only for select, radio-group, checkbox-group)string'label'
valueKey name for value in remote data (only for select, radio-group, checkbox-group)string'value'

Component Methods

el-form-renderer supports all el-form methods:

MethodDescriptionParameters
validateValidate the entire form(callback?: (valid: boolean, fields?: object) => void) => Promise<boolean>
validateFieldValidate specific form fields(props: string | string[], callback?: (valid: boolean, fields?: object) => void) => Promise<boolean>
resetFieldsReset form fields(props?: string | string[]) => void
scrollToFieldScroll to specified form field(prop: string) => void
clearValidateClear validation results(props?: string | string[]) => void

Extended Methods

MethodDescriptionParameters
updateFormBatch update form data(data: object) => void
setOptionsDynamically set options for select-type components(id: string, options: Array) => void
getFormValueGet form data() => object
getComponentByIdGet form item component instance by id(id: string) => ComponentInstance

Component Slots

Slot NameDescription
defaultContent inserted at the bottom of the form
id:xxxContent inserted before the form item with id 'xxx'

Type Definitions

Content Interface

typescript
interface Content {
  // Required properties
  id: string
  type: string

  // Optional basic properties
  label?: string
  default?: any
  readonly?: boolean
  disabled?: boolean

  // Group related
  items?: Content[]

  // Options configuration
  options?: Array<{ label: string; value?: any }>
  remote?: RemoteConfig

  // Display control
  hidden?: (formValue: Object, item: Content) => boolean

  // HTML and component properties
  attrs?: object
  el?: object
  component?: Vue

  // Data processing
  inputFormat?: (obj: any) => any
  outputFormat?: (val: any) => any
  trim?: boolean

  // Validation
  rules?: object | array
  overrideRules?: boolean

  // Events
  on?: {
    [eventName: string]: (args: any[], updateForm: function) => void
  }

  // Deprecated
  enableWhen?: object | string
  atChange?: (id: string, value: any) => void
}

RemoteConfig Interface

typescript
interface RemoteConfig {
  url: string
  prop?: string
  request?: () => Promise<any>
  dataPath?: string
  onResponse?: (resp: any) => any
  onError?: (error: Error) => void
  label?: string
  value?: string
}

Supported Form Component Types

Common type values:

  • input - Input
  • textarea - Textarea
  • select - Select
  • radio-group - Radio Group
  • radio-button - Radio Button Group
  • checkbox-group - Checkbox Group
  • checkbox-button - Checkbox Button Group
  • switch - Switch
  • date-picker - Date Picker
  • time-picker - Time Picker
  • time-select - Time Select
  • cascader - Cascader
  • input-number - Input Number
  • rate - Rate
  • color-picker - Color Picker
  • slider - Slider
  • group - Group (for creating nested objects)

Examples

Basic Usage

vue
<template>
  <el-form-renderer :content="content" v-model:FormData="formData" label-width="100px" />
</template>

<script setup>
import { reactive } from "vue"

const formData = reactive({
  name: "",
  age: 0
})

const content = [
  {
    type: "input",
    id: "name",
    label: "Name",
    rules: [{ required: true, message: "Please enter name" }]
  },
  {
    type: "input-number",
    id: "age",
    label: "Age",
    el: { min: 0, max: 150 }
  }
]
</script>

Using Methods

vue
<template>
  <el-form-renderer ref="formRef" :content="content" v-model:FormData="formData">
    <el-button @click="handleSubmit">Submit</el-button>
    <el-button @click="handleReset">Reset</el-button>
  </el-form-renderer>
</template>

<script setup>
import { ref, reactive } from "vue"

const formRef = ref()
const formData = reactive({ name: "" })
const content = [
  {
    type: "input",
    id: "name",
    label: "Name",
    rules: [{ required: true, message: "Please enter name" }]
  }
]

const handleSubmit = async () => {
  const valid = await formRef.value.validate()
  if (valid) {
    console.log("Form data:", formData)
  }
}

const handleReset = () => {
  formRef.value.resetFields()
}
</script>