how to pass validator in form builder angular 10 code example
Example 1: custom validator
import {AbstractControl, ValidatorFn} from '@angular/forms';
export function blue(): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } | null =>
control.value?.toLowerCase() === 'blue'
? null : {wrongColor: control.value};
}
<>
Example 2: formgroup check if valid
form: FormGroup;
onSubmit(){
//checks if form is valid
if( this.form.valid){
//more code here
}
}
Example 3: angular9+how+to+add+validators
this.form.controls["firstName"].setValidators([Validators.minLength(1), Validators.maxLength(30)]);