feat(forms): support updating of validators on exiting controls (#9516)

lint

fix

async

d

test

test
This commit is contained in:
Torgeir Helgevold 2016-06-23 11:18:07 -04:00 committed by Victor Berchet
parent 098b461b69
commit 638fd744aa
2 changed files with 95 additions and 0 deletions

View File

@ -100,6 +100,18 @@ export abstract class AbstractControl {
get pending(): boolean { return this._status == PENDING; }
setAsyncValidators(newValidator: AsyncValidatorFn|AsyncValidatorFn[]): void {
this.asyncValidator = coerceToAsyncValidator(newValidator);
}
clearAsyncValidators(): void { this.asyncValidator = null; }
setValidators(newValidator: ValidatorFn|ValidatorFn[]): void {
this.validator = coerceToValidator(newValidator);
}
clearValidators(): void { this.validator = null; }
markAsTouched(): void { this._touched = true; }
markAsDirty({onlySelf}: {onlySelf?: boolean} = {}): void {

View File

@ -65,6 +65,57 @@ export function main() {
var c = new FormControl(null, Validators.required);
expect(c.errors).toEqual({'required': true});
});
it('should set single validator', () => {
var c = new FormControl(null);
expect(c.valid).toEqual(true);
c.setValidators(Validators.required);
c.updateValue(null);
expect(c.valid).toEqual(false);
c.updateValue('abc');
expect(c.valid).toEqual(true);
});
it('should set multiple validators from array', () => {
var c = new FormControl('');
expect(c.valid).toEqual(true);
c.setValidators([Validators.minLength(5), Validators.required]);
c.updateValue('');
expect(c.valid).toEqual(false);
c.updateValue('abc');
expect(c.valid).toEqual(false);
c.updateValue('abcde');
expect(c.valid).toEqual(true);
});
it('should clear validators', () => {
var c = new FormControl('', Validators.required);
expect(c.valid).toEqual(false);
c.clearValidators();
expect(c.validator).toEqual(null);
c.updateValue('');
expect(c.valid).toEqual(true);
});
it('should add after clearing', () => {
var c = new FormControl('', Validators.required);
expect(c.valid).toEqual(false);
c.clearValidators();
expect(c.validator).toEqual(null);
c.setValidators([Validators.required]);
expect(c.validator).not.toBe(null);
});
});
describe('asyncValidator', () => {
@ -135,6 +186,38 @@ export function main() {
expect(c.errors).toEqual({'async': true, 'other': true});
}));
it('should add single async validator', fakeAsync(() => {
var c = new FormControl('value', null);
c.setAsyncValidators(asyncValidator('expected'));
expect(c.asyncValidator).not.toEqual(null);
c.updateValue('expected');
tick();
expect(c.valid).toEqual(true);
}));
it('should add async validator from array', fakeAsync(() => {
var c = new FormControl('value', null);
c.setAsyncValidators([asyncValidator('expected')]);
expect(c.asyncValidator).not.toEqual(null);
c.updateValue('expected');
tick();
expect(c.valid).toEqual(true);
}));
it('should clear async validators', fakeAsync(() => {
var c = new FormControl('value', [asyncValidator('expected'), otherAsyncValidator]);
c.clearValidators();
expect(c.asyncValidator).toEqual(null);
}));
});
describe('dirty', () => {