mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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:
committed by
Andrew Scott
parent
c888dfdea0
commit
c7b210d484
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user