Conditional rendering

visible and disabled are the two built-in runtime switches for conditional form behavior. required (UI flag) can be conditional too.

  • visible controls whether a field is rendered at all.
  • disabled keeps the field rendered, but marks it disabled in React Hook Form.

Both options accept either a boolean or a function:

nickname: {
  type: "text",
  visible: true,
},
internalNote: {
  type: "text",
  disabled: (values) => !values.isAdmin,
},

When a function is provided, BlueForm evaluates it reactively from the current form values. No manual useWatch is needed in field components.

Conditions inside array rows and sections

Added in v0.16.

The functions receive a second argument with the field's position:

PropertyExampleDescription
path"rows.2.detail"Full path of the field
name"detail"Config key of the field
namespace"rows.2"Path of the enclosing array row or nested section (undefined at the top level)
scope{ kind: "other", detail: "" }Values at namespace; the whole form values at the top level

Use scope for conditions that depend on the current array row:

items: {
  type: "array",
  props: {
    config: {
      kind: { type: "select", props: { options } },
      detail: {
        type: "text",
        // shown only in rows whose own `kind` is "other"
        visible: (_, { scope }) => scope?.kind === "other",
      },
    },
  },
},

Reads through scope are tracked like reads through values. Typing in one row re-runs only that row's conditions.

JSON conditions

Added in v0.17.

visible, disabled and required also accept a plain object, so a whole config can be stored as JSON (e.g. in a database) and loaded with defineDynamicConfig:

const config = defineConfig<Order>({
  discountCode: { type: "text", visible: { when: "role", eq: "member" } },
  notes: {
    type: "text",
    disabled: { or: [{ when: "status", in: ["paid", "shipped"] }, { when: "locked", eq: true }] },
  },
  insurance: { type: "text", visible: { when: "age", gte: 18, lt: 65 } },
});
OperatorHolds when the value…Field types
(none)is truthyany
eq / neqis / is not === the given valueany
in / notInis / is not one of the valuesany
empty / notEmptyis / is not null, undefined, "" or []any
gt gte lt ltecompares as a number (numeric strings from inputs are converted)number
matchesmatches a regular expression ("^a" or "/^a/i")string

Several operators in one object must all hold ({ when: "age", gte: 18, lt: 65 }). Combine conditions with and: [...], or: [...] and not: {...}.

Type-checked. In a typed config when autocompletes the model's paths and each operator only accepts values of that field's type:

visible: { when: "rol", eq: "admin" }    // ✗ unknown path
visible: { when: "role", eq: "guest" }   // ✗ not "admin" | "member"
visible: { when: "age", eq: "18" }       // ✗ age is a number
visible: { when: "name", gt: 1 }         // ✗ numeric comparison on a string

Relative to the scope. when is a path relative to the field's array row or nested section (the form root at the top level), so per-row conditions need no index. Use from: "root" for a path from the form root. Root paths are checked against the form model, even in inline nested configs:

items: {
  type: "array",
  props: {
    config: {
      kind: { type: "select" },
      detail: { type: "text", visible: { when: "kind", eq: "other" } },               // this row
      adminNote: { type: "text", visible: { when: "role", from: "root", eq: "admin" } }, // form root
    },
  },
},

For a row config written separately, pass the root model as the second type argument, so both kinds of paths are checked: defineConfig<Item, Order>({...}). A config written with defineConfig<Item>() still nests anywhere; it just cannot use from: "root".

Dependencies are tracked the same way as for functions: a condition is re-evaluated only when a path it reads changes.

Preview
Code

visible

When visible resolves to false, BlueForm does not render that field node. The field is not registered through RHF at that moment.

If a field was visible first, then later becomes hidden, it gets unmounted. From that point, RHF behavior depends on shouldUnregister:

  • shouldUnregister: false keeps the last value in form state.
  • shouldUnregister: true removes the value from form state.

If a field is visible: false on the first render, it never mounts, so field-level defaultValue is never registered.

That means this config:

secret: {
  type: "text",
  visible: false,
  defaultValue: "preset",
}

does not put secret into the submitted payload by itself.

If you need an initial value to exist regardless of visibility, define it at the form level instead:

<Form
  defaultValues={{ secret: "preset" }}
  config={{
    secret: {
      type: "text",
      visible: false,
    },
  }}
/>

visible also does not automatically make a field irrelevant to validation. If a hidden field was already registered and still exists in form state, a rule like required can still block submission. For conditional requirements, prefer validate and short-circuit when the field is inactive, and use a required function so the field still shows its required marker (*) when it applies:

email: {
  type: "text",
  visible: (values) => values.contactMethod === "email",
  required: (values) => values.contactMethod === "email", // UI flag only (v0.14+)
  rules: {
    validate: (value, values) =>
      values.contactMethod !== "email" ||
      Boolean(value) ||
      "Email is required",
  },
}

required only drives the required value that useField() returns; it never adds validation. Without it, required is inferred from rules.required, so validate-based rules would show no marker.

disabled

disabled does not unmount the field. The field stays registered and rendered, but RHF treats controller-based fields as disabled.

In practice:

  • the component still receives field props through useField
  • the input should render as disabled in the UI
  • fields wired through useField are excluded from the submit payload by RHF

This payload exclusion is a React Hook Form controller behavior. It applies to regular fields backed by useController. Array fields expose disabled for UI logic through useArrayField, but useFieldArray itself does not provide the same payload exclusion behavior.

Use disabled when the user should still see the field, but cannot interact with it yet.

Use visible when the field should disappear entirely from the current UI.

Performance: deps

Added in v0.14. Since v0.16, deps are inferred and deps is optional.

Each field evaluates its own visible / disabled / required functions. While a function runs, BlueForm records which values it reads from the values argument (values.role, values.profile.city, …). After that, the function runs again only when one of those paths changes, and the field re-renders only when the result changes. Typing anywhere else in the form does not touch the field.

The recorded paths are refreshed on every run, so conditions that short-circuit work as expected: with values.a === "x" && values.b === "y", b becomes a dependency once a is "x".

Some reads are coarser:

  • Reading an array (values.rows.length, values.rows.some(...)) depends on the whole array.
  • Enumerating an object (Object.keys(values.profile), spread, JSON.stringify) depends on that whole object; enumerating values itself re-runs the function on every change.

You can still declare deps. It overrides the inference: the functions then re-run only when one of these paths changes, even if they read other values.

studentId: {
  type: "text",
  visible: (values) => values.role === "student",
  deps: ["role"], // optional since v0.16
},

Paths are absolute ("profile.role"). Only reads through the values argument are tracked. A function that reads state from outside the form, such as a React state variable in a closure, is re-evaluated when the form's parent re-renders, not when that state changes on its own.