refactor(forms): update email validator to inherit abstractValidator (#44545)

Modified email validator to inherit abstractValidator.

For every validato type different PR will be raised as discussed in #42378.

Closes #42267

PR Close #44545
This commit is contained in:
iRealNirmal
2021-12-21 15:08:09 +05:30
committed by Andrew Scott
parent c888dfdea0
commit c7b210d484
2 changed files with 23 additions and 27 deletions
+17 -23
View File
@@ -470,35 +470,29 @@ export const EMAIL_VALIDATOR: any = {
selector: '[email][formControlName],[email][formControl],[email][ngModel]',
providers: [EMAIL_VALIDATOR]
})
export class EmailValidator implements Validator {
private _enabled = false;
private _onChange?: () => void;
export class EmailValidator extends AbstractValidatorDirective {
/**
* @description
* Tracks changes to the email attribute bound to this directive.
*/
@Input()
set email(value: boolean|string) {
this._enabled = value === '' || value === true || value === 'true';
if (this._onChange) this._onChange();
}
@Input() email!: boolean|string;
/**
* Method that validates whether an email address is valid.
* Returns the validation result if enabled, otherwise null.
* @nodoc
*/
validate(control: AbstractControl): ValidationErrors|null {
return this._enabled ? emailValidator(control) : null;
}
/** @internal */
override inputName = 'email';
/**
* Registers a callback function to call when the validator inputs change.
* @nodoc
*/
registerOnValidatorChange(fn: () => void): void {
this._onChange = fn;
/** @internal */
override normalizeInput = (input: unknown): boolean =>
// Avoid TSLint requirement to omit semicolon, see
// https://github.com/palantir/tslint/issues/1476
// tslint:disable-next-line:semicolon
(input === '' || input === true || input === 'true');
/** @internal */
override createValidator = (input: number): ValidatorFn => emailValidator;
/** @nodoc */
override enabled(input: boolean): boolean {
return input;
}
}