feat(forms): compose validator fns automatically if arrays

This commit is contained in:
Kara Erickson 2016-06-13 11:27:04 -07:00
parent 14a3ade662
commit 61960c51a3
3 changed files with 36 additions and 4 deletions

View File

@ -69,8 +69,9 @@ export class FormBuilder {
/**
* Construct a new {@link FormControl} with the given `value`,`validator`, and `asyncValidator`.
*/
control(value: Object, validator: ValidatorFn = null, asyncValidator: AsyncValidatorFn = null):
modelModule.FormControl {
control(
value: Object, validator: ValidatorFn|ValidatorFn[] = null,
asyncValidator: AsyncValidatorFn|AsyncValidatorFn[] = null): modelModule.FormControl {
return new modelModule.FormControl(value, validator, asyncValidator);
}

View File

@ -3,9 +3,11 @@ import {ListWrapper, StringMapWrapper} from '../facade/collection';
import {isBlank, isPresent, normalizeBool} from '../facade/lang';
import {PromiseWrapper} from '../facade/promise';
import {composeAsyncValidators, composeValidators} from './directives/shared';
import {AsyncValidatorFn, ValidatorFn} from './directives/validators';
/**
* Indicates that a FormControl is valid, i.e. that no errors exist in the input value.
*/
@ -50,6 +52,15 @@ function toObservable(r: any): Observable<any> {
return PromiseWrapper.isPromise(r) ? ObservableWrapper.fromPromise(r) : r;
}
function coerceToValidator(validator: ValidatorFn | ValidatorFn[]): ValidatorFn {
return Array.isArray(validator) ? composeValidators(validator) : validator;
}
function coerceToAsyncValidator(asyncValidator: AsyncValidatorFn | AsyncValidatorFn[]):
AsyncValidatorFn {
return Array.isArray(asyncValidator) ? composeAsyncValidators(asyncValidator) : asyncValidator;
}
/**
* @experimental
*/
@ -275,8 +286,9 @@ export class FormControl extends AbstractControl {
_onChange: Function;
constructor(
value: any = null, validator: ValidatorFn = null, asyncValidator: AsyncValidatorFn = null) {
super(validator, asyncValidator);
value: any = null, validator: ValidatorFn|ValidatorFn[] = null,
asyncValidator: AsyncValidatorFn|AsyncValidatorFn[] = null) {
super(coerceToValidator(validator), coerceToAsyncValidator(asyncValidator));
this._value = value;
this.updateValueAndValidity({onlySelf: true, emitEvent: false});
this._initObservables();

View File

@ -31,6 +31,8 @@ export function main() {
return e;
}
function otherAsyncValidator() { return PromiseWrapper.resolve({'other': true}); }
describe('Form Model', () => {
describe('FormControl', () => {
it('should default the value to null', () => {
@ -50,6 +52,15 @@ export function main() {
expect(c.valid).toEqual(false);
});
it('should support arrays of validator functions if passed', () => {
const c = new FormControl('value', [Validators.required, Validators.minLength(3)]);
c.updateValue('a');
expect(c.valid).toEqual(false);
c.updateValue('aaa');
expect(c.valid).toEqual(true);
});
it('should return errors', () => {
var c = new FormControl(null, Validators.required);
expect(c.errors).toEqual({'required': true});
@ -116,6 +127,14 @@ export function main() {
expect(c.valid).toEqual(true);
}));
it('should support arrays of async validator functions if passed', fakeAsync(() => {
const c =
new FormControl('value', null, [asyncValidator('expected'), otherAsyncValidator]);
tick();
expect(c.errors).toEqual({'async': true, 'other': true});
}));
});
describe('dirty', () => {