Skip to content

Commit 1650e80

Browse files
committed
Angular Forms Course
1 parent a036286 commit 1650e80

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
<input matInput
66
placeholder="Course title"
7-
formControlName="title" #titleHint>
7+
formControlName="title" #title>
88

99
<mat-hint align="end">
10-
{{titleHint.value.length}} / 60
10+
{{title.value.length}} / 60
1111
</mat-hint>
1212

13+
<mat-error *ngIf="form.controls['title']?.errors?.titleExists">This title is already being used.</mat-error>
14+
1315
</mat-form-field>
1416

1517
<mat-form-field>
@@ -75,10 +77,10 @@
7577

7678
</mat-form-field>
7779

78-
<!--div class="form-val">
80+
<div class="form-val">
7981

80-
{{form.errors | json}}
82+
{{form.controls['title'].errors | json}}
8183

82-
</div-->
84+
</div>
8385

8486
</div>

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {createPasswordStrengthValidator} from '../../validators/password-strengt
44
import {createPromoRangeValidator} from '../../validators/date-range.validator';
55
import {CoursesService} from '../../services/courses.service';
66
import {Observable} from 'rxjs';
7+
import {courseTitleValidator} from '../../validators/course-title.validator';
78

89
@Component({
910
selector: 'create-course-step-one',
@@ -19,7 +20,13 @@ export class CreateCourseStepOneComponent implements OnInit {
1920

2021
constructor(private fb: FormBuilder, private courses: CoursesService) {
2122
this.form = fb.group({
22-
title: ["", [Validators.required, Validators.minLength(3), Validators.maxLength(60)]],
23+
title: [
24+
"", {
25+
validators: [Validators.required, Validators.minLength(3), Validators.maxLength(60)],
26+
asyncValidators: [courseTitleValidator(this.courses)],
27+
updateOn: "blur"
28+
}
29+
],
2330
adminPassword: ["", [Validators.required, Validators.minLength(8), createPasswordStrengthValidator()]],
2431
supportEmail: ["", [Validators.required, Validators.email]],
2532
category: ["BEGINNER", Validators.required],
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {CoursesService} from '../services/courses.service';
2+
import {AbstractControl, AsyncValidatorFn} from '@angular/forms';
3+
import {map} from 'rxjs/operators';
4+
5+
6+
export function courseTitleValidator(courses: CoursesService): AsyncValidatorFn {
7+
return (control: AbstractControl) => {
8+
return courses.findAllCourses()
9+
.pipe(
10+
map(courses => {
11+
12+
const course = courses.find(course => course.description.toLowerCase() == control.value.toLowerCase());
13+
14+
return course ? {titleExists:true} : null;
15+
})
16+
)
17+
};
18+
}

0 commit comments

Comments
 (0)