feat(forms): allow to compile forms in strictNullChecks mode (#14679)
Closes #14667 PR Close #14679
This commit is contained in:

committed by
Miško Hevery

parent
2d78c8cc05
commit
5486e5417b
@ -11,6 +11,11 @@ import {Observable} from 'rxjs/Observable';
|
||||
import {AbstractControl} from '../model';
|
||||
import {NG_VALIDATORS, Validators} from '../validators';
|
||||
|
||||
/** @experimental */
|
||||
export type ValidationErrors = {
|
||||
[key: string]: any
|
||||
};
|
||||
|
||||
/**
|
||||
* An interface that can be implemented by classes that can act as validators.
|
||||
*
|
||||
@ -31,13 +36,13 @@ import {NG_VALIDATORS, Validators} from '../validators';
|
||||
* @stable
|
||||
*/
|
||||
export interface Validator {
|
||||
validate(c: AbstractControl): {[key: string]: any};
|
||||
validate(c: AbstractControl): ValidationErrors|null;
|
||||
registerOnValidatorChange?(fn: () => void): void;
|
||||
}
|
||||
|
||||
/** @experimental */
|
||||
export interface AsyncValidator extends Validator {
|
||||
validate(c: AbstractControl): Promise<{[key: string]: any}>|Observable<{[key: string]: any}>;
|
||||
validate(c: AbstractControl): Promise<ValidationErrors|null>|Observable<ValidationErrors|null>;
|
||||
}
|
||||
|
||||
export const REQUIRED_VALIDATOR: Provider = {
|
||||
@ -75,14 +80,14 @@ export class RequiredValidator implements Validator {
|
||||
private _onChange: () => void;
|
||||
|
||||
@Input()
|
||||
get required(): boolean /*| string*/ { return this._required; }
|
||||
get required(): boolean|string { return this._required; }
|
||||
|
||||
set required(value: boolean) {
|
||||
set required(value: boolean|string) {
|
||||
this._required = value != null && value !== false && `${value}` !== 'false';
|
||||
if (this._onChange) this._onChange();
|
||||
}
|
||||
|
||||
validate(c: AbstractControl): {[key: string]: any} {
|
||||
validate(c: AbstractControl): ValidationErrors|null {
|
||||
return this.required ? Validators.required(c) : null;
|
||||
}
|
||||
|
||||
@ -108,7 +113,7 @@ export class RequiredValidator implements Validator {
|
||||
host: {'[attr.required]': 'required ? "" : null'}
|
||||
})
|
||||
export class CheckboxRequiredValidator extends RequiredValidator {
|
||||
validate(c: AbstractControl): {[key: string]: any} {
|
||||
validate(c: AbstractControl): ValidationErrors|null {
|
||||
return this.required ? Validators.requiredTrue(c) : null;
|
||||
}
|
||||
}
|
||||
@ -150,7 +155,7 @@ export class EmailValidator implements Validator {
|
||||
if (this._onChange) this._onChange();
|
||||
}
|
||||
|
||||
validate(c: AbstractControl): {[key: string]: any} {
|
||||
validate(c: AbstractControl): ValidationErrors|null {
|
||||
return this._enabled ? Validators.email(c) : null;
|
||||
}
|
||||
|
||||
@ -160,13 +165,13 @@ export class EmailValidator implements Validator {
|
||||
/**
|
||||
* @stable
|
||||
*/
|
||||
export interface ValidatorFn { (c: AbstractControl): {[key: string]: any}; }
|
||||
export interface ValidatorFn { (c: AbstractControl): ValidationErrors|null; }
|
||||
|
||||
/**
|
||||
* @stable
|
||||
*/
|
||||
export interface AsyncValidatorFn {
|
||||
(c: AbstractControl): Promise<{[key: string]: any}>|Observable<{[key: string]: any}>;
|
||||
(c: AbstractControl): Promise<ValidationErrors|null>|Observable<ValidationErrors|null>;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -207,7 +212,7 @@ export class MinLengthValidator implements Validator,
|
||||
}
|
||||
}
|
||||
|
||||
validate(c: AbstractControl): {[key: string]: any} {
|
||||
validate(c: AbstractControl): ValidationErrors|null {
|
||||
return this.minlength == null ? null : this._validator(c);
|
||||
}
|
||||
|
||||
@ -257,7 +262,7 @@ export class MaxLengthValidator implements Validator,
|
||||
}
|
||||
}
|
||||
|
||||
validate(c: AbstractControl): {[key: string]: any} {
|
||||
validate(c: AbstractControl): ValidationErrors|null {
|
||||
return this.maxlength != null ? this._validator(c) : null;
|
||||
}
|
||||
|
||||
@ -299,7 +304,7 @@ export class PatternValidator implements Validator,
|
||||
private _validator: ValidatorFn;
|
||||
private _onChange: () => void;
|
||||
|
||||
@Input() pattern: string /*|RegExp*/;
|
||||
@Input() pattern: string|RegExp;
|
||||
|
||||
ngOnChanges(changes: SimpleChanges): void {
|
||||
if ('pattern' in changes) {
|
||||
@ -308,7 +313,7 @@ export class PatternValidator implements Validator,
|
||||
}
|
||||
}
|
||||
|
||||
validate(c: AbstractControl): {[key: string]: any} { return this._validator(c); }
|
||||
validate(c: AbstractControl): ValidationErrors|null { return this._validator(c); }
|
||||
|
||||
registerOnValidatorChange(fn: () => void): void { this._onChange = fn; }
|
||||
|
||||
|
Reference in New Issue
Block a user