I have a simple login page with a reactive form and subform component app-login-form
that uses ControlValueAccessor
, which is working, but I can't figure out how to display the errors in the subform. This is an example before I start creating more complex forms.
When submitted I try to access the subform and markAllAsTouched
, but when I'm watching the elements that the classes don't change.
I made a quick StackBlitz to show what I'm doing. How do I get the error messages to show up when I submit the form?
public onSubmit(event: Event): void {
if (this.form.valid) {
console.log('VALID', this.form.value);
} else {
console.log('INVALID', this.form.value);
Object.keys(this.form.controls).forEach((controlName) => {
console.log('SHOW_ERRORS', controlName);
const control = this.form.get(controlName);
// ISSUE: Nothing changes on the element still ng-untouched, and
// was expecting it to be ng-touched
control.markAllAsTouched();
});
}
}