ng-form-group-validation
Version:
A simple validation library for Angular
77 lines (68 loc) • 2.67 kB
text/typescript
import { AsyncPipe } from '@angular/common';
import { Component, ContentChild, ElementRef, Inject, Input, Optional, ViewChild } from '@angular/core';
import { NgControl, Validators } from '@angular/forms';
import { BehaviorSubject } from 'rxjs';
import { ERROR_MESSAGE } from './error-messages';
export class NgValidationComponent {
label = "field";
formControl!: NgControl;
formControlElement!: ElementRef;
formGroup!: ElementRef;
isControlError = false;
requiredStar = false;
errorMsg = new BehaviorSubject<string>("");
constructor( () public customMessages: any = {} ) { }
ngAfterViewInit(): void {
this.formControl?.control?.valueChanges.subscribe(value => {
debugger;
if (this.formControl && this.formControl.control && this.formControl.control.errors) {
this.formGroup.nativeElement.classList.add('e-error');
this.isControlError = true;
this.errorMsg.next(this.getErrorMessage(this.formControl, this.label));
}
if (this.formControl && this.formControl.control && !this.formControl.control.errors) {
this.formGroup.nativeElement.classList.remove('e-error');
this.isControlError = false;
}
})
}
get validator() {
return this?.formControl?.control?.hasValidator(Validators.required)
}
getErrorMessage(formControl: NgControl, field: string) {
let name: string = field || 'This Field';
name = name.charAt(0).toUpperCase() + name.slice(1);
if (formControl.invalid) {
const errors: any = formControl.control?.errors
const [errorKey] = Object.keys(errors)
let message = `${name} is invalid`;
if ( this.customMessages &&errorKey in this.customMessages) {
return this.customMessages[errorKey as keyof typeof this.customMessages](name, errors[errorKey]);
} else if (errorKey in ERROR_MESSAGE) {
return ERROR_MESSAGE[errorKey as keyof typeof ERROR_MESSAGE](name, errors[errorKey]);
}
return message;
}
return (`${name} is invalid`)
}
}