Skip to content

Commit 0453389

Browse files
committed
Angular Forms Course
1 parent d2ed6a9 commit 0453389

6 files changed

+93
-19
lines changed

src/app/address-form/address-form.component.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,16 @@
3434

3535
</mat-form-field>
3636

37+
<div class="form-val">
38+
39+
{{form.value | json}}
40+
41+
</div>
42+
43+
<div class="form-val">
44+
45+
{{form.valid | json}}
46+
47+
</div>
48+
3749
</fieldset>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
.address-error {
3+
color:red;
4+
}

src/app/address-form/address-form.component.ts

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import {Component, forwardRef, Input, OnDestroy, OnInit} from '@angular/core';
2-
import {ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR, Validators} from '@angular/forms';
1+
import {Component, Input, OnDestroy, OnInit} from '@angular/core';
2+
import {
3+
AbstractControl,
4+
ControlValueAccessor,
5+
FormBuilder, FormGroup,
6+
NG_VALIDATORS,
7+
NG_VALUE_ACCESSOR,
8+
Validator,
9+
Validators
10+
} from '@angular/forms';
311
import {noop, Subscription} from 'rxjs';
412

513
@Component({
@@ -10,30 +18,36 @@ import {noop, Subscription} from 'rxjs';
1018
{
1119
provide: NG_VALUE_ACCESSOR,
1220
multi: true,
13-
useExisting: forwardRef(() => AddressFormComponent)
14-
}
21+
useExisting: AddressFormComponent
22+
},
23+
{
24+
provide: NG_VALIDATORS,
25+
multi: true,
26+
useExisting: AddressFormComponent
27+
},
1528
]
1629
})
17-
export class AddressFormComponent implements ControlValueAccessor, OnDestroy {
30+
export class AddressFormComponent implements ControlValueAccessor, OnDestroy, Validator {
1831

1932
@Input()
2033
legend:string;
2134

22-
form: FormGroup;
35+
form: FormGroup = this.fb.group({
36+
addressLine1: [null, [Validators.required]],
37+
addressLine2: [null, [Validators.required]],
38+
zipCode: [null, [Validators.required]],
39+
city: [null, [Validators.required]]
40+
});
2341

24-
onTouched: Function = noop;
42+
onTouched: Function = () => {};
43+
onValidationChange: Function = () => {};
2544

2645
onChangeSubs: Subscription[] = [];
2746

2847
constructor(private fb: FormBuilder) {
29-
this.form = this.fb.group({
30-
addressLine1: ["", Validators.required],
31-
addressLine2: ["", Validators.required],
32-
zipCode: ["", Validators.required],
33-
city: ["", Validators.required]
34-
});
3548
}
3649

50+
3751
ngOnDestroy() {
3852
for (let sub of this.onChangeSubs) {
3953
sub.unsubscribe();
@@ -64,4 +78,43 @@ export class AddressFormComponent implements ControlValueAccessor, OnDestroy {
6478
}
6579
}
6680

81+
registerOnValidatorChange(fn: () => void) {
82+
this.onValidationChange = fn;
83+
}
84+
85+
validate(control: AbstractControl) {
86+
87+
if (this.form.valid) {
88+
return null;
89+
}
90+
91+
let errors : any = {};
92+
93+
errors = this.addControlErrors(errors, "addressLine1");
94+
errors = this.addControlErrors(errors, "addressLine2");
95+
errors = this.addControlErrors(errors, "zipCode");
96+
errors = this.addControlErrors(errors, "city");
97+
98+
console.log("errors", errors);
99+
100+
return errors;
101+
}
102+
103+
addControlErrors(allErrors: any, controlName:string) {
104+
105+
const errors = {...allErrors};
106+
107+
const controlErrors = this.form.controls[controlName].errors;
108+
109+
if (controlErrors) {
110+
errors[controlName] = controlErrors;
111+
}
112+
113+
return errors;
114+
115+
}
116+
67117
}
118+
119+
120+

src/app/create-course/create-course-step-one/create-course-step-one.component.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@
7878

7979
</mat-form-field>
8080

81-
<!--address-form formControlName="addressOne" legend="Address 1"></address-form>
81+
<address-form formControlName="address" legend="Address"></address-form>
8282

83-
<address-form formControlName="addressTwo" legend="Address 2"></address-form-->
83+
<!--address-form formControlName="addressTwo" legend="Address 2"></address-form-->
8484

8585
</div>
8686

@@ -89,3 +89,9 @@
8989
{{form.value | json}}
9090

9191
</div>
92+
93+
<div class="form-val">
94+
95+
{{form.errors | json}}
96+
97+
</div>

src/app/create-course/create-course-step-one/create-course-step-one.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ export class CreateCourseStepOneComponent implements OnInit {
3333
downloadsAllowed: [false, Validators.requiredTrue],
3434
releasedAt: [new Date(), Validators.required],
3535
longDescription: ["",[Validators.required, Validators.minLength(3)]],
36-
addressOne: [null],
37-
addressTwo: [null]
36+
address: [null]
3837
});
3938
}
4039

src/app/create-course/create-course.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h2 class="title">Create New Course</h2>
1313
<create-course-step-one #stepOne></create-course-step-one>
1414

1515
<div class="stepper-buttons">
16-
<button mat-raised-button color="primary" matStepperNext>Continue to Step 2</button>
16+
<button mat-raised-button color="primary" matStepperNext [disabled]="!stepOne.form.valid">Continue to Step 2</button>
1717
</div>
1818

1919
</mat-step>
@@ -26,7 +26,7 @@ <h2 class="title">Create New Course</h2>
2626

2727
<div class="stepper-buttons">
2828
<button mat-raised-button matStepperPrevious>Back</button>
29-
<button mat-raised-button color="primary" matStepperNext>Continue to Step 3</button>
29+
<button mat-raised-button color="primary" matStepperNext [disabled]="!stepTwo.form.valid">Continue to Step 3</button>
3030
</div>
3131

3232
</mat-step>

0 commit comments

Comments
 (0)