Angular reactive form hasValidator return false for custom validator function #7500
Unanswered
Mithun-codes
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
`import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
export function trimValidator(minlength: number, maxlength: number): ValidatorFn {
return ({ value }: AbstractControl): ValidationErrors | null => {
const trimValue = value?.trim() || '';
if (trimValue.length < minlength) {
return { minlength: true };
} else if (trimValue.length > maxlength) {
return { minlength: true }; }
};
}
`
myform = this.fbgroup({ name: ['', [Validators.required]], description: ['', [trimValidator(10, 3000)]] })
// When i access default validator through hasValidator then i got true but when i pass custom validator functiion to hasvalidator then return me false
// i am checking this in another file (in some custom-directive)
`this.myform.get('name')?.hasValidator(Validator.required) // true
this.myform.get('description')?.hasValidator(trimValidator(10, 3000)) // false why?
`
Beta Was this translation helpful? Give feedback.
All reactions