I have a form which changes its validation based on some value from the dropdown. If college A is selected then min percentage required in 60, if college B is selected then min percentage drops down to 55. Although I have figured out a way to get the min error in the template in so that I didn't have to hard code the percentage value in my template. Although I am not sure if this is the correct way or not.
<div formGroupName="marks">
<div class="form-group">
<label for="email" class="col-lg-3 control-label">Semester 1</label>
<div class="col-lg-3">
<input autocomplete="off"
#sem1
type="text"
min="0"
max="99"
maxlength="1"
maxlength="2"
class="form-control"
id="email"
placeholder="Sem 1 (%)"
(keypress)="onlyNumberKey($event)"
formControlName="sem1">
<span *ngIf="registrationForm.controls['marks'].controls['sem1'].hasError('required') &&
registrationForm.controls['marks'].controls['sem1'].touched"
class="text-danger">
Sem 1 percentage required
</span>
<span *ngIf="registrationForm.controls['marks'].controls['sem1'].hasError('min') ||
registrationForm.controls['marks'].controls['sem1'].hasError('max') ||
registrationForm.controls['marks'].controls['sem1'].hasError('minlength')||
registrationForm.controls['marks'].controls['sem1'].hasError('maxlength') &&
registrationForm.controls['marks'].controls['sem1'].touched"
class="text-danger">
Percenatge must be {{ registrationForm.get('marks.sem1')._errors.min.min }} and above.
</span>
</div>
</div>
</div>
Component
registrationForm = new FormGroup({
college: new FormControl('', [
Validators.required, dropDrownValidator
]),
marks: new FormGroup({
sem1: new FormControl('',
[
Validators.required,
Validators.min(60),
Validators.max(99),
Validators.minLength(1),
Validators.maxLength(2)
]
)
})
});