Skip to content

Commit 2ee68bc

Browse files
author
Your Name
committed
Angular Forms In Depth
1 parent 9f836b9 commit 2ee68bc

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@
66

77
<input matInput
88
placeholder="Address Line 1"
9-
formControlName="addressLine1">
9+
formControlName="addressLine1" (blur)="onTouched()">
1010

1111
</mat-form-field>
1212

1313
<mat-form-field>
1414

1515
<input matInput
1616
placeholder="Address Line 2"
17-
formControlName="addressLine2">
17+
formControlName="addressLine2" (blur)="onTouched()">
1818

1919
</mat-form-field>
2020

2121
<mat-form-field>
2222

2323
<input matInput
2424
placeholder="Zip Code"
25-
formControlName="zipCode">
25+
formControlName="zipCode" (blur)="onTouched()">
2626

2727
</mat-form-field>
2828

2929
<mat-form-field>
3030

3131
<input matInput
3232
placeholder="City"
33-
formControlName="city">
33+
formControlName="city" (blur)="onTouched()">
3434

3535
</mat-form-field>
3636

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

+55-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,24 @@ import {noop, Subscription} from 'rxjs';
1313
@Component({
1414
selector: 'address-form',
1515
templateUrl: './address-form.component.html',
16-
styleUrls: ['./address-form.component.scss']
16+
styleUrls: ['./address-form.component.scss'],
17+
providers: [
18+
{
19+
provide: NG_VALUE_ACCESSOR,
20+
multi:true,
21+
useExisting: AddressFormComponent
22+
}
23+
]
1724
})
18-
export class AddressFormComponent {
25+
export class AddressFormComponent implements ControlValueAccessor, OnDestroy {
1926

2027
@Input()
2128
legend:string;
2229

30+
onTouched = () => {};
31+
32+
onChangeSub: Subscription;
33+
2334
form: FormGroup = this.fb.group({
2435
addressLine1: [null, [Validators.required]],
2536
addressLine2: [null, [Validators.required]],
@@ -28,9 +39,51 @@ export class AddressFormComponent {
2839
});
2940

3041
constructor(private fb: FormBuilder) {
42+
43+
}
44+
45+
registerOnChange(onChange: any) {
46+
this.onChangeSub = this.form.valueChanges.subscribe(onChange);
47+
}
48+
49+
ngOnDestroy() {
50+
this.onChangeSub.unsubscribe();
51+
}
52+
53+
54+
writeValue(value: any) {
55+
if (value) {
56+
this.form.setValue(value);
57+
}
58+
}
59+
60+
registerOnTouched(onTouched: any) {
61+
this.onTouched = onTouched;
62+
}
63+
64+
setDisabledState(disabled:boolean) {
65+
if (disabled) {
66+
this.form.disable();
67+
}
68+
else {
69+
this.form.enable();
70+
}
71+
3172
}
3273

3374
}
3475

3576

3677

78+
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
89+

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
<div [formGroup]="form">
33

4-
<address-form legend="Address"></address-form>
4+
<address-form legend="Address" formControlName="address"></address-form>
55

66
<mat-form-field>
77

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export class CreateCourseStep1Component implements OnInit {
3030
releasedAt: [new Date(), Validators.required],
3131
category: ['BEGINNER', Validators.required],
3232
downloadsAllowed: [false, Validators.requiredTrue],
33-
longDescription: ['', [Validators.required, Validators.minLength(3)]]
33+
longDescription: ['', [Validators.required, Validators.minLength(3)]],
34+
address: [null, Validators.required]
3435
});
3536

3637
courseCategories$ : Observable<CourseCategory[]>;

0 commit comments

Comments
 (0)