I met the same problem and for finding all validation errors and displaying them, I wrote next method:
getFormValidationErrors() {
Object.keys(this.productForm.controls).forEach(key => {
const controlErrors: ValidationErrors = this.productForm.get(key).errors;
if (controlErrors != null) {
Object.keys(controlErrors).forEach(keyError => {
console.log('Key control: ' + key + ', keyError: ' + keyError + ', err value: ', controlErrors[keyError]);
});
}
});
}
Form name productForm
should be changed to yours.
It works in next way: we get all our controls from form in format {[p: string]: AbstractControl}
and iterate by each error key, for get details of error. It skips null
error values.
It also can be changed for displaying validation errors on the template view, just replace console.log(..)
to what you need.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…