funky-framadate-front/src/app/features/shared/components/ui/form/errors-list/errors-list.component.ts

54 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-05-04 09:33:28 +02:00
import { Component, Input, OnInit } from '@angular/core';
import { FormGroup, ValidationErrors } from '@angular/forms';
@Component({
selector: 'app-errors-list',
templateUrl: './errors-list.component.html',
styleUrls: ['./errors-list.component.scss'],
})
export class ErrorsListComponent implements OnInit {
@Input() form: FormGroup;
public totalErrors = 0;
public messages = [];
ngOnInit() {
this.form.valueChanges.subscribe((data) => {
this.getFormValidationErrors();
});
}
getFormValidationErrors(): void {
// console.log('%c ==>> Validation Errors: ', 'color: red; font-weight: bold; font-size:25px;');
let totalErrors = 0;
this.messages = [];
Object.keys(this.form.controls).forEach((key) => {
2021-05-04 09:42:56 +02:00
const theKey = this.form.get(key);
const invalidData = theKey.invalid;
if (invalidData) {
totalErrors++;
console.log('key', key, invalidData);
this.messages.push('' + key + ' est invalide');
}
const controlErrors: ValidationErrors = theKey.errors;
2021-05-04 09:33:28 +02:00
if (controlErrors != null) {
console.log('controlErrors', controlErrors);
totalErrors++;
Object.keys(controlErrors).forEach((keyError) => {
const message = '' + key + ', ' + keyError + '';
console.log(
'Key control: ' + key + ', keyError: ' + keyError + ', err value: ' + controlErrors[keyError]
);
this.messages.push(message);
});
}
});
this.totalErrors = totalErrors;
2021-05-04 09:35:23 +02:00
if (totalErrors) {
console.log('Number of errors: ', totalErrors);
}
2021-05-04 09:33:28 +02:00
}
}