check if form control has validator code example
Example 1: angular check if control has required validator
import { AbstractControl } from '@angular/forms';
export const isControlRequired = (control: AbstractControl): boolean => {
if (!control) {
return false;
}
if (control.validator) {
const validator = control.validator({} as AbstractControl);
if (validator && validator.required) {
return true;
}
}
return false;
};
Example 2: formgroup check if valid
form: FormGroup;
onSubmit(){
//checks if form is valid
if( this.form.valid){
//more code here
}
}