Skip to content

Commit 2793edc

Browse files
committed
Angular Forms Course
1 parent 0fc364d commit 2793edc

File tree

4 files changed

+90
-10
lines changed

4 files changed

+90
-10
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {noop, Subscription} from 'rxjs';
2727
},
2828
]
2929
})
30-
export class AddressFormComponent implements ControlValueAccessor, OnDestroy, Validator {
30+
export class AddressFormComponent implements ControlValueAccessor, OnDestroy, OnInit, Validator {
3131

3232
@Input()
3333
legend:string;
@@ -47,6 +47,12 @@ export class AddressFormComponent implements ControlValueAccessor, OnDestroy, Va
4747
constructor(private fb: FormBuilder) {
4848
}
4949

50+
ngOnInit() {
51+
52+
this.form.valueChanges.subscribe(val => this.onValidationChange());
53+
54+
}
55+
5056

5157
ngOnDestroy() {
5258
for (let sub of this.onChangeSubs) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
</mat-form-field>
3333

34-
<file-upload requiredFileType="image/png"></file-upload>
34+
<file-upload requiredFileType="image/png" formControlName="thumbnailFile"></file-upload>
3535

3636
<!--div class="form-val">
3737

src/app/file-upload/file-upload.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
<input type="file" class="file-input" (change)="onFileSelected($event)" [accept]="requiredFileType" #fileUpload>
3+
<input type="file" class="file-input" (change)="onFileSelected($event)" [accept]="requiredFileType" [disabled]="disabled" #fileUpload>
44

55

66
<div class="file-upload">
@@ -11,7 +11,7 @@
1111

1212
</mat-form-field>
1313

14-
<button mat-mini-fab color="primary" class="upload-btn" (click)="fileUpload.click()">
14+
<button mat-mini-fab color="primary" class="upload-btn" (click)="onClick(fileUpload)" [disabled]="disabled">
1515
<mat-icon>
1616
attach_file
1717
</mat-icon>

src/app/file-upload/file-upload.component.ts

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
import {Component, Input} from '@angular/core';
22
import {HttpClient, HttpEventType} from '@angular/common/http';
3-
import {finalize} from 'rxjs/operators';
3+
import {catchError, finalize} from 'rxjs/operators';
4+
import {AbstractControl, ControlValueAccessor, NG_VALIDATORS, NG_VALUE_ACCESSOR, Validator} from '@angular/forms';
5+
import {noop, of} from 'rxjs';
46

57

68
@Component({
79
selector: 'file-upload',
810
templateUrl: "file-upload.component.html",
9-
styleUrls: ["file-upload.component.scss"]
11+
styleUrls: ["file-upload.component.scss"],
12+
providers: [
13+
{
14+
provide: NG_VALUE_ACCESSOR,
15+
multi: true,
16+
useExisting: FileUploadComponent
17+
},
18+
{
19+
provide: NG_VALIDATORS,
20+
multi: true,
21+
useExisting: FileUploadComponent
22+
},
23+
]
1024
})
11-
export class FileUploadComponent {
25+
export class FileUploadComponent implements ControlValueAccessor, Validator {
1226

1327
@Input()
1428
requiredFileType:string;
@@ -17,12 +31,25 @@ export class FileUploadComponent {
1731

1832
uploadProgress:number;
1933

34+
fileUploadError = false;
2035
validFileUploaded = false;
2136

37+
disabled = false;
38+
39+
onTouched = () => {};
40+
41+
onChange = (val) => {};
42+
43+
onValidationChange = () => {};
44+
2245
constructor(private http: HttpClient) {
2346

2447
}
2548

49+
onClick(fileUpload: HTMLInputElement) {
50+
this.onTouched();
51+
fileUpload.click();
52+
}
2653

2754
onFileSelected(event) {
2855

@@ -39,21 +66,68 @@ export class FileUploadComponent {
3966
observe: 'events'
4067
})
4168
.pipe(
69+
catchError(error => {
70+
this.fileUploadError = true;
71+
return of(error);
72+
}),
4273
finalize(() => {
4374
this.uploadProgress = null;
44-
this.validFileUploaded = true;
4575
})
4676
)
4777
.subscribe(event => {
4878

49-
if ( event.type === HttpEventType.UploadProgress ) {
50-
this.uploadProgress = Math.round((100 * event.loaded) / event.total);
79+
if (event.type === HttpEventType.UploadProgress) {
80+
this.uploadProgress = Math.round((100 * event.loaded) / event.total);
81+
}
82+
else if (event.type == HttpEventType.Response) {
83+
console.log('file uploaded');
84+
this.validFileUploaded = true;
85+
this.onChange(file.name);
86+
this.onValidationChange();
5187
}
5288

5389
});
5490

5591
}
5692

93+
registerOnChange(onChange: any) {
94+
this.onChange = onChange;
95+
}
96+
97+
registerOnTouched(onTouched: any) {
98+
this.onTouched = onTouched;
99+
}
57100

101+
setDisabledState(isDisabled: boolean) {
102+
this.disabled = isDisabled;
103+
}
58104

105+
writeValue(obj: any) {
106+
// not applicable to a file upload control
107+
}
108+
109+
registerOnValidatorChange(fn: () => void) {
110+
this.onValidationChange = fn;
111+
}
112+
113+
validate(control: AbstractControl) {
114+
115+
if (this.validFileUploaded) {
116+
console.log("Valid file uploaded");
117+
return null;
118+
}
119+
120+
let errors : any = {
121+
requiredFileType: this.requiredFileType
122+
};
123+
124+
125+
if (this.fileUploadError) {
126+
errors.uploadFailed = true;
127+
}
128+
129+
console.log("validation errors", errors);
130+
131+
return errors;
132+
}
59133
}

0 commit comments

Comments
 (0)