You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug - Input - Validation - First time validation doesn't show error #7558
@willshowell are you referring to this line: { provide: ErrorStateMatcher, useClass: ShowOnDirtyErrorStateMatcher }? As this is not working for me, I was wondering how one can use it.
@willshowell i made a form that gets pre populated by using patchValue. Problem was the form did get marked as INVALID, but the Material2 components didnt mark the individual fields as invalid. As a result the user couldn't see what field had an error. Only if you would touch an invalid field, it would become "red". So this has now been solved, but I was surprised fixing this required looping over all the fields. I thought something like:
This will also mark all direct ancestors as dirty to maintain the model.
So it looks like marking bubbles up to ancestors but does not affect child controls.
If you're not satisfied with looping over all the controls manually, a more elegant solution might be to use your own custom ErrorStateMatcher. As you may know, the default one only shows mat-errors when the control is touched AND invalid. You could provide one that shows errors any time the control is invalid at all!
@willshowell that sounds like a great idea. I'm using the form to validate a csv import file before sending it to the server(so one can correct mistakes as well). So thats why every little speed bump can help to make the experience better. As a csv file could easily result in thousands reactive forms, you can imagine the impact of having to loop over all those controls.
this is working perfectly for me:
on form submit:
if (!this.elementForm.valid) {
Object.keys(this.elementForm.controls).forEach(field => {
const control = this.elementForm.get(field);
control.markAsTouched({ onlySelf: true });
});
return;
}
this is working perfectly for me:
on form submit:
if (!this.elementForm.valid) {
Object.keys(this.elementForm.controls).forEach(field => {
const control = this.elementForm.get(field);
control.markAsTouched({ onlySelf: true });
});
return;
}
worked for me, thanks
angular-automatic-lock-bot commented on Sep 10, 2019
Activity
willshowell commentedon Oct 5, 2017
Per the spec, the default error behavior is to wait until the invalid input has been
touched. You can customize it to show whendirtyif you wish.sofarojo commentedon Oct 9, 2017
Yes, you right, thank you 👍
cmddavid commentedon Jan 10, 2018
@willshowell are you referring to this line:
{ provide: ErrorStateMatcher, useClass: ShowOnDirtyErrorStateMatcher }? As this is not working for me, I was wondering how one can use it.Edit: i made it working using the following
But this does seem overkill to me, any better way of doing this without looping over all the form fields with map method?
willshowell commentedon Jan 10, 2018
@cmddavid what exactly are you trying to do?
This Stackblitz demonstrates showing errors when
dirtyinstead oftouched.This Stackblitz (taken from the docs site) demonstrates showing errors when
dirtyORtouched.cmddavid commentedon Jan 10, 2018
@willshowell i made a form that gets pre populated by using patchValue. Problem was the form did get marked as INVALID, but the Material2 components didnt mark the individual fields as invalid. As a result the user couldn't see what field had an error. Only if you would touch an invalid field, it would become "red". So this has now been solved, but I was surprised fixing this required looping over all the fields. I thought something like:
elementForm.markAsDirty();Should mark the complete form as dirty.
willshowell commentedon Jan 10, 2018
@cmddavid The docs for
markAsDirtysay,So it looks like marking bubbles up to ancestors but does not affect child controls.
If you're not satisfied with looping over all the controls manually, a more elegant solution might be to use your own custom ErrorStateMatcher. As you may know, the default one only shows
mat-errors when the control istouchedANDinvalid. You could provide one that shows errors any time the control is invalid at all!cmddavid commentedon Jan 10, 2018
@willshowell that sounds like a great idea. I'm using the form to validate a csv import file before sending it to the server(so one can correct mistakes as well). So thats why every little speed bump can help to make the experience better. As a csv file could easily result in thousands reactive forms, you can imagine the impact of having to loop over all those controls.
ruonan59 commentedon Apr 30, 2018
this is working perfectly for me:
on form submit:
if (!this.elementForm.valid) {
Object.keys(this.elementForm.controls).forEach(field => {
const control = this.elementForm.get(field);
control.markAsTouched({ onlySelf: true });
});
return;
}
JonathanPlacester commentedon Feb 11, 2019
worked for me, thanks
angular-automatic-lock-bot commentedon Sep 10, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.