Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forms): easier updating of validators on existing controls #9516

Merged
merged 1 commit into from
Jun 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions modules/@angular/forms/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
83 changes: 83 additions & 0 deletions modules/@angular/forms/test/model_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down