W3cubDocs

/Angular

FormRecord

class final

Tracks the value and validity state of a collection of FormControl instances, each of which has the same value type.

See more...

class FormRecord<TControl extends AbstractControl = AbstractControl, TControl> extends FormGroup<{
    [key: string]: TControl;
}> {
  registerControl(name: string, control: TControl): TControl
  addControl(name: string, control: TControl, options?: { emitEvent?: boolean; }): void
  removeControl(name: string, options?: { emitEvent?: boolean; }): void
  setControl(name: string, control: TControl, options?: { emitEvent?: boolean; }): void
  contains(controlName: string): boolean
  setValue(value: { [key: string]: ɵValue<TControl>; }, options?: { onlySelf?: boolean; emitEvent?: boolean; }): void
  patchValue(value: { [key: string]: ɵValue<TControl>; }, options?: { onlySelf?: boolean; emitEvent?: boolean; }): void
  reset(value?: { [key: string]: ɵValue<TControl>; }, options?: { onlySelf?: boolean; emitEvent?: boolean; }): void
  getRawValue(): {...}

  // inherited from forms/FormGroup
  constructor(controls: TControl, validatorOrOpts?: ValidatorFn | AbstractControlOptions | ValidatorFn[], asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[])
  controls: ɵTypedOrUntyped<TControl, TControl, {...}
  registerControl<K extends string & keyof TControl>(name: K, control: TControl[K]): TControl[K]
  addControl<K extends string & keyof TControl>(name: K, control: Required<TControl>[K], options: { emitEvent?: boolean; } = {}): void
  removeControl(name: string, options: { emitEvent?: boolean; } = {}): void
  setControl<K extends string & keyof TControl>(name: K, control: TControl[K], options: { emitEvent?: boolean; } = {}): void
  contains<K extends string & keyof TControl>(controlName: K): boolean
  setValue(value: ɵIsAny<TControl, { [key: string]: any; }, { [K in keyof TControl]: ɵRawValue<TControl[K]>; }>, options: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
  patchValue(value: ɵIsAny<TControl, { [key: string]: any; }, Partial<{ [K in keyof TControl]: ɵValue<TControl[K]>; }>>, options: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
  reset(value: ɵIsAny<TControl, any, ɵIsAny<TControl, { [key: string]: any; }, Partial<{ [K in keyof TControl]: ɵValue<TControl[K]>; }>>> = {} as unknown as ɵFormGroupValue<TControl>, options: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
  getRawValue(): ɵTypedOrUntyped<TControl, ɵFormGroupRawValue<TControl>, any>

  // inherited from forms/AbstractControl
  constructor(validators: ValidatorFn | ValidatorFn[], asyncValidators: AsyncValidatorFn | AsyncValidatorFn[])
  value: TValue
  validator: ValidatorFn | null
  asyncValidator: AsyncValidatorFn | null
  parent: FormGroup | FormArray | null
  status: FormControlStatus
  valid: boolean
  invalid: boolean
  pending: boolean
  disabled: boolean
  enabled: boolean
  errors: ValidationErrors | null
  pristine: boolean
  dirty: boolean
  touched: boolean
  untouched: boolean
  valueChanges: Observable<TValue>
  statusChanges: Observable<FormControlStatus>
  updateOn: FormHooks
  root: AbstractControl
  setValidators(validators: ValidatorFn | ValidatorFn[]): void
  setAsyncValidators(validators: AsyncValidatorFn | AsyncValidatorFn[]): void
  addValidators(validators: ValidatorFn | ValidatorFn[]): void
  addAsyncValidators(validators: AsyncValidatorFn | AsyncValidatorFn[]): void
  removeValidators(validators: ValidatorFn | ValidatorFn[]): void
  removeAsyncValidators(validators: AsyncValidatorFn | AsyncValidatorFn[]): void
  hasValidator(validator: ValidatorFn): boolean
  hasAsyncValidator(validator: AsyncValidatorFn): boolean
  clearValidators(): void
  clearAsyncValidators(): void
  markAsTouched(opts: { onlySelf?: boolean; } = {}): void
  markAllAsTouched(): void
  markAsUntouched(opts: { onlySelf?: boolean; } = {}): void
  markAsDirty(opts: { onlySelf?: boolean; } = {}): void
  markAsPristine(opts: { onlySelf?: boolean; } = {}): void
  markAsPending(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
  disable(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
  enable(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
  setParent(parent: FormGroup<any> | FormArray<any>): void
  abstract setValue(value: TRawValue, options?: Object): void
  abstract patchValue(value: TValue, options?: Object): void
  abstract reset(value?: TValue, options?: Object): void
  getRawValue(): any
  updateValueAndValidity(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
  setErrors(errors: ValidationErrors, opts: { emitEvent?: boolean; } = {}): void
  get<P extends string | ((string | number)[])>(path: P): AbstractControl<ɵGetProperty<TRawValue, P>> | null
  getError(errorCode: string, path?: string | (string | number)[]): any
  hasError(errorCode: string, path?: string | (string | number)[]): boolean
}

Description

FormRecord is very similar to FormGroup, except it can be used with a dynamic keys, with controls added and removed as needed.

FormRecord accepts one generic argument, which describes the type of the controls it contains.

Further information is available in the Usage Notes...

Methods

Registers a control with the records's list of controls.

registerControl(name: string, control: TControl): TControl

Parameters
name string
control TControl
Returns

TControl

See FormGroup#registerControl for additional information.

Add a control to this group.

addControl(name: string, control: TControl, options?: { emitEvent?: boolean; }): void

Parameters
name string
control TControl
options object

Optional. Default is undefined.

Returns

void

See FormGroup#addControl for additional information.

Remove a control from this group.

removeControl(name: string, options?: { emitEvent?: boolean; }): void

Parameters
name string
options object

Optional. Default is undefined.

Returns

void

See FormGroup#removeControl for additional information.

Replace an existing control.

setControl(name: string, control: TControl, options?: { emitEvent?: boolean; }): void

Parameters
name string
control TControl
options object

Optional. Default is undefined.

Returns

void

See FormGroup#setControl for additional information.

Check whether there is an enabled control with the given name in the group.

contains(controlName: string): boolean

Parameters
controlName string
Returns

boolean

See FormGroup#contains for additional information.

Sets the value of the FormRecord. It accepts an object that matches the structure of the group, with control names as keys.

setValue(value: { [key: string]: ɵValue<TControl>; }, options?: { onlySelf?: boolean; emitEvent?: boolean; }): void

Parameters
value object
options object

Optional. Default is undefined.

Returns

void

See FormGroup#setValue for additional information.

Patches the value of the FormRecord. It accepts an object with control names as keys, and does its best to match the values to the correct controls in the group.

patchValue(value: { [key: string]: ɵValue<TControl>; }, options?: { onlySelf?: boolean; emitEvent?: boolean; }): void

Parameters
value object
options object

Optional. Default is undefined.

Returns

void

See FormGroup#patchValue for additional information.

Resets the FormRecord, marks all descendants pristine and untouched and sets the value of all descendants to null.

reset(value?: { [key: string]: ɵValue<TControl>; }, options?: { onlySelf?: boolean; emitEvent?: boolean; }): void

Parameters
value object

Optional. Default is undefined.

options object

Optional. Default is undefined.

Returns

void

See FormGroup#reset for additional information.

The aggregate value of the FormRecord, including any disabled controls.

getRawValue(): { [key: string]: ɵRawValue<TControl>; }

Parameters

There are no parameters.

Returns

`{

}`

See FormGroup#getRawValue for additional information.

Usage notes

let numbers = new FormRecord({bill: new FormControl('415-123-456')});
numbers.addControl('bob', new FormControl('415-234-567'));
numbers.removeControl('bill');

© 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/api/forms/FormRecord