import { Subscription } from 'rxjs'; import { EventEmitter } from '@angular/core'; import { AsyncValidatorFn } from '../interfaces/Validator'; import { FormGroup } from './FormGroup'; import { AbstractControlState, ControlOptions, FormArrayState, FormGroupState } from '../interfaces/Form'; import { FormArray } from './FormArray'; import { Schema, ValidationError } from 'yup'; import _ from 'lodash'; /** * This is the base class for `FormControl`, `FormGroup.ts`, and `FormArray`. * * It provides some of the shared behavior that all controls and groups of controls have, like * running types, calculating status, and resetting state. It also defines the properties * that are shared between all sub-classes, like `value`, `valid`, and `dirty`. It shouldn't be * instantiated directly. * * * @publicApi */ export declare abstract class AbstractControl { state: AbstractControlState | null; options: ControlOptions | null; /** @internal */ readonly focused: boolean; readonly debounce = 500; /** * The current value of the control. * * * For a `FormControl`, the current value. * * For a `FormGroup`, the values of enabled controls as an object with a key-value pair for each member of the group. * * For a `FormArray`, the values of enabled controls as an array. * */ readonly value: any; /** * A Joi object schema */ /** * True if the control is marked as `touched`. * * A control is marked `touched` once the user has triggered * a `blur` event on it. */ readonly touched: boolean; /** * A control is `pristine` if the user has not yet changed the value in the UI. * * True if the user has not yet changed the value in the UI; compare `dirty`. * Programmatic changes to a control's value do not mark it dirty. */ readonly pristine: boolean; /** * The validation status of the control. There are four possible * validation status values: * * * **VALID**: This control has passed all validation checks. * * **INVALID**: This control has failed at least one validation check. * * **PENDING**: This control is in the midst of conducting a validation check. * * **DISABLED**: This control is exempt from validation checks. * * These status values are mutually exclusive, so a control cannot be both valid AND invalid or invalid AND disabled. */ readonly status: string; readonly asyncValidator: AsyncValidatorFn | null; runAsyncValidator: Function; runValidator: Function; _asyncValidationSubscription: any; _validationSubscription: Subscription; /** * An object containing the ValidationError generated by Yup or null if there are no errors. */ validationError: ValidationError; /** Array of error messages */ readonly errors: string[]; private _parent; private _root; private _name; private _path; /** * * @param controls A collection of child controls. The key for each child is the name * under which it is registered. * */ readonly controls: FormGroupState | FormArrayState; /** * @description * Emits an event when the form submission has been triggered. */ /** * A multicasting observable that emits an event every timespan the value of the control changes, in * the UI or programmatically. */ readonly valueChanges: EventEmitter; /** * A multicasting observable that emits an event every timespan the validation `status` of the control * recalculates. */ readonly statusChanges: EventEmitter; /** * Initialize the AbstractControl instance. * * @param state * */ constructor(state: AbstractControlState | null, options: ControlOptions | null); readonly schema: Schema | null; /** @internal */ _getControlSchema: () => any; readonly path: string; name: string; /** * The parent control. */ readonly parent: FormGroup | FormArray; readonly root: FormGroup; /** * A control is `enabled` as long as its `status` is not `DISABLED`. * * @see `status` * * True if the control has any status other than 'DISABLED', * false if the status is 'DISABLED'. * */ readonly enabled: boolean; /** * A control is `disabled` when its `status` is `DISABLED`. * * @see `status` * * Disabled controls are exempt from validation checks and * are not included in the aggregate value of their ancestor * controls. * * True if the control is disabled, false otherwise. */ readonly disabled: boolean; /** * A control is `valid` when its `status` is `VALID`. * * @see `status` * * True if the control has passed all of its validation tests, * false otherwise. */ readonly valid: boolean; /** * A control is `invalid` when its `status` is `INVALID`. * * @see `status` * * True if this control has failed one or more of its validation checks, * false otherwise. */ readonly invalid: boolean; /** * A control is `pending` when its `status` is `PENDING`. * * @see `status` * * True if this control is in the process of conducting a validation check, * false otherwise. */ readonly pending: boolean; /** * A control is `dirty` if the user has changed the value * in the UI. * * True if the user has changed the value of this control in the UI; compare `pristine`. * Programmatic changes to a control's value do not mark it dirty. */ readonly dirty: boolean; markAsFocused(): void; markAsBlurred(): void; /** * Marks the control as `touched`. A control is touched by focus and * blur events that do not change the value; compare `markAsDirty`; * * @param opts options options that determine how the control propagates changes * and emits events events after marking is applied. * * `onlySelf`: When true, mark only this control. When false or not supplied, * marks all direct ancestors. Default is false. */ markAsTouched(): void; /** * Marks the control as `untouched`. * * If the control has any children, also marks all children as `untouched` * and recalculates the `touched` status of all parent controls. * * @param opts options options that determine how the control propagates changes * and emits events after the marking is applied. * * `onlySelf`: When true, mark only this control. When false or not supplied, * marks all direct ancestors. Default is false. */ markAsUntouched(opts?: { onlySelf?: boolean; }): void; /** * Marks the control as `dirty`. A control becomes dirty when * the control's value is changed through the UI; compare `markAsTouched`. * * @param opts options options that determine how the control propagates changes * and emits events after marking is applied. * * `onlySelf`: When true, mark only this control. When false or not supplied, * marks all direct ancestors. Default is false. */ markAsDirty(opts?: { onlySelf?: boolean; }): void; /** * Marks the control as `pristine`. * * If the control has any children, marks all children as `pristine`, * and recalculates the `pristine` status of all parent * controls. * * @param opts options options that determine how the control emits events after * marking is applied. * * `onlySelf`: When true, mark only this control. When false or not supplied, * marks all direct ancestors. Default is false.. */ markAsPristine(opts?: { onlySelf?: boolean; }): void; /** * Sets errors on a form control when running validations manually, rather than automatically. * * Calling `setErrors` also updates the validity of the parent control. * * @usageNotes * ### Manually set the errors for a control * * ``` * const login = new FormControl('someLogin'); * login.setErrors({ * notUnique: true * }); * * expect(login.valid).toEqual(false); * expect(login.errors).toEqual({ notUnique: true }); * * login.setValue('someOtherLogin'); * * expect(login.valid).toEqual(true); * ``` */ setError(error: ValidationError, opts?: { emitEvent?: boolean; }): void; /** * @param parent Sets the parent of the control */ setParent(parent: FormGroup | FormArray): void; /** * @param root Sets the root of the form */ setRoot(root: FormGroup): void; /** * Recalculates the value and validation status of the control. * * By default, it also updates the value and validity of its ancestors. * * @param opts options options determine how the control propagates changes and emits events * after updates and validity checks are applied. * * `onlySelf`: When true, only update this control. When false or not supplied, * update all direct ancestors. Default is false.. * * `emitEvent`: When true or not supplied (the default), emit the `valueChanges` event * observables emit events with the latest status and value when the control is updated. * When false, no events are emitted. */ updateValueAndValidity: ((opts?: { onlySelf?: boolean; emitEvent?: boolean; }) => void) & _.Cancelable; /** @internal */ private _calculateStatus; /** @internal */ _runValidator(): any; /** @internal */ _updateChildError: (errors: ValidationError[]) => void; private _cancelExistingSubscription; private _setInitialStatus; /** @internal */ _updateValue(): void; /** @internal */ _isNotExcluded: (c: AbstractControl) => Boolean; /** @internal */ _initObservables(): void; /** @internal */ _runControlAysnValidator(emitEvent: boolean): void; /** @internal */ /** @internal */ _updateControlsErrors(emitEvent: boolean): void; /** * Sets the value of the control. Abstract method (implemented in sub-classes). */ abstract setValue(value: any, options?: Object): void; /** * Resets the control. Abstract method (implemented in sub-classes). */ abstract reset(value?: any, options?: Object): void; /** * Clear the control. Abstract method (implemented in sub-classes). */ abstract clear(options?: Object): void; /** @internal */ abstract _allControlsDisabled(): boolean; /** @internal */ abstract _anyControlsHaveStatus(status: string): boolean; } /** * Reports that a FormControl is valid, meaning that no errors exist in the input value. * * @see `status` */ export declare const VALID = "VALID"; /** * Reports that a FormControl is invalid, meaning that an error exists in the input value. * * @see `status` */ export declare const INVALID = "INVALID"; /** * Reports that a FormControl is pending, meaning that that async validation is occurring and * errors are not yet available for the input value. * * @see `markAsPending` * @see `status` */ export declare const PENDING = "PENDING"; /** * Reports that a FormControl is disabled, meaning that the control is exempt from ancestor * calculations of validity or value. * * @see `markAsDisabled` * @see `status` */ export declare const DISABLED = "DISABLED"; export declare type FormHooks = 'change' | 'blur' | 'submit';