Skip to content

Commit

Permalink
feat: provide a mergeErrors helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioArnt committed Aug 26, 2020
1 parent ccd56bb commit e6c9a82
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
12 changes: 12 additions & 0 deletions projects/ngneat/reactive-forms/src/lib/formArray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ describe('FormArray', () => {
expect(control.hasErrorAndDirty('isInvalid')).toBeTruthy();
});

it('should setErrors', () => {
const control = createArray(true);
control.setErrors({ customError: true });
expect(control.errors).toEqual({ customError: true });
});

it('should mergeErrors', () => {
const control = createArray(true);
control.mergeErrors({ customError: true });
expect(control.errors).toEqual({ isInvalid: true, customError: true });
});

it('should setEnable', () => {
const control = createArray();

Expand Down
10 changes: 10 additions & 0 deletions projects/ngneat/reactive-forms/src/lib/formArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ export class FormArray<T = any, E extends object = any> extends NgFormArray {
return super.setErrors(errors, opts);
}

mergeErrors(errors: Partial<E>, opts: EmitEvent = {}): void {
this.setErrors(
{
...this.errors,
...errors
},
opts
);
}

getError<K extends ExtractStrings<E>>(errorCode: K, path?: ControlPath) {
return super.getError(errorCode, path) as E[K] | null;
}
Expand Down
12 changes: 12 additions & 0 deletions projects/ngneat/reactive-forms/src/lib/formControl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ describe('FormControl', () => {
expect(control.enabled).toBe(false);
});

it('should setErrors', () => {
const control = new FormControl<string>('', Validators.required);
control.setErrors({ customError: true });
expect(control.errors).toEqual({ customError: true });
});

it('should mergeErrors', () => {
const control = new FormControl<string>('', Validators.required);
control.mergeErrors({ customError: true });
expect(control.errors).toEqual({ required: true, customError: true });
});

it('should setDisable', () => {
const control = new FormControl<string>();
control.setDisable();
Expand Down
10 changes: 10 additions & 0 deletions projects/ngneat/reactive-forms/src/lib/formControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ export class FormControl<T = any, E extends object = any> extends NgFormControl
return super.setErrors(errors, opts);
}

mergeErrors(errors: Partial<E>, opts: EmitEvent = {}): void {
this.setErrors(
{
...this.errors,
...errors
},
opts
);
}

hasErrorAndTouched(error: ExtractStrings<E>): boolean {
return hasErrorAndTouched(this, error);
}
Expand Down
12 changes: 12 additions & 0 deletions projects/ngneat/reactive-forms/src/lib/formGroup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ describe('FormGroup', () => {
expect(control.reset).toHaveBeenCalled();
});

it('should setErrors', () => {
const control = createGroup(true);
control.setErrors({ customError: true });
expect(control.errors).toEqual({ customError: true });
});

it('should mergeErrors', () => {
const control = createGroup(true);
control.mergeErrors({ customError: true });
expect(control.errors).toEqual({ isInvalid: true, customError: true });
});

it('should setValidators', () => {
const control = createGroup();
spyOn(control, 'setValidators');
Expand Down
10 changes: 10 additions & 0 deletions projects/ngneat/reactive-forms/src/lib/formGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ export class FormGroup<T extends Obj = any, E extends object = any> extends NgFo
return super.setErrors(errors, opts);
}

mergeErrors(errors: Partial<E>, opts: EmitEvent = {}): void {
this.setErrors(
{
...this.errors,
...errors
},
opts
);
}

getError<K extends keyof E, K1 extends keyof T>(errorCode: K, path?: [K1]): E[K] | null;
getError<K extends keyof E, K1 extends keyof T, K2 extends keyof T[K1]>(errorCode: K, path?: [K1, K2]): E[K] | null;
getError<K extends keyof E, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(
Expand Down

0 comments on commit e6c9a82

Please sign in to comment.