Object MethodUI and Dialogs

Form.validate(schema)

Checks a form schema without opening a window; returns true, or false and an explanation.

Documented for app version 1.0.50Updated:

Detailed Explanation

This section explains when to use the API, how to call it, and which structures it works best with in production flow.

How To Call It

Form is a global table: call its functions with a dot (Form.<method>(...)), not a colon. Requires Macro Handler 1.0.51 or later.

When To Use It

Checks a form schema without opening a window; returns true, or false and an explanation.

Parameters and Return

The schema contains a fields array and optional title, positive, negative, surface and timeoutMs. Field types include input, checkbox, radio, select, multi, tags, signature, image, color, file, datetime, range, slider, recorder, navigation and agentDetect; layouts include text/spacer, row/section and tabs. Keys must be unique; radio/select/tabs selected indices start at 1. Up to 256 fields and 8 layout levels are accepted.

Best Combined With

Use visibleWhen = {{key="enabled", value=true}} for conditional visibility and required, pattern, help and style for field rules. Cyclic visibility and invalid schemas are rejected. Form uses the shared Setting renderer; it does not execute custom HTML or external code. Checkbox defaults are boolean; other values are strings. Radio/select and tabs return the selected label; multi returns comma-separated labels. Use tonumber before numeric operations.

Example Usage

The snippet below is a starter pattern that can be applied directly in runtime flow.

local schema = {title = "Run settings", positive = "Start", negative = "Cancel", fields = {
  {type = "checkbox", key = "enabled", label = "Enabled", value = true},
  {type = "input", key = "target", label = "Target", value = "Example", required = true},
  {type = "slider", key = "count", label = "Count", min = 1, max = 10, value = 3},
  {type = "select", key = "mode", options = {"Safe", "Fast"}, selected = 2},
  {type = "multi", key = "steps", label = "Steps", options = {"Scan", "Report"}, value = {"Scan"}}
}}
local ok, message = Form.validate(schema)
print(ok, message)

Copyable Progressive Examples

From foundation to combined usage, each level is provided as a separate code block so you can copy the level you need and adapt it directly.

Foundation

Shows the shortest direct way to call the API.

Foundation
local schema = {title = "Run settings", positive = "Start", negative = "Cancel", fields = {
  {type = "checkbox", key = "enabled", label = "Enabled", value = true},
  {type = "input", key = "target", label = "Target", value = "Example", required = true},
  {type = "slider", key = "count", label = "Count", min = 1, max = 10, value = 3},
  {type = "select", key = "mode", options = {"Safe", "Fast"}, selected = 2},
  {type = "multi", key = "steps", label = "Steps", options = {"Scan", "Report"}, value = {"Scan"}}
}}
local ok, message = Form.validate(schema)
print(ok, message)

Simple

Wraps the base call with minimal flow control.

Simple
local stepOk = true
local schema = {title = "Run settings", positive = "Start", negative = "Cancel", fields = {
  {type = "checkbox", key = "enabled", label = "Enabled", value = true},
  {type = "input", key = "target", label = "Target", value = "Example", required = true},
  {type = "slider", key = "count", label = "Count", min = 1, max = 10, value = 3},
  {type = "select", key = "mode", options = {"Safe", "Fast"}, selected = 2},
  {type = "multi", key = "steps", label = "Steps", options = {"Scan", "Report"}, value = {"Scan"}}
}}
local ok, message = Form.validate(schema)
print(ok, message)
if stepOk then
  wait(200)
end

Practical Flow

A practical pattern for real macros with pcall, logging, and guards.

Practical Flow
local ok, err = pcall(function()
  local schema = {title = "Run settings", positive = "Start", negative = "Cancel", fields = {
    {type = "checkbox", key = "enabled", label = "Enabled", value = true},
    {type = "input", key = "target", label = "Target", value = "Example", required = true},
    {type = "slider", key = "count", label = "Count", min = 1, max = 10, value = 3},
    {type = "select", key = "mode", options = {"Safe", "Fast"}, selected = 2},
    {type = "multi", key = "steps", label = "Steps", options = {"Scan", "Report"}, value = {"Scan"}}
  }}
  local ok, message = Form.validate(schema)
  print(ok, message)
end)

if not ok then
  print("API step failed: Form.validate: " .. tostring(err))
  requestStop()
end

Detailed

This level packages the API into a reusable helper with error reporting.

Detailed
-- Form.validate(schema)
local function run_validate_step()
  local schema = {title = "Run settings", positive = "Start", negative = "Cancel", fields = {
    {type = "checkbox", key = "enabled", label = "Enabled", value = true},
    {type = "input", key = "target", label = "Target", value = "Example", required = true},
    {type = "slider", key = "count", label = "Count", min = 1, max = 10, value = 3},
    {type = "select", key = "mode", options = {"Safe", "Fast"}, selected = 2},
    {type = "multi", key = "steps", label = "Steps", options = {"Scan", "Report"}, value = {"Scan"}}
  }}
  local ok, message = Form.validate(schema)
  print(ok, message)
end

local ok, err = pcall(run_validate_step)
if not ok then
  toast("Step failed")
  print(err)
end

Combined

Combines the API with related structures to form a more realistic workflow.

Combined
local schema = {title = "Run settings", positive = "Start", negative = "Cancel", fields = {
  {type = "checkbox", key = "enabled", label = "Enabled", value = true},
  {type = "input", key = "target", label = "Target", value = "Example", required = true},
  {type = "slider", key = "count", label = "Count", min = 1, max = 10, value = 3},
  {type = "select", key = "mode", options = {"Safe", "Fast"}, selected = 2},
  {type = "multi", key = "steps", label = "Steps", options = {"Scan", "Report"}, value = {"Scan"}}
}}
local ok, message = Form.validate(schema)
print(ok, message)