logologo
开始
手册
开发
插件
API
首页
English
简体中文
日本語
한국어
Español
Português
Deutsch
Français
Русский
Italiano
Türkçe
Українська
Tiếng Việt
Bahasa Indonesia
ไทย
Polski
Nederlands
Čeština
العربية
עברית
हिन्दी
Svenska
开始
手册
开发
插件
API
首页
logologo
RunJS 概述
导入模块
容器内渲染

全局变量

window
document
navigator

ctx

ctx.blockModel
ctx.collection
ctx.collectionField
ctx.dataSource
ctx.dataSourceManager
ctx.element
ctx.exit()
ctx.exitAll()
ctx.filterManager
ctx.form
ctx.getModel()
ctx.getValue()
ctx.getVar()
ctx.i18n
ctx.importAsync()
ctx.initResource()
ctx.libs
ctx.location
ctx.logger
ctx.makeResource()
ctx.message
ctx.modal
ctx.model
ctx.notification
ctx.off()
ctx.on()
ctx.openView()
ctx.render()
ctx.request()
ctx.requireAsync()
ctx.resource
ctx.route
ctx.router
ctx.setValue()
ctx.sql
ctx.t()
ctx.view
Previous Pagectx.filterManager
Next Pagectx.getModel()

#ctx.form

当前区块内的表单实例。

在表单相关的 JSField、JSItem 中,可使用 ctx.form:

  • 读取当前表单所有字段的值
  • 更新其他字段值实现字段联动
  • 触发表单校验或提交逻辑

#类型定义

form: FormInstance<any>;

interface FormInstance<Values = any> {
  getFieldValue: (name: NamePath<Values>) => StoreValue;
  getFieldsValue: (() => Values) & ((nameList: NamePath<Values>[] | true, filterFunc?: FilterFunc) => any) & ((config: GetFieldsValueConfig) => any);
  getFieldError: (name: NamePath<Values>) => string[];
  getFieldsError: (nameList?: NamePath<Values>[]) => FieldError[];
  getFieldWarning: (name: NamePath<Values>) => string[];
  isFieldsTouched: ((nameList?: NamePath<Values>[], allFieldsTouched?: boolean) => boolean) & ((allFieldsTouched?: boolean) => boolean);
  isFieldTouched: (name: NamePath<Values>) => boolean;
  isFieldValidating: (name: NamePath<Values>) => boolean;
  isFieldsValidating: (nameList?: NamePath<Values>[]) => boolean;
  resetFields: (fields?: NamePath<Values>[]) => void;
  setFields: (fields: FieldData<Values>[]) => void;
  setFieldValue: (name: NamePath<Values>, value: any) => void;
  setFieldsValue: (values: RecursivePartial<Values>) => void;
  validateFields: ValidateFields<Values>;
  submit: () => void;
}

#常用方法

// 读取当前已注册字段的值(默认仅包含已渲染/已注册的字段)
const values = ctx.form.getFieldsValue();

// 读取所有字段的值(包含已注册但未渲染的字段)
const allValues = ctx.form.getFieldsValue(true);

// 读取单个字段值
const email = ctx.form.getFieldValue('email');

// 批量更新字段值(用于联动场景)
ctx.form.setFieldsValue({
  status: 'active',
  updatedAt: new Date(),
});

// 触发表单校验 / 提交
await ctx.form.validateFields();
await ctx.form.submit();

提示:

  • getFieldsValue() 仅返回当前已渲染/已注册字段的值;getFieldsValue(true) 返回所有字段(含已注册未渲染)
  • 在 JS 字段中,建议用 ctx.form.getFieldsValue() / ctx.form.getFieldValue() 读取其他字段值做计算或联动
  • 不要直接操作 DOM 表单元素,始终通过 ctx.form 读写表单状态