CustomFnConfig
@ng-forge/dynamic-forms
Signal forms adapter configuration for advanced form behavior.
Provides configuration options for signal forms integration including legacy migration, custom functions, and custom validators.
Signature
interface CustomFnConfig<
TFormValue extends Record<string, unknown> = Record<string, unknown>
>Type Parameters
| Name | Constraint | Default | Description |
|---|---|---|---|
TFormValue | Record<string, unknown> | Record<string, unknown> | - |
Properties
| Name | Type | Description |
|---|---|---|
customFunctions ? | Record<string, CustomFunction<TFormValue>> | Custom evaluation functions for conditional expressions. Used for: when/readonly/disabled logic Return type: any value (typically boolean) |
derivations ? | Record<string, CustomFunction<TFormValue>> | Custom derivation functions for value derivation logic. These functions compute derived values and are called when a
Derivation functions:
- Receive an Use derivation functions for complex mappings or logic that can't be easily expressed as a JavaScript expression. |
asyncDerivations ? | Record<string, AsyncDerivationFunction<TFormValue>> | Async derivation functions for asynchronous value derivation logic. These functions perform asynchronous operations (service calls, complex pipelines)
and return the derived value via a Promise or Observable. They are called when a
Async derivation functions:
- Receive an |
asyncConditions ? | Record<string, AsyncConditionFunction<TFormValue>> | Async condition functions for asynchronous field state logic. These functions perform asynchronous operations and return a boolean
indicating whether the condition is met. They are referenced by
Async condition functions:
- Receive an |
validators ? | Record<string, CustomValidator> | Custom validators using Angular's public FieldContext API (ctx, params?) => ValidationError | ValidationError[] | null Validators receive FieldContext which provides access to:
- Current field value: Return Types:
- Single error: |
asyncValidators ? | Record<string, AsyncCustomValidator<unknown, unknown, unknown>> | Async custom validators using Angular's resource-based Angular's validateAsync uses the resource API for async validation. Validators must provide params, factory, onSuccess, and optional onError callbacks. Structure:
- Use Cases: - Database lookups via services with resource API - Complex async business logic with Angular resources Note: For HTTP validation, prefer |
httpValidators ? | Record<string, HttpCustomValidator<unknown, unknown>> | HTTP validators using Angular's For function-based HTTP validation with automatic request cancellation.
Prefer declarative HTTP validators ( |
Examples
customFnConfig: {
customFunctions: {
isAdult: (context) => context.age >= 18,
formatCurrency: (context) => new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
}).format(context.value)
},
derivations: {
getCurrencyForCountry: (context) => {
const countryToCurrency: Record<string, string> = {
'USA': 'USD', 'Germany': 'EUR', 'UK': 'GBP'
};
return countryToCurrency[context.formValue.country as string] ?? 'USD';
}
},
simpleValidators: {
noSpaces: (value) => {
return typeof value === 'string' && value.includes(' ')
? { kind: 'noSpaces', message: 'Spaces not allowed' }
: null;
}
},
contextValidators: {
lessThanField: (ctx, params) => {
const value = ctx.value();
const otherField = params?.field as string;
const otherValue = ctx.root()[otherField]?.value();
if (otherValue !== undefined && value >= otherValue) {
return { kind: 'notLessThan', message: `Must be less than ${otherField}` };
}
return null;
}
}
}packages/dynamic-forms/src/lib/models/form-config.ts:323