Skip to content

[WIP] fix: form array validation (for rewrite) #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: feat-rewrite
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions projects/ngx-sub-form/src/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import {
AbstractControlOptions,
ControlValueAccessor,
FormArray,
FormControl,
FormGroup,
ValidationErrors,
} from '@angular/forms';
import { AbstractControlOptions, ControlValueAccessor, FormArray, FormGroup, ValidationErrors } from '@angular/forms';
import { ReplaySubject } from 'rxjs';
import { Nilable } from 'tsdef';
import {
ControlValueAccessorComponentInstance,
FormBindings,
NgxSubFormArrayOptions,
NgxSubFormOptions,
} from './ngx-sub-form.types';
import {
AAAAAA,
ArrayPropertyKey,
ControlsNames,
NewFormErrors,
OneOfControlsTypes,
TypedFormGroup,
} from './shared/ngx-sub-form-utils';
import {
ControlValueAccessorComponentInstance,
FormBindings,
NgxSubFormArrayOptions,
NgxSubFormOptions,
} from './ngx-sub-form.types';

export const deepCopy = <T>(value: T): T => JSON.parse(JSON.stringify(value));

Expand Down Expand Up @@ -88,7 +82,7 @@ export const getFormGroupErrors = <ControlInterface, FormInterface>(
// with the index and the error
// this way, we avoid holding a lot of potential `null`
// values in the array for the valid form controls
const errorsInArray: Record<number, ValidationErrors> = {};
const errorsInArray: AAAAAA = {};

for (let i = 0; i < control.length; i++) {
const controlErrors = control.at(i).errors;
Expand All @@ -97,8 +91,18 @@ export const getFormGroupErrors = <ControlInterface, FormInterface>(
}
}

// following fixes
// https://github.com/cloudnc/ngx-sub-form/issues/161
// if we've got a form array we can't only take into account
// the errors in the form array coming from its items
// we also need to take into account the errors attached to
// the form array itself if it has any validator
if (control.errors) {
errorsInArray['formArray'] = control.errors;
}

if (Object.values(errorsInArray).length > 0) {
const accHoldingArrays = acc as Record<keyof ControlInterface, Record<number, ValidationErrors>>;
const accHoldingArrays = acc as Record<keyof ControlInterface, AAAAAA>;
accHoldingArrays[key as keyof ControlInterface] = errorsInArray;
}
} else if (control.errors) {
Expand Down
6 changes: 4 additions & 2 deletions projects/ngx-sub-form/src/lib/shared/ngx-sub-form-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ValidationErrors,
} from '@angular/forms';
import { getObservableLifecycle } from 'ngx-observable-lifecycle';
import { Observable, Subject, timer } from 'rxjs';
import { Observable, timer } from 'rxjs';
import { debounce, takeUntil } from 'rxjs/operators';
import { NgxSubFormComponent } from '../deprecated/ngx-sub-form.component';

Expand Down Expand Up @@ -47,9 +47,11 @@ export type FormErrors<FormInterface> = null | Partial<
}
>;

export type AAAAAA = { [key in number | 'formArray']?: ValidationErrors };

// @todo rename to `FormErrorsType` once the deprecated one is removed
export type NewFormErrorsType<T> = {
[K in keyof T]-?: T[K] extends any[] ? Record<number, ValidationErrors> : ValidationErrors;
[K in keyof T]-?: T[K] extends any[] ? AAAAAA : ValidationErrors;
};

// @todo rename to `FormErrors` once the deprecated one is removed
Expand Down
22 changes: 18 additions & 4 deletions src/app/app.spec.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { Spaceship, Speeder, VehicleType } from './interfaces/vehicle.interface'
import { hardCodedListings } from './services/listings.data';

context(`EJawa demo`, () => {
const testContexts = [
{ id: 'old', testName: 'Old implementation', url: '' },
const testContexts: { id: any; testName: any; url: any }[] = [
// { id: 'old', testName: 'Old implementation', url: '' },
{ id: 'new', testName: 'New implementation', url: '/rewrite' },
] as const;
];

testContexts.forEach(({ id, testName, url }) => {
context(testName, () => {
Expand Down Expand Up @@ -124,7 +124,7 @@ context(`EJawa demo`, () => {
});
});

it(`should display the (nested) errors from the form`, () => {
it.only(`should display the (nested) errors from the form`, () => {
DOM.createNewButton.click();

DOM.form.errors.should($el => {
Expand Down Expand Up @@ -176,6 +176,14 @@ context(`EJawa demo`, () => {
},
crewMembers: {
required: true,
crewMembers: {
formArray: {
minLengthArray: {
currentLength: 0,
minimumLength: 3,
},
},
},
},
wingCount: {
required: true,
Expand Down Expand Up @@ -250,6 +258,12 @@ context(`EJawa demo`, () => {
required: true,
},
},
formArray: {
minLengthArray: {
currentLength: 1,
minimumLength: 3,
},
},
},
},
wingCount: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@
Add a crew member
</button>
</fieldset>

<!-- todo: remove as this is just for debugging -->
<pre>{{ form.formGroupErrors | json }}</pre>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormArray, FormControl, Validators } from '@angular/forms';
import { AbstractControl, FormArray, FormControl, Validators } from '@angular/forms';
import { createForm, FormType, subformComponentProviders } from 'ngx-sub-form';
import { CrewMember } from '../../../../../interfaces/crew-member.interface';

Expand All @@ -18,7 +18,12 @@ export class CrewMembersComponent {
public form = createForm<CrewMember[], CrewMembersForm>(this, {
formType: FormType.SUB,
formControls: {
crewMembers: new FormArray([]),
crewMembers: new FormArray(
[],
// the following validator is here to make sure we have a proper fix to
// https://github.com/cloudnc/ngx-sub-form/issues/161
this.minLengthArray(3),
),
},
toFormGroup: (obj: CrewMember[]): CrewMembersForm => {
return {
Expand Down Expand Up @@ -50,4 +55,19 @@ export class CrewMembersComponent {
}),
);
}

private minLengthArray(minimumLength: number) {
return (c: AbstractControl): { [key: string]: any } | null => {
if (c.value.length >= minimumLength) {
return null;
}

return {
minLengthArray: {
currentLength: c.value.length,
minimumLength,
},
};
};
}
}