import { InjectionToken, type Provider } from '@angular/core'; import { AbstractControl, FormGroupDirective, NgForm, type ValidationErrors } from '@angular/forms'; import { Observable } from 'rxjs'; import * as i0 from "@angular/core"; export declare const dirty$: (control: AbstractControl) => Observable; export declare const touched$: (control: AbstractControl) => Observable; /** * Defines when a {@link AbstractControl control} is in an *state*. */ export type StateMatcher = (control: AbstractControl, parent?: FormGroupDirective | NgForm) => Observable; /** * Emits whenever the value, status, touched/untouched state of the control changes or the parent submits. * * Evaluates to `true` when the control status is `INVALID` and it is `touched` or the parent is `submitted`. */ export declare const NGX_DEFAULT_CONTROL_ERROR_STATE_MATCHER: StateMatcher; export declare const NGX_CONTROL_ERROR_STATE_MATCHER: InjectionToken; export declare const NGX_CONTROL_ERROR_PARENT: InjectionToken; /** * Configures {@link NgxControlError}. */ export declare const provideNgxControlError: (options?: { errorStateMatcher?: () => StateMatcher; parent?: () => NgForm | FormGroupDirective; }) => Provider[]; /** * Represents the context of the template the {@link NgxControlError} sits on. */ export interface NgxControlErrorContext { /** * Reference to the `errors` of {@link NgxControlError.control} */ $implicit: ValidationErrors; /** * Reference to {@link NgxControlError.control} */ control: AbstractControl; /** * Reference to {@link NgxControlError.track} */ track: string | string[]; } /** * Structural directive for displaying form control errors consistently and reduce boilerplate. * * ## Usage * * ```html * * ``` * * The template will be rendered, when the control is in an [_error state_](#configuration) and its errors include the tracked error(s). * * without `NgxControlError`: * * ```html * * ``` * * ## Configuration * * A `StateMatcher` defines when the provided control is in an _error state_. * A `StateMatcher` is a function which returns an observable. Every time the `StateMatcher` emits a value, the directive checks whether it should render or hide its template: * The directive renders its template when the `StateMatcher` emits `true` and the errors of the control include at least 1 tracked error, else its template will be hidden. * * ```ts * export type StateMatcher = ( * control: AbstractControl, * parent?: FormGroupDirective | NgForm, * ) => Observable; * ``` * * Per default the control is considered in an _error state_ when 1. its status is `INVALID` and 2. it is `touched` or its form has been `submitted`. * * You can override this behavior: * * ```ts * // * // A control is in an error state when its status is invalid. * // Emits whenever statusChanges emits. * // You may want to add more sources, such as valueChanges. * // * export const customErrorStateMatcher: StateMatcher = (control) => * control.statusChanges.pipe( * startWith(control.status), * map((status) => status === 'INVALID'), * ); * ``` * * ### Via DI * * ```ts * provideNgxControlError({ errorStateMatcher: customErrorStateMatcher }); * ``` * * ### Via Input * * ```html * * ``` * * ## Integration * * ### [NGX Translate](https://github.com/ngx-translate/core) * * You can iterate over all possible errors and pass the `errors` to the translate pipe: * * ```html * * ``` * * ### [Angular Material](https://github.com/angular/components) * * ```html * * Name * * Name is required. * * ``` */ export declare class NgxControlError { /** @ignore */ private readonly templateRef; /** @ignore */ private readonly viewContainerRef; constructor(); /** * The errors this directive tracks, when a {@link control$ control} is provided. */ set track(track: string | string[] | undefined); get track(): string | string[] | undefined; /** * The control which `errors` are tracked. Either a control instance or the name of the control when used in a form. * * @see {@link AbstractControl.errors} */ set control(control: AbstractControl | undefined); get control(): AbstractControl | undefined; /** * The control which `errors` are tracked. Either a control instance or the name of the control when used in a form. * * @see {@link AbstractControl.errors} */ set controlInput(control: AbstractControl | string); /** * A `StateMatcher` which defines when this {@link control$ control} is in an *error state*. * This directive **ONLY** renders this template when the `StateMatcher` evaluates to `true`. * * Defaults to {@link NGX_CONTROL_ERROR_STATE_MATCHER}. */ set errorStateMatcher(errorStateMatcher: StateMatcher); get errorStateMatcher(): StateMatcher; /** * The top level parent of this {@link control$ control}. * * NOTE: Might not be the control referenced by {@link AbstractControl.parent parent} of this {@link control$ control}. */ set parent(parent: FormGroupDirective | NgForm | undefined); get parent(): FormGroupDirective | NgForm | undefined; /** * The errors this directive tracks, when a {@link control$ control} is provided. */ readonly track$: import("@angular/core").WritableSignal; /** * The top level parent of this {@link control$ control}. * * NOTE: Might not be the control referenced by {@link AbstractControl.parent parent} of this {@link control$ control}. */ readonly parent$: import("@angular/core").WritableSignal; /** * The direct parent form group directive of this control. */ private readonly controlContainer; /** * The control which `errors` are tracked. * * @see {@link AbstractControl.errors} */ readonly control$: import("@angular/core").WritableSignal | undefined>; /** * A `StateMatcher` which defines when this {@link control$ control} is in an *error state*. * This directive **ONLY** renders this template when the `StateMatcher` evaluates to `true`. * * Defaults to {@link NGX_CONTROL_ERROR_STATE_MATCHER}. */ readonly errorStateMatcher$: import("@angular/core").WritableSignal; /** * The context of this template. */ get context(): NgxControlErrorContext; /** * Whether this {@link control$ control's} errors include one of the {@link track$ tracked errors} and whether it is in an *{@link errorState$ error state}*. */ private readonly _hasError$; /** * Whether this {@link control$ control's} errors include one of the {@link track$ tracked errors} and whether it is in an *{@link errorState$ error state}*. */ readonly hasError$: import("@angular/core").Signal; /** @ignore */ static ngTemplateContextGuard: (directive: NgxControlError, context: unknown) => context is NgxControlErrorContext; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵdir: i0.ɵɵDirectiveDeclaration; }