Skip to content

Commit 41d8a95

Browse files
author
Your Name
committed
Angular Forms In Depth
1 parent 1175037 commit 41d8a95

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
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
@@ -10,6 +10,12 @@
1010

1111
</mat-radio-group>
1212

13+
<mat-form-field>
14+
15+
<input type="number" matInput placeholder="Price" formControlName="price">
16+
17+
</mat-form-field>
18+
1319
</div>
1420

1521
<div class="form-val">

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import {FormBuilder, FormGroup, Validators} from '@angular/forms';
1010
export class CreateCourseStep2Component implements OnInit {
1111

1212
form = this.fb.group({
13-
courseType: ['premium', Validators.required]
13+
courseType: ['premium', Validators.required],
14+
price: [null, [
15+
Validators.required,
16+
Validators.min(1),
17+
Validators.max(9999),
18+
Validators.pattern("[0-9]+")
19+
]]
1420
});
1521

1622
constructor(private fb: FormBuilder) {
@@ -19,6 +25,20 @@ export class CreateCourseStep2Component implements OnInit {
1925

2026
ngOnInit() {
2127

28+
this.form.valueChanges
29+
.subscribe(val => {
30+
31+
const priceControl = this.form.controls["price"];
32+
33+
if (val.courseType == 'free' && priceControl.enabled) {
34+
priceControl.disable({emitEvent: false});
35+
}
36+
else if (val.courseType == 'premium' && priceControl.disabled) {
37+
priceControl.enable({emitEvent:false});
38+
}
39+
40+
});
41+
2242

2343

2444
}

0 commit comments

Comments
 (0)