Interaction and availability
Touched and dirty describe user interaction. Disabled, readonly, and hidden determine whether a node currently participates as an interactive part of the form.
👆 Touched state
Nodes start untouched. Value changes do not imply touch.
markAsTouched()marks an interactive field touched.- Any touched interactive descendant makes its ancestor forms and arrays touched.
- Calling
markAsTouched()on an aggregate marks that node and every interactive descendant. { skipDescendants: true }marks only the aggregate itself.markAsUntouched()clears only the node's own stored touch; touched descendants may keep aggregate state touched.reset()is the recursive operation that clears touch throughout a subtree.
profile.markAsTouched({ skipDescendants: true });
profile.name.touched(); // false
profile.touched(); // true
👆 Dirty state
Nodes start pristine. Programmatic set(), update(), and patch() preserve dirty state; a control-originated update marks its directly bound node dirty immediately.
- Any dirty interactive descendant makes ancestors dirty.
markAsDirty()andmarkAsPristine()affect only the node's own stored state.- A dirty descendant can keep a form dirty after
form.markAsPristine(). reset()recursively clears dirty state.- Validation changes never mark a node dirty.
Touched, dirty, and validity are independent dimensions.
⚙️ Configured state sources
Readonly and hidden accept a boolean or reactive function. Disabled additionally accepts a reason string:
const profile = form({
name: field('', {
disabled: () => account().locked ? 'The account is locked' : false,
readonly: () => !permissions().canEdit,
hidden: () => !featureFlags().profile,
}),
});
A normal function may read signals directly; no extra computed() is needed. Callback results
use JavaScript truthiness: 0, '', null, and undefined clear the configured state, while
nonzero numbers, nonempty strings, and objects activate it. hidden() and readonly() still
return booleans. A nonempty string from disabled also becomes a reason message; other truthy
results produce an unnamed reason. Static disabled strings, including '', still supply a reason.
These functions also receive { index }, where index is the zero-based position of the item
containing this node in its nearest array ancestor. It is null outside arrays, including after
detachment. The read is reactive: moving a row reevaluates availability even when no value changes.
Nested groups and forms keep the containing row index, while nested arrays use their innermost
containing array. The value is a number, not a signal. See the
indexed callback example for typed sibling access.
Effective state is the union of three independent causes:
- Mutable local state controlled by actions.
- A configured reactive condition.
- Inherited parent state.
enable(), markAsWritable(), and show() clear mutable local state but cannot override an active configured or inherited condition.
Referencing the containing form
State callbacks can reference the form being declared without an explicit callback return type,
including this.form in a component. This works for field(), form(), group(), and array().
The field values and public state signals retain their inferred types.
import { field, form, required } from '@ngblocks/form-nodes';
const profile = form({
purpose: field('', [required]),
itemIds: field<number[]>(null, {
validators: [required],
disabled: () => profile.purpose(),
}),
internalNotes: field('', {
hidden: () => profile.purpose() === 'public',
readonly: () => profile.purpose() === 'review',
}),
});
profile.itemIds.disabled(); // false
if (profile.itemIds.disabled() || !profile.itemIds.hasError('required')) {
throw new Error('Item IDs should start enabled and required.');
}
profile.purpose.set('public');
profile.itemIds.disabled(); // true
profile.internalNotes.hidden(); // true
if (!profile.itemIds.disabled() || !profile.internalNotes.hidden() || !profile.valid()) {
throw new Error('State callbacks should follow the purpose field and suppress disabled validation.');
}
profile.purpose.set('review');
profile.internalNotes.hidden(); // false
profile.internalNotes.readonly(); // true
if (profile.internalNotes.hidden() || !profile.internalNotes.readonly()) {
throw new Error('Changing purpose should update both hidden and readonly state.');
}
profile.purpose.set('');
profile.itemIds.disabled(); // false
if (profile.itemIds.disabled() || profile.internalNotes.readonly() || !profile.itemIds.hasError('required')) {
throw new Error('Clearing purpose should restore interactive state and required validation.');
}
Here, a nonempty purpose disables itemIds. Use !profile.purpose() instead if the field should
be disabled until a purpose is selected. Programmatic writes still work while disabled.
As with direct validator callbacks, the callback return type is intentionally any to avoid
TypeScript's circular initializer inference. Static options remain type checked. Add an explicit
result annotation when you want to restrict a callback's result, for example
disabled: (): boolean => !!profile.purpose() or
disabled: (): boolean | string => profile.purpose() ? 'Locked' : false.
This supports references resolved when the state is read, including through a component computed
declared after the form. Initial asynchronous validation also waits to check these callbacks until
the initial microtask or a validation-state read after construction. It does not make an eager read
inside configure safe before the form variable is assigned, or allow circular dependencies between state computations.
⚡ State propagation
Disabled, readonly, and hidden propagate downward, never upward:
- A parent state affects every descendant.
- A child's own cause is preserved while an ancestor imposes the same state.
- Clearing the parent reveals any child cause that remains active.
- Changing a nested aggregate affects only its subtree, siblings excluded.
- A parent does not become disabled, readonly, or hidden merely because every child is.
🎛️ Disabled reasons
disabledReasons() identifies every cause in outermost-to-innermost order:
profile.disable('Editing is temporarily unavailable');
profile.name.disabledReasons();
// [{ sourceNode: profile, message: 'Editing is temporarily unavailable' }, ...]
Each reason retains the node where it originated. Multiple local causes remain distinct. Calling disable() again replaces the prior imperative reason; enable() removes only that imperative reason.
🎛️ Non-interactive behavior
A node is non-interactive while hidden, disabled, or readonly. During that time:
- Its validators are skipped, own errors are hidden, and it is considered valid.
- Async work is cancelled or made stale and public
pending()becomes false. - Its invalid, touched, dirty, and pending state does not affect ancestors.
- Public touched and dirty signals report false.
markAsTouched()is ignored.- Values remain readable and programmatically writable.
- Validators and stored interaction flags are retained.
When the node becomes interactive again, validation runs against its current value and previously stored touched or dirty state becomes observable again.
Async validators restart even if that value has not changed, including when the state was inherited
from a parent. Explicit params are read again from current signals, and results from cancelled
executions remain ignored.
Hidden state does not manipulate the DOM. Use @if to remove hidden UI controls.
See Advanced behavior and edge cases for retained state, async cancellation, and what becomes observable after interaction is restored.