Skip to content

Commit

Permalink
Angular Forms In Depth
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Dec 14, 2020
1 parent 1175037 commit 41d8a95
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

</mat-radio-group>

<mat-form-field>

<input type="number" matInput placeholder="Price" formControlName="price">

</mat-form-field>

</div>

<div class="form-val">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import {FormBuilder, FormGroup, Validators} from '@angular/forms';
export class CreateCourseStep2Component implements OnInit {

form = this.fb.group({
courseType: ['premium', Validators.required]
courseType: ['premium', Validators.required],
price: [null, [
Validators.required,
Validators.min(1),
Validators.max(9999),
Validators.pattern("[0-9]+")
]]
});

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

ngOnInit() {

this.form.valueChanges
.subscribe(val => {

const priceControl = this.form.controls["price"];

if (val.courseType == 'free' && priceControl.enabled) {
priceControl.disable({emitEvent: false});
}
else if (val.courseType == 'premium' && priceControl.disabled) {
priceControl.enable({emitEvent:false});
}

});



}
Expand Down

0 comments on commit 41d8a95

Please sign in to comment.