I have the following code repeatedly in my forms within my components:
<div class="form-group row">
<label class="col-sm-4 col-form-label" for="description">Description</label>
<input class="form-control form-control-lg"
id="description"
type="text" placeholder="..."
formControlName="description"
[ngClass]="{
'form-control-warning': descriptionCtrl.untouched && descriptionCtrl.invalid,
'form-control-success': descriptionCtrl.valid,
'form-control-danger': descriptionCtrl.touched && descriptionCtrl.invalid }">
</div>
... Another "blocks" with the same code
So, in order to simplify this, I've created the following method:
handleClass = (control: AbstractControl): any => {
if (control.valid) {
return 'form-control-success';
} else {
if (control.touched) {
return 'form-control-danger';
} else {
return 'form-control-warning';
}
}
}
In template:
[ngClass]="handleClass(control)"
However this is still not what I want, since I need to do create this method on all components. I'm looking for a general way to do this dynamically.
PS: All inputs in my project have the same rule as the input above.
What's the best way to accomplish this? I hope my question was clear enough.