Skip to content

Commit d40802e

Browse files
author
Your Name
committed
controlvalueaccessor
1 parent e5a26f7 commit d40802e

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141

4242
</div>
4343

44+
<div class="form-val">
45+
46+
Form validity state: {{form.valid }}
47+
48+
</div>
49+
4450
<div class="form-val">
4551

4652
{{form.value | json}}

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

+36-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Component, Input} from '@angular/core';
22
import {HttpClient, HttpEventType} from '@angular/common/http';
33
import {catchError, finalize} from 'rxjs/operators';
44
import {of} from 'rxjs';
5-
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
5+
import {AbstractControl, ControlValueAccessor, NG_VALIDATORS, NG_VALUE_ACCESSOR, ValidationErrors, Validator} from '@angular/forms';
66

77

88
@Component({
@@ -14,10 +14,15 @@ import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
1414
provide: NG_VALUE_ACCESSOR,
1515
multi: true,
1616
useExisting: FileUploadComponent
17+
},
18+
{
19+
provide: NG_VALIDATORS,
20+
multi: true,
21+
useExisting: FileUploadComponent
1722
}
1823
]
1924
})
20-
export class FileUploadComponent implements ControlValueAccessor {
25+
export class FileUploadComponent implements ControlValueAccessor, Validator {
2126

2227
@Input()
2328
requiredFileType:string;
@@ -26,12 +31,16 @@ export class FileUploadComponent implements ControlValueAccessor {
2631

2732
fileUploadError = false;
2833

34+
fileUploadSuccess = false;
35+
2936
uploadProgress:number;
3037

3138
onChange = (fileName:string) => {};
3239

3340
onTouched = () => {};
3441

42+
onValidatorChange = () => {};
43+
3544
disabled : boolean = false;
3645

3746
constructor(private http: HttpClient) {
@@ -75,8 +84,9 @@ export class FileUploadComponent implements ControlValueAccessor {
7584
this.uploadProgress = Math.round(100 * (event.loaded / event.total));
7685
}
7786
else if (event.type == HttpEventType.Response) {
87+
this.fileUploadSuccess = true;
7888
this.onChange(this.fileName);
79-
89+
this.onValidatorChange();
8090
}
8191
});
8292

@@ -102,6 +112,29 @@ export class FileUploadComponent implements ControlValueAccessor {
102112
this.disabled = disabled;
103113
}
104114

115+
registerOnValidatorChange(onValidatorChange: () => void) {
116+
this.onValidatorChange = onValidatorChange;
117+
}
118+
119+
validate(control: AbstractControl): ValidationErrors | null {
120+
121+
if (this.fileUploadSuccess) {
122+
return null;
123+
}
124+
125+
let errors: any = {
126+
requiredFileType: this.requiredFileType
127+
};
128+
129+
if (this.fileUploadError) {
130+
errors.uploadFailed = true;
131+
}
132+
133+
return errors;
134+
}
135+
136+
137+
105138

106139
}
107140

0 commit comments

Comments
 (0)