FormObject[{"name1"type1,"name2"type2,…}]
表示一个表单,含有名为 namei 的、接受 typei 类型的数据的字段.
FormObject[{{"name1",label1}type1,…}]
使用 labeli 作为名为 namei 的字段的标签.
FormObject[{"name1"assoc1,…,objj,…}]
对字段使用完整规范 associ,并且使用 objj 作为表单布局的一部分.
FormObject
字段类型列表 »FormObject[{"name1"type1,"name2"type2,…}]
表示一个表单,含有名为 namei 的、接受 typei 类型的数据的字段.
FormObject[{{"name1",label1}type1,…}]
使用 labeli 作为名为 namei 的字段的标签.
FormObject[{"name1"assoc1,…,objj,…}]
对字段使用完整规范 associ,并且使用 objj 作为表单布局的一部分.
更多信息和选项
- FormObject[…] 在笔记本界面中显示为具有可填充字段的表单,可以或者输入文本,或者使用其他控件.
- FormObject 可用于在 FormFunction 中指定表单结构.
- 在计算时,FormObject[…] 转化为 FormObject[fields],其中 fields 中的每个项目具有格式为 "namei"->associ 的完整规范. 完整规范 associ 的元素包括:
-
"Input" 字段的原始输入,如果有(通常是一个字符串) "Interpreter" 用于字段的解释器 "Required" 字段是否要求输入 "Default" 字段的默认值,如果有 "Control" 给字段提供的控件类型 "AutoSubmitting" 在字段中进行输入是否会自动提交表单 "Disambiguation" 是否要求为解释消除歧义 "Label" 字段的标签 "Hint" 显示在输入字段内的提示信息 "Masked" 是否遮住输入字段中的输入 "Help" 与字段相关联的帮助 - Setting[FormObject[…]] 给出格式为 <|"name1"->val1,…|> 的关联,其中 vali 是通过解释 "Input" 元素获得的值,根据 "Interpreter" 与 "namei" 关联,"Input" 元素与每个 "namei" 关联.
- 如果 "Input" 缺失,或者 "Interpreter" 失败,那么 "Value" 的值是 Failure 对象.
- FormObject[…][<|"name1"->input1,…|>] 产生 FormObject[…],其中对应于包括的每个 namei 的 "Input" 被 inputi 替换.
- 在 FormObject[{…,objj,…}] 中,objj 可以是 Delimiter,表示水平分隔符.
- 它们也可以是头部为 Style、Row、Item、Text、ExpressionCell 和 TextCell 的表达式,以及诸如 TabView 和 Grid 这样的的视窗和布局构件.
- 在 FormObject[{"name1"->type1,…}] 中,typei 用于对完整规范中的 "Interpreter" 给出值.
- 类型规范的可能形式为:
-
"form" 由 Interpreter 支持的任何形式 Restricted[…]等 由 Interpreter 支持的任意限制形式等 Interpreter[…] 任意完整 Interpreter 对象 tfun 将应用于原始输入的任意函数 - 如果只给出 "namei",没有给出 typei 规则,则假定解释器是 Identity,因此值恰好是以相应 "Input" 元素给出.
- 默认情况下,用于每个字段的控件由指定的输入类型确定. 对于类型为 "String",单行输入默认情况下位于网络上;输入 "Text" 产生多行文本字段.
- 显式给出与 "Control" 相关联的数值的可能的控件包括 InputField、PopupMenu、Slider 和 RadioButtonBar.
- 规范 "namei"->AutoSubmitting[typei] 表明如果对与 namei 相关联的字段进行输入,应该自动提交整个表单.
- FormObject[AutoSubmitting[spec]] 表明如果对任意字段进行输入,则自动提交表单.
- FormObject 的整体选项包括:
-
AppearanceRules Automatic 给出整体外观规则的关联 FormLayoutFunction Automatic 产生表单最终布局的函数 PageTheme Automatic 部署网页时使用的整体主题 - AppearanceRules 的设置中的典型元素包括:
-
"Title" None 表单的整体标题 "Description" None 表单顶部包括的描述 "RequiredFieldIndicator" None 显示必填字段的指示器 "SubmitLabel" "Submit" 表单的提交按钮的文本 "AddLabel" "+" 重复元素的添加重复元素的添加按钮的文本 "DeleteLabel" "-" 删除按钮的文本 "AddTooltip" "Add" 添加按钮的工具提示条 "DeleteTooltip" "Remove" 删除按钮的工具提示条 "NextLabel" "Next" 多页面表单的"下一页"按钮的文本 "ItemLayout" "Horizontal" 字段标签和消息的布局 - "ItemLayout" 的可能设置包括 "Horizontal"、"Vertical" 和 "Inline".
范例
打开所有单元 关闭所有单元基本范例 (1)
范围 (19)
form = FormObject[{"first" -> "String", "second" -> "Number"}];form[<|"first" -> "a string", "second" -> "not a number"|>]Setting[%]FormObject["Slider" -> <|"Interpreter" -> Restricted["Number", {1, 100}], "Control" -> Slider|>]FormObject 结构 (1)
有效的 FormObject 计算得到具有所有元信息的 Association 对象:
FormObject["x" -> "Number" -> 3]//InputForm表单提交 (1)
表单元数据 (3)
form = FormObject[{"x" -> "Number", "y" -> "Number"}];
form = form[{"x" -> "3", "y" -> "4"}];
form[All, "Input"]可以提取 "Help"、"Control"、"Interpreter" 等:
form["x", "Interpreter"]form["x", "Control"]form["x", "Failure"]form["x"]可以按照相同方式提取 AppearanceRules:
form = FormObject["Number" -> "Integer", AppearanceRules -> <|"Title" -> "My Form", "ItemLayout" -> "Vertical", "Description" -> "My description"|>, PageTheme -> "Black"]form[AppearanceRules, "Title"]form[AppearanceRules, "ItemLayout"]form[PageTheme]使用 Setting 提取清理后的数据:
FormObject["x" -> "Integer"]//SettingFormObject[{"x" -> "Integer", "y" -> "Integer"}]["x" -> "2"]//SettingFormObject[{"x" -> "Integer", "y" -> "Integer"}]["x" -> "2"]["y" -> "3"]//SettingFormObject["x" -> "Integer" -> 3]//Setting解释器规范 (3)
使用丰富的 Interpreter 规范:
FormObject[{"x" -> Restricted["Integer", {1, 10}]}][{"x" -> "23"}]FormObject["x" -> EntityList["Planet"]]FormObject["switch" -> {"On" -> True, "Off" -> False}]FormObject[{
"Filter" -> ImageEffect[],
"Image" -> Map[
ImageResize[ExampleData[#], {100, 100}]&, ExampleData["TestImage"][[1 ;; 6]]
]
}]toObject[s_String] /; ToUpperCase[s] == s := myObject[s];
toObject[s_] := Failure["InterpretationFailure", <|"MessageTemplate" -> "Please insert an uppercased string"|>];
FormObject[{"x" -> toObject}]["x" -> "abc"]默认 (1)
FormObject[{"first" -> "String", "second" -> "Number" -> 3}]//Setting使用 RuleDelayed 延迟计算:
FormObject[{"date" -> "DateTime" :> DateObject[]}]//Setting输入 (1)
FormObject["Start" -> <|"Input" -> "Initial text"|>]使用 RuleDelayed 强制在每次绘制表单时进行一次新的计算:
FormObject["x" -> <|"Interpreter" -> Restricted["Number", {1, 100}], "Input" :> RandomInteger[{1, 100}], "Control" -> Slider|>]控件 (2)
FormObject[{"x" -> "Color"}]FormObject[{"x" -> "Image"}]FormObject[{"x" -> "Boolean"}]FormObject[{"x" -> <|"Interpreter" -> Restricted["Integer", {1, 10}], "Control" -> Slider|>}]FormObject[<|"x" -> <|"Interpreter" -> {"A" -> 1, "B" -> 2, "C" -> 3}, "Control" -> RadioButtonBar|>|>]FormObject[<|"x" -> <|"Interpreter" -> {"A" -> 1, "B" -> 2, "C" -> 3}, "Control" -> InputField, "Help" -> "Write A, B or C"|>|>]FormObject[{"cat" -> <|"Interpreter" -> "String", "Control" -> Function[Tooltip[InputField[##], EntityValue[Entity["Species", "Species:FelisCatus"], EntityProperty["Species", "Image"]]]]|>}]标签 (1)
FormObject[{"x", "First Name"} -> "String"]可以使用 RuleDelayed 并且返回任意表达式:
FormObject["pc" -> <|"Label" :> "PC: " <> $MachineID|>]FormObject[
"pc" -> <|"Label" :> GeoGraphics[
$GeoLocation,
GeoRange -> Quantity[100, "Meters"]
]|>
]使用 None 删除标签:
FormObject["Query" -> <|"Label" -> None, "Hint" -> "Search"|>]FormObject[{
"First" -> <|"Label" -> None, "Hint" -> "First Name"|>,
"Last" -> <|"Label" -> None, "Hint" -> "Last Name"|>
}]帮助 (1)
FormObject["x" -> <|
"Interpreter" -> Restricted["Number", {0, 100}],
"Help" -> "Enter a positive number lower than 100."
|>]可以使用 RuleDelayed 并且返回任意表达式:
FormObject["City" -> <|
"Interpreter" :> Restricted["City", First@GeoIdentify["Country"]],
"Help" :> "Enter your favorite city in " <> CommonName[First@GeoIdentify["Country"]]
|>]提示 (1)
遮罩 (1)
推广和延伸 (1)
可以在 XMLTemplate 内使用表单;将产生 HTML 代码:
form = FormObject[{"x" -> "String", "y" -> {100, 200, 300}}]XMLTemplate["<form method='GET'><wolfram:slot id='form'/></form>"][<|"form" -> form|>]XMLTemplate["<form method='GET'>
<wolfram:sequence values='Values[#form[All, \"Control\"]]'>
<p>`1`</p>
</wolfram:sequence>
</form>"][<|"form" -> form|>]XMLTemplate["<form method='GET'>
<p><wolfram:expr>#form[\"x\", \"Control\"]</wolfram:expr></p>
<p class='label'><wolfram:expr>#form[\"y\", \"Label\"]</wolfram:expr></p>
<p><wolfram:expr>#form[\"y\", \"Control\"]</wolfram:expr></p>
</form>"][<|"form" -> form|>]选项 (2)
AppearanceRules (1)
FormObject["Start" -> "Country", AppearanceRules -> <|"Title" -> "Where do you want to go?", "Description" -> "Enter a country and we will find you an accomodation"|>]FormObject["Product" -> {"Flyers", "Business cards", "Leflets"}, AppearanceRules -> <|"Title" -> [image], "Description" -> "What do you want to print today?"|>]FormObject[{"First" -> "String", "Second" -> "String"}, AppearanceRules -> <|"ItemLayout" -> "Vertical"|>]FormObject[{"First" -> <|"Help" -> "Insert a value"|>, "Second" -> <|"Help" -> "Insert a value"|>}, AppearanceRules -> <|"ItemLayout" -> "Inline"|>]应用 (1)
使用 FormObject 验证数据:
validator = FormObject[{"Date" -> Interpreter["StructuredDate", DateFormat -> {"MonthNameShort", "Day", "Year"}], "Value" -> "Number"}];
Map[
Setting[validator[{"Date" -> First[#], "Value" -> Last[#]}]]&,
Import["ExampleData/financialtimeseries.csv"]
]//Short属性和关系 (2)
可以使用 FormObject 作为 APIFunction 和 FormFunction 的第一个参数:
signupForm = FormObject[{"Name" -> "String", "Email" -> "EmailAddress", "Password" -> <|"Masked" -> True, "Default" :> Hash[AbsoluteTime[]], "Help" -> "Leave the field blank and a password will be generated"|>}, AppearanceRules -> <|"Title" -> "Signup", "SubmitLabel" -> "Signup now!"|>];
CloudDeploy[FormFunction[signupForm, Identity]]CloudDeploy[APIFunction[signupForm, Identity]]FormObject 的单个字段使用 Interpreter 来转化 Wolfram 语言表达式下的输入:
fo1 = FormObject["x" -> "Country"]fo2 = FormObject["x" -> Interpreter["Country"]]与直接应用 Interpreter 相同:
First[Setting[fo1["x" -> "france"]]] === First[Setting[fo2["x" -> "france"]]] === Interpreter["Country"]["france"]可能存在的问题 (4)
"Hint" 和 "Masked" 只被 InputField 支持:
FormObject[{
"x" -> <|
"Hint" -> "Enter your name"
|>,
"password" -> <|
"Masked" -> True,
"Input" -> "ABC"
|>}
]FormObject[{
"x" -> <|
"Interpreter" -> "Color",
"Hint" -> "Enter your name"
|>,
"y" -> <|
"Interpreter" -> "Image",
"Masked" -> True
|>}
]在 Interpreter 类型中使用 Alternatives 时,可能会得到错误的控制器:
FormObject["x" -> "JSON" | "String"]FormObject["x" -> <|
"Interpreter" -> "JSON" | "String",
"Control" -> InputField
|>]FormObject 是形式规约的惰性表示;因此,其中的按钮不起作用:
FormObject["x" -> RepeatingElement["String"]]当 Interpreter 类型不是支持列表的形式时,仅保留最后一个元素:
FormObject["x" -> "Integer"]["x" -> {"1", "2", "3"}]技术笔记
-
▪
- 创建高级网页表单
文本
Wolfram Research (2014),FormObject,Wolfram 语言函数,https://reference.wolfram.com/language/ref/FormObject.html.
CMS
Wolfram 语言. 2014. "FormObject." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/FormObject.html.
APA
Wolfram 语言. (2014). FormObject. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/FormObject.html 年
BibTeX
@misc{reference.wolfram_2026_formobject, author="Wolfram Research", title="{FormObject}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/FormObject.html}", note=[Accessed: 23-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_formobject, organization={Wolfram Research}, title={FormObject}, year={2014}, url={https://reference.wolfram.com/language/ref/FormObject.html}, note=[Accessed: 23-June-2026]}