Skip to content
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

Fix recursive form validation #4

Merged
merged 2 commits into from
Mar 22, 2020
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2.0.0-beta6

Fix
- Recursive form validation fixed
- Fixed change detection of dynamic created components

## 2.0.0-beta5

Fix
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"type": "git"
},
"license": "MIT",
"version": "2.0.0-beta5",
"version": "2.0.0-beta6",
"peerDependencies": {
"@angular/common": "^7.2.0 || ^8.0.0",
"@angular/core": "^7.2.0 || ^8.0.0",
Expand Down
2 changes: 2 additions & 0 deletions src/lib/abstract-form-group/abstract-form-group.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ export abstract class AbstractFormGroupComponent<FormType extends AbstractGroupT
if (host instanceof HTMLElement && !host.contains(component.location.nativeElement)) {
host.appendChild(component.location.nativeElement);
}

component.changeDetectorRef.detectChanges();
});
});

Expand Down
18 changes: 13 additions & 5 deletions src/lib/form-builder/form-builder.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,7 @@ export class FormBuilderComponent<T extends { [key: string]: any } = {}> impleme
}

public submit(): T {
Object.keys(this.group.controls).forEach((field) => {
const control = this.group.get(field);
control.markAsDirty();
control.updateValueAndValidity();
});
this.validateAllFormFields(this.group);

if (!this.group.valid) {
return;
Expand All @@ -162,6 +158,18 @@ export class FormBuilderComponent<T extends { [key: string]: any } = {}> impleme
return this.group.value;
}

private validateAllFormFields(formGroup: FormGroup): void {
Object.keys(formGroup.controls).forEach(field => {
const control = formGroup.get(field);
if (control instanceof FormControl) {
control.markAsTouched({onlySelf: true});
control.updateValueAndValidity({onlySelf: true});
} else if (control instanceof FormGroup) {
this.validateAllFormFields(control);
}
});
}

public getErrors(group?: FormGroup | FormArray): { [key: string]: ValidationErrors } | ValidationErrors[] | null {
const errors = group instanceof FormArray ? [] : {};

Expand Down
1 change: 1 addition & 0 deletions src/lib/form-field/form-field.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class FormFieldDirective extends AbstractFormFieldComponent<any> implemen
if (host instanceof HTMLElement && !host.contains(component.location.nativeElement)) {
host.appendChild(component.location.nativeElement);
}

component.changeDetectorRef.detectChanges();
}
}
4 changes: 2 additions & 2 deletions src/lib/form-slot/form-slot.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class FormSlotDirective implements AfterViewInit {
@Input()
public mwFieldName: string;

private renderFn: (viewRef: ViewContainerRef, elRef: ElementRef) => void;
private renderFn: (viewRef: ViewContainerRef, elRef: ElementRef) => void;

constructor(public viewRef: ViewContainerRef,
public elRef: ElementRef,
Expand All @@ -22,7 +22,7 @@ export class FormSlotDirective implements AfterViewInit {
}
}

public setup(renderFunction: (viewRef: ViewContainerRef, elRef: ElementRef) => void): void {
public setup(renderFunction: (viewRef: ViewContainerRef, elRef: ElementRef) => void): void {
this.renderFn = renderFunction;
if (this.viewRef) {
this.ngAfterViewInit();
Expand Down