refactor(forms): remove facade (#12558)

This commit is contained in:
Dzmitry Shylovich 2016-11-16 05:48:34 +03:00 committed by Victor Berchet
parent 6a212fd561
commit f79b320fc4
2 changed files with 52 additions and 35 deletions

View File

@ -5,10 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be * Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {Directive, Input, OnChanges, SimpleChanges, forwardRef} from '@angular/core'; import {Directive, Input, OnChanges, SimpleChanges, forwardRef} from '@angular/core';
import {isPresent} from '../facade/lang';
import {AbstractControl} from '../model'; import {AbstractControl} from '../model';
import {NG_VALIDATORS, Validators} from '../validators'; import {NG_VALIDATORS, Validators} from '../validators';
@ -57,17 +54,17 @@ export const REQUIRED_VALIDATOR: any = {
@Directive({ @Directive({
selector: '[required][formControlName],[required][formControl],[required][ngModel]', selector: '[required][formControlName],[required][formControl],[required][ngModel]',
providers: [REQUIRED_VALIDATOR], providers: [REQUIRED_VALIDATOR],
host: {'[attr.required]': 'required? "" : null'} host: {'[attr.required]': 'required ? "" : null'}
}) })
export class RequiredValidator implements Validator { export class RequiredValidator implements Validator {
private _required: boolean; private _required: boolean;
private _onChange: () => void; private _onChange: () => void;
@Input() @Input()
get required(): boolean { return this._required; } get required(): boolean /*| string*/ { return this._required; }
set required(value: boolean) { set required(value: boolean) {
this._required = isPresent(value) && `${value}` !== 'false'; this._required = value != null && value !== false && `${value}` !== 'false';
if (this._onChange) this._onChange(); if (this._onChange) this._onChange();
} }
@ -75,7 +72,7 @@ export class RequiredValidator implements Validator {
return this.required ? Validators.required(c) : null; return this.required ? Validators.required(c) : null;
} }
registerOnValidatorChange(fn: () => void) { this._onChange = fn; } registerOnValidatorChange(fn: () => void): void { this._onChange = fn; }
} }
/** /**
@ -112,7 +109,7 @@ export const MIN_LENGTH_VALIDATOR: any = {
@Directive({ @Directive({
selector: '[minlength][formControlName],[minlength][formControl],[minlength][ngModel]', selector: '[minlength][formControlName],[minlength][formControl],[minlength][ngModel]',
providers: [MIN_LENGTH_VALIDATOR], providers: [MIN_LENGTH_VALIDATOR],
host: {'[attr.minlength]': 'minlength? minlength : null'} host: {'[attr.minlength]': 'minlength ? minlength : null'}
}) })
export class MinLengthValidator implements Validator, export class MinLengthValidator implements Validator,
OnChanges { OnChanges {
@ -121,12 +118,8 @@ export class MinLengthValidator implements Validator,
@Input() minlength: string; @Input() minlength: string;
private _createValidator() { ngOnChanges(changes: SimpleChanges): void {
this._validator = Validators.minLength(parseInt(this.minlength, 10)); if ('minlength' in changes) {
}
ngOnChanges(changes: SimpleChanges) {
if (changes['minlength']) {
this._createValidator(); this._createValidator();
if (this._onChange) this._onChange(); if (this._onChange) this._onChange();
} }
@ -136,7 +129,11 @@ export class MinLengthValidator implements Validator,
return this.minlength == null ? null : this._validator(c); return this.minlength == null ? null : this._validator(c);
} }
registerOnValidatorChange(fn: () => void) { this._onChange = fn; } registerOnValidatorChange(fn: () => void): void { this._onChange = fn; }
private _createValidator(): void {
this._validator = Validators.minLength(parseInt(this.minlength, 10));
}
} }
/** /**
@ -162,7 +159,7 @@ export const MAX_LENGTH_VALIDATOR: any = {
@Directive({ @Directive({
selector: '[maxlength][formControlName],[maxlength][formControl],[maxlength][ngModel]', selector: '[maxlength][formControlName],[maxlength][formControl],[maxlength][ngModel]',
providers: [MAX_LENGTH_VALIDATOR], providers: [MAX_LENGTH_VALIDATOR],
host: {'[attr.maxlength]': 'maxlength? maxlength : null'} host: {'[attr.maxlength]': 'maxlength ? maxlength : null'}
}) })
export class MaxLengthValidator implements Validator, export class MaxLengthValidator implements Validator,
OnChanges { OnChanges {
@ -171,22 +168,22 @@ export class MaxLengthValidator implements Validator,
@Input() maxlength: string; @Input() maxlength: string;
private _createValidator() { ngOnChanges(changes: SimpleChanges): void {
this._validator = Validators.maxLength(parseInt(this.maxlength, 10)); if ('maxlength' in changes) {
}
ngOnChanges(changes: SimpleChanges) {
if (changes['maxlength']) {
this._createValidator(); this._createValidator();
if (this._onChange) this._onChange(); if (this._onChange) this._onChange();
} }
} }
validate(c: AbstractControl): {[key: string]: any} { validate(c: AbstractControl): {[key: string]: any} {
return isPresent(this.maxlength) ? this._validator(c) : null; return this.maxlength != null ? this._validator(c) : null;
} }
registerOnValidatorChange(fn: () => void) { this._onChange = fn; } registerOnValidatorChange(fn: () => void): void { this._onChange = fn; }
private _createValidator(): void {
this._validator = Validators.maxLength(parseInt(this.maxlength, 10));
}
} }
@ -220,20 +217,18 @@ export class PatternValidator implements Validator,
private _validator: ValidatorFn; private _validator: ValidatorFn;
private _onChange: () => void; private _onChange: () => void;
@Input() pattern: string; @Input() pattern: string /*|RegExp*/;
private _createValidator() { this._validator = Validators.pattern(this.pattern); } ngOnChanges(changes: SimpleChanges): void {
if ('pattern' in changes) {
ngOnChanges(changes: SimpleChanges) {
if (changes['pattern']) {
this._createValidator(); this._createValidator();
if (this._onChange) this._onChange(); if (this._onChange) this._onChange();
} }
} }
validate(c: AbstractControl): {[key: string]: any} { validate(c: AbstractControl): {[key: string]: any} { return this._validator(c); }
return this.pattern ? this._validator(c) : null;
}
registerOnValidatorChange(fn: () => void) { this._onChange = fn; } registerOnValidatorChange(fn: () => void): void { this._onChange = fn; }
private _createValidator(): void { this._validator = Validators.pattern(this.pattern); }
} }

View File

@ -793,7 +793,7 @@ export function main() {
expect(form.valid).toEqual(true); expect(form.valid).toEqual(true);
})); }));
it('should support optional fields with pattern validator', fakeAsync(() => { it('should support optional fields with string pattern validator', fakeAsync(() => {
const fixture = TestBed.createComponent(NgModelMultipleValidators); const fixture = TestBed.createComponent(NgModelMultipleValidators);
fixture.componentInstance.required = false; fixture.componentInstance.required = false;
fixture.componentInstance.pattern = '[a-z]+'; fixture.componentInstance.pattern = '[a-z]+';
@ -815,6 +815,28 @@ export function main() {
expect(form.control.hasError('pattern', ['tovalidate'])).toBeTruthy(); expect(form.control.hasError('pattern', ['tovalidate'])).toBeTruthy();
})); }));
it('should support optional fields with RegExp pattern validator', fakeAsync(() => {
const fixture = TestBed.createComponent(NgModelMultipleValidators);
fixture.componentInstance.required = false;
fixture.componentInstance.pattern = /^[a-z]+$/;
fixture.detectChanges();
tick();
const form = fixture.debugElement.children[0].injector.get(NgForm);
const input = fixture.debugElement.query(By.css('input'));
input.nativeElement.value = '';
dispatchEvent(input.nativeElement, 'input');
fixture.detectChanges();
expect(form.valid).toBeTruthy();
input.nativeElement.value = '1';
dispatchEvent(input.nativeElement, 'input');
fixture.detectChanges();
expect(form.valid).toBeFalsy();
expect(form.control.hasError('pattern', ['tovalidate'])).toBeTruthy();
}));
it('should support optional fields with minlength validator', fakeAsync(() => { it('should support optional fields with minlength validator', fakeAsync(() => {
const fixture = TestBed.createComponent(NgModelMultipleValidators); const fixture = TestBed.createComponent(NgModelMultipleValidators);
fixture.componentInstance.required = false; fixture.componentInstance.required = false;
@ -1177,7 +1199,7 @@ class NgModelValidationBindings {
class NgModelMultipleValidators { class NgModelMultipleValidators {
required: boolean; required: boolean;
minLen: number; minLen: number;
pattern: string; pattern: string|RegExp;
} }
@Directive({ @Directive({