dynamic-forms / Class

RootFormRegistryService

Decorators:@Injectable

Registry service that maintains references to root form fields. This provides a type-safe way to access root form values from child fields without relying on internal Angular Forms API navigation.

This service should be provided at the component level to ensure proper isolation between different form instances.

IMPORTANT: This registry supports two registration patterns:

  1. registerFormValueSignal() - Register the SOURCE signal BEFORE form creation This allows logic functions to read form values during schema evaluation.
  2. registerRootForm() - Register the FieldTree AFTER form creation This provides access to the full form structure.

For cross-field logic to work correctly, registerFormValueSignal() should be called BEFORE the form() call.

Methods

clear()

Clears all registered forms. Useful for testing.

Presentation
clear(): void;
Returns
void

getFormContext()

Gets form context by id.

Presentation
getFormContext(formId: string = 'default'): Record<string, unknown>;
Parameters
NameTypeDescription
formId
string
Returns
Record<string, unknown>

getFormValue()

Gets the form value, preferring the direct signal if available.

This method tries in order:

  1. Form value signal (if registered) - reads directly from source signal
  2. Root form's .value() (if registered) - reads from FieldTree
  3. Returns empty object if nothing is registered

Reading from signals creates reactive dependencies, so computeds will re-evaluate when form values change.

Presentation
getFormValue(formId: string = 'default'): Record<string, unknown>;
Parameters
NameTypeDescription
formId
string
Returns
Record<string, unknown>

getFormValueSignal()

Gets the registered form value signal by id.

This allows direct access to the source signal, which can be read reactively to get form values.

Presentation
getFormValueSignal(formId: string = 'default'): Signal<Record<string, unknown>> | undefined;
Parameters
NameTypeDescription
formId
string
Returns
Signal<Record<string, unknown>> | undefined

getRegisteredFormIds()

Gets all registered form IDs.

Presentation
getRegisteredFormIds(): string[];
Returns
string[]

getRootForm()

Gets a registered root form field by id.

IMPORTANT: This method reads from a signal, which means any computed signal that calls this method will automatically re-evaluate when the form is registered or updated.

Presentation
getRootForm(formId: string = 'default'): FieldTree<unknown> | undefined;
Parameters
NameTypeDescription
formId
string
Returns
FieldTree<unknown> | undefined

registerFormContext()

Registers form context (field paths and metadata) for a specific form. This can be used to store additional context information.

Presentation
registerFormContext(context: Record<string, unknown>, formId: string = 'default'): void;
Parameters
NameTypeDescription
context
Record<string, unknown>
formId
string
Returns
void

registerFormValueSignal()

Registers the form value signal BEFORE form creation.

This is the recommended pattern for cross-field logic:

  1. Create the formValue signal
  2. Register it with registerFormValueSignal()
  3. Create the form with form(formValue, schema(...))

This allows logic functions to access form values during schema evaluation.

Presentation
registerFormValueSignal(valueSignal: Signal<Record<string, unknown>>, formId: string = 'default'): void;
Parameters
NameTypeDescription
valueSignal
Signal<Record<string, unknown>>
formId
string
Returns
void

registerRootForm()

Registers a root form field with an optional identifier. If no id is provided, uses 'default' as the key.

This triggers reactive updates for any computed signals that depend on getRootForm().

Presentation
registerRootForm(rootField: FieldTree<unknown>, formId: string = 'default'): void;
Parameters
NameTypeDescription
rootField
FieldTree<unknown>
formId
string
Returns
void

unregisterForm()

Unregisters a form and its context. This should be called when a form component is destroyed to prevent memory leaks.

Presentation
unregisterForm(formId: string = 'default'): void;
Parameters
NameTypeDescription
formId
string
Returns
void