I have a nested form group called 'grades':
Although the nested form group 'grades' has the ng-invalid class applied the children form controls do have applied the ng-valid class.
Why is the invalidation not inherited from the nested form to its controls?
this.schoolyearForm = this.formBuilder.group({
name: [this.createSchoolyear.name, [Validators.minLength(3), Validators.required]],
endDate: [this.createSchoolyear.endDate, Validators.required],
startDate: [this.createSchoolyear.startDate, Validators.required],
grades: this.formBuilder.group({
bestGrade: [this.createSchoolyear.bestGrade, Validators.required],
worstGrade: [this.createSchoolyear.worstGrade, Validators.required]
}, { validator: this.gradesCompare })
});
gradesCompare(c: AbstractControl): { [key: string]: boolean } | null
{
let bestGradeControl = c.get("bestGrade");
let worstGradeControl = c.get("worstGrade");
if (bestGradeControl.pristine || worstGradeControl.pristine)
return null;
if (bestGradeControl.value === worstGradeControl.value)
{
return { "match": true };
}
return null;
}
See the red and green borders:
On the FormControl`s are the css ng-invalid classes not applied:
.ng-valid:not(form) {
border-left: 5px solid #42A948; /* green */
}
.ng-invalid:not(form) {
border-left: 5px solid #a94442; /* red */
}
Therefore they are bordered green, but I expected them to be red!
UPDATE
I changed the plunkr of user Ben to this one:
https://plnkr.co/edit/VobcC0Qw1EBDytYdkzru?p=preview
It works as I wished it would work, but it seems like a workaround to me. The multiple ng-class expression are hard to understand...
Please Ben grab this plunkr as yours, post it as solution, but only if you think honestly you would also be satisfied with the solution. If not try to find a better solution please, now that you fully know my requirements.
Others are also welcome to join this challenge, now that you know how the validation/ui/error-messages should really behave.