Skip to main content

Form Container

WdForm

Practical Guide

Applicable Scenarios

Quickly implement scenarios for submitting and displaying form data. After binding a data source, components such as single-line inputs, dropdown selections, and submit buttons can be automatically generated based on field types.

Basic Capabilities Description

  1. Creating, updating, or viewing form data via the Form Container
  2. Supports binding data sources of data models or APIs types to achieve data interaction between frontend and backend
  3. Through field properties, you can control the ordering, visibility, and switching of component types for field mapping within internal form components
  4. For more practical instructions, refer to the Practical Guide for Common Form Scenarios

Extended Scenarios Description

Calculating and Displaying User Input Data in Forms

Example

The following is a complex value calculation scenario

  • formArr1: an array where the value of c in each row is the sum of a and b
  • formArr2: an array where the value of c in each row is the sum of a and b
  • sum1: the accumulated sum of the c values in formArr1
  • sum2: the accumulated sum of the c values in formArr2

Configuration Method

  1. As shown, drag the form container and nested components into place, and configure as needed. The expression for the value of c as the sum of a and b in the nested component is implemented as follows:

    Number($w.formArr1.value[$w.index_formArr1].a)*Number($w.formArr1.value[$w.index_formArr1].b)
  2. sum1 is the accumulated sum of the c values in the array formArr1 The expression is implemented as follows:

    $w.form1.value?.formArr1?.reduce((sum, v) => sum +Number(v.c) , 0)
  3. sum1+sum2 The expression is implemented as follows:

    Number($w.form1.value.sum1)+Number($w.form1.value.sum2)
  4. sum1/sum2

    The expression is implemented as follows:

    Number($w.form1.value.sum1)/Number($w.form1.value.sum2)

Dynamically Implementing Data Creation, Update, and Viewing Scenarios Using 1 Form Container Component

Example

Bind a variable to the "Form Scenario" property of the form container, and assign different values to the variable to implement creation, update, and view scenarios.

Configuration Method

  1. Create a variable of type string named sence
  2. Bind the form container to a required data model to render and generate form components, then bind the "Form Scenario" property of the form container to the sence variable
  3. In different business scenarios, assign different values to the sence variable to enable the form container to serve different purposes.
    1. Assign "create" to the form container to set it as the creation scenario for submitting new data.
    2. Assign "edit" to the form container to set it as the update scenario for editing and updating existing data.
    3. Assign "read" to the form container to set it as the view scenario for displaying and viewing existing user data.

Binding Expression

Expressions can be static data or variables. You can connect to built-in data tables and external APIs, etc., via Query, and then bind a query in the expression.

Format Requirements for Method Parameters When Data Source Type is APIs (This feature has been deprecated in the latest version)

  1. When the Form Scenario is "creation", configure

    1. Add a method: Business-required form fields must be flattened at the root node of input parameters. Example:
  2. When the Form Scenario is "update", configure

    1. Query method: Requires form fields to be flattened at the root node of output parameters. Example:
    2. Update method: Business-required form fields must be flattened at the root node of input parameters. Example:
  3. When the Form Scenario is "view", configure

    1. Query method: Requires form fields to be flattened at the root node of output parameters. Example:

Property Introduction

组件接收的外部传入的属性

属性名
属性标识
类型
说明
表单场景formTypestring

默认值:"create"

数据源datasourceTypestring

默认值:"model"

数据模型dataSourceNamestring

示例:""

字段管理fieldsarray
数据标识(_id)_idstring

示例:""

表单值valueobject

自动填充表单输入数据。每个键值都分配给具有匹配的表单数据键的输入。

查询方法methodGetItemstring

示例:""

查询入参paramGetItemobject

直接输入对象字面量, 如 {status: 1}

示例:{}

新增方法methodCreatestring

示例:""

更新方法methodUpdatestring

示例:""

PC端表单列数colNumstring

默认值:"1"

PC端组件宽度lgWidthstring

尺寸配置仅对PC端生效

默认值:"hundred"

示例:"md"

标题位置layoutstring

设置当前容器内表单类组件的标题和输入选择区域为左右布局或上下布局

默认值:"horizontal"

Event Introduction

组件暴露的事件,可以监听组件的事件来触发一些外部的动作

事件名
事件code
事件出参 event.detail
适用情况
说明
提交submit兼容三端

-

校验成功validateSuccessobject
  • errors: object 校验错误
    兼容三端

    -

    校验失败validateFailobject
    • errors: object 校验错误
      兼容三端

      -

      查询为空queryEmptyobject
      • data: object

        查询结果

        兼容三端

        -

        查询成功querySuccessobject
        • data: object

          查询结果

          兼容三端

          -

          查询失败queryFailobject
          • error: object 错误信息

            组件内部抛出的错误信息

            • requestid: string请求 id

              如果是接口相关错误会提供请求 id

            • message: string错误提示信息
            • code: string错误码
            • original: 原始错误
          兼容三端

          -

          Property API

          通过属性 API,可以获取组件内部的状态和属性值,可以通过$w.componentId.propertyName 来访问组件内部的值,如 $w.input1.value ,详请请参考 属性 API

          只读属性名
          属性标识
          类型
          说明
          是否禁用提交isDisabledSubmitboolean

          表单是否禁用提交

          表单场景formTypestring
          数据模型dataSourceNamestring
          标题位置layoutstring

          设置当前容器内表单类组件的标题和输入选择区域为左右布局或上下布局

          错误信息errorsobject

          提交的校验错误信息对象

          数据源初始值remoteValueobject

          数据源初始化请求的初始值

          表单前端值valueobject

          当前前端表单状态值

          数据源类型信息dataSourceProfile

          数据源信息

          数据源方法提交入参submitParamsobject

          数据源方法提交入参, 绑定query的时候没有

          清除校验clearValidateobject

          清除校验

          Method API

          通过方法 API,可以通过程序触发组件内部的方法,比如提交表单,显示弹窗等, 可以通过$w.componentId.methodName来调用组件方法,如 $w.form1.submit()

          方法名
          方法标识
          参数
          方法说明
          设置值setValueobject
          • value: object

            表单的值

            通过 $w.form1.setValue({a:'111'}) 设置组件值

            提交submit

            通过 $w..submit() 触发表单提交

            置空clearValueobject
            • isImmediate: boolean是否开启防抖

            通过 $w..clearValue() 将表单置空

            清空校验clearValidate

            通过 $w..clearValidate() 清空校验

            校验validate

            通过 $w..validate() 触发表单提交

            禁用提交disableSubmit

            通过 $w..disableSubmit 设置禁用表单提交

            启用提交enableSubmit

            通过 $w..enableSubmit 设置启用表单提交

            Style API

            通过样式 API,可以覆盖组件中内部元素的样式来实现自定义,例如在低代码编辑器中中通过 #wd-page-root .wd-btn 即可为所有的按钮组件编写样式,通过 :scope 可以控制单个组件样式, 详细说明请参考样式 API

            名称
            类名
            说明和示例
            根元素.wd-form表单组件根元素
            /* :scope 指的是当前组件元素 */
            /* 具体可参考样式 API 文档 */
            :scope.wd-form {
              /* 在这里编写CSS 样式 */
            }
            PC 端表单根元素.wd-pc-form可以为 PC 端的表单编写样式
            /* :scope 指的是当前组件元素 */
            /* 具体可参考样式 API 文档 */
            :scope.wd-pc-form {
              /* 在这里编写CSS 样式 */
            }
            H5 端表单根元素.wd-h5-form可以为 H5 端的表单编写样式
            /* :scope 指的是当前组件元素 */
            /* 具体可参考样式 API 文档 */
            :scope.wd-h5-form {
              /* 在这里编写CSS 样式 */
            }
            小程序端表单根元素.wd-mp-form可以为小程序端的表单编写样式
            /* :scope 指的是当前组件元素 */
            /* 具体可参考样式 API 文档 */
            :scope.wd-mp-form {
              /* 在这里编写CSS 样式 */
            }

            了解样式 API

            Frequently Asked Questions

            When using data container components, must other components be placed inside them?

            The Data Container is the core component for page-data interaction, capable of directly querying and displaying data. On our platform, multiple data container components are available, such as ListView, DataView, WdForm, and WdTable.

            The Data Container enables deep integration of data and page components. This greatly simplifies data operation processes. In addition to providing data, the Data Container also supports common page interaction features, such as pagination loading, loading status, and error prompts.

            When using data containers, if a component relies on data returned by the data container, it is recommended to place the component inside the data container component. For example, to display an image from a specific field in a data detail, it is better to place the component inside the corresponding data container. This way, the data container can automatically handle loading states, loading failures, etc., without needing to worry about rendering timing.

            If you only need to use the data container to obtain data without performing UI interactions, you can directly use the Query function to query data.

            How to prevent duplicate form submissions?

            On the form submit button, create a Boolean variable with a default value of false and bind it to the button's disabled property. When the form container's submit event triggers, perform a "variable assignment" to set this variable to true. Then, in the success branch, call the data source method to submit the data. After successful submission, trigger another "variable assignment" to reset the variable to false. During submission, when the variable value is true, the button remains disabled, thus preventing duplicate submissions.

            How do you refresh other components when an event is triggered?

            When you need to refresh other components upon an event trigger, bind a variable to the target component's property, for example by referencing this variable in a filter parameter expression. Then change the variable's value in the triggering event to update the corresponding target component's content.