Skip to content

Commit 4b38271

Browse files
committed
feat: Prepare for Model driven forms
* Bootstrap4Module's directives now supports both Template and ModelDriven * Attempt to reuse Email and Checkbox validator as ValidatorFn
1 parent 8be8b6c commit 4b38271

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
import { Directive, HostBinding } from '@angular/core';
2-
import { FormControl, NgModel } from '@angular/forms';
1+
import { AfterViewInit, Directive, HostBinding, ContentChild } from '@angular/core';
2+
import { NgModel, AbstractControl, FormControlName } from '@angular/forms';
33

44
// tslint:disable-next-line
5-
@Directive({selector: '[ngModel]'})
6-
export class FormControlStatusDirective {
5+
@Directive({selector: '[ngModel],[formControlName]'})
6+
export class FormControlStatusDirective implements AfterViewInit {
77

8-
private control: FormControl;
8+
@ContentChild(NgModel) private input: NgModel;
9+
@ContentChild(FormControlName) private formControl: FormControlName;
10+
private control: AbstractControl;
911

10-
constructor(ngModel: NgModel) {
11-
this.control = ngModel.control;
12+
constructor() {}
13+
14+
ngAfterViewInit() {
15+
if (this.input) {
16+
this.control = this.input.control;
17+
} else if (this.formControl) {
18+
this.control = this.formControl.control;
19+
}
1220
}
1321

1422
@HostBinding('class.form-control-success') get valid() {
15-
return this.control.dirty && this.control.valid;
23+
return !this.control ? false : this.control.dirty && this.control.valid;
1624
}
1725

1826
@HostBinding('class.form-control-danger') get invalid() {
19-
return this.control.touched && this.control.invalid;
27+
return !this.control ? false : this.control.touched && this.control.invalid;
2028
}
2129
}

src/app/shared/bootstrap4/forms/form-group-has-status.directive.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { Directive, HostBinding, ContentChild, AfterViewInit } from '@angular/core';
2-
import { FormControl, NgModel } from '@angular/forms';
2+
import { AbstractControl, NgModel, FormControlName } from '@angular/forms';
33

44
// tslint:disable-next-line
55
@Directive({selector: 'div.form-group,fieldset.form-group'})
66
export class FormGroupHasStatusDirective implements AfterViewInit {
77

88
@ContentChild(NgModel) input: NgModel;
9-
private control: FormControl;
9+
@ContentChild(FormControlName) formControl: FormControlName;
10+
private control: AbstractControl;
1011

1112
constructor() {}
1213

1314
ngAfterViewInit() {
14-
this.control = this.input && this.input.control;
15+
if (this.input) {
16+
this.control = this.input.control;
17+
} else if (this.formControl) {
18+
this.control = this.formControl.control;
19+
}
1520
}
1621

1722
@HostBinding('class.has-success') get valid() {

src/app/shared/validators/checkbox-checked.validator.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { Directive, forwardRef } from '@angular/core';
22
import { FormControl, NG_VALIDATORS } from '@angular/forms';
33

4+
export class CheckboxValidator {
5+
static checked(c: FormControl) {
6+
return new CheckboxCheckedValidator().validate(c);
7+
}
8+
}
49

510
@Directive({
611
// tslint:disable-next-line

src/app/shared/validators/email.validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Directive, forwardRef } from '@angular/core';
22
import { NG_VALIDATORS, FormControl } from '@angular/forms';
33

4-
function validateEmailFactory() {
4+
export function validateEmailFactory() {
55
return (c: FormControl) => {
66
let EMAIL_REGEXP = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
77

0 commit comments

Comments
 (0)