Skip to content

Commit

Permalink
fix(forms): fix Validators.min/maxLength with FormArray (#13095)
Browse files Browse the repository at this point in the history
Fixes #13089
  • Loading branch information
Dzmitry Shylovich authored and vicb committed Dec 15, 2016
1 parent 65c9b5b commit 7383e4a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
4 changes: 0 additions & 4 deletions modules/@angular/forms/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ export const PENDING = 'PENDING';
*/
export const DISABLED = 'DISABLED';

export function isControl(control: Object): boolean {
return control instanceof AbstractControl;
}

function _find(control: AbstractControl, path: Array<string|number>| string, delimiter: string) {
if (path == null) return null;

Expand Down
4 changes: 2 additions & 2 deletions modules/@angular/forms/src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class Validators {
if (isEmptyInputValue(control.value)) {
return null; // don't validate empty values to allow optional controls
}
const length = typeof control.value === 'string' ? control.value.length : 0;
const length: number = control.value ? control.value.length : 0;
return length < minLength ?
{'minlength': {'requiredLength': minLength, 'actualLength': length}} :
null;
Expand All @@ -84,7 +84,7 @@ export class Validators {
*/
static maxLength(maxLength: number): ValidatorFn {
return (control: AbstractControl): {[key: string]: any} => {
const length = typeof control.value === 'string' ? control.value.length : 0;
const length: number = control.value ? control.value.length : 0;
return length > maxLength ?
{'maxlength': {'requiredLength': maxLength, 'actualLength': length}} :
null;
Expand Down
26 changes: 25 additions & 1 deletion modules/@angular/forms/test/validators_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {fakeAsync, tick} from '@angular/core/testing';
import {describe, expect, it} from '@angular/core/testing/testing_internal';
import {AbstractControl, FormControl, Validators} from '@angular/forms';
import {AbstractControl, FormArray, FormControl, Validators} from '@angular/forms';
import {Observable} from 'rxjs/Observable';

import {normalizeAsyncValidator} from '../src/directives/normalize_validator';
Expand Down Expand Up @@ -68,6 +68,18 @@ export function main() {
'minlength': {'requiredLength': 2, 'actualLength': 1}
});
});

it('should not error when FormArray has valid length', () => {
const fa = new FormArray([new FormControl(''), new FormControl('')]);
expect(Validators.minLength(2)(fa)).toBeNull();
});

it('should error when FormArray has invalid length', () => {
const fa = new FormArray([new FormControl('')]);
expect(Validators.minLength(2)(fa)).toEqual({
'minlength': {'requiredLength': 2, 'actualLength': 1}
});
});
});

describe('maxLength', () => {
Expand All @@ -85,6 +97,18 @@ export function main() {
'maxlength': {'requiredLength': 2, 'actualLength': 3}
});
});

it('should not error when FormArray has valid length', () => {
const fa = new FormArray([new FormControl(''), new FormControl('')]);
expect(Validators.maxLength(2)(fa)).toBeNull();
});

it('should error when FormArray has invalid length', () => {
const fa = new FormArray([new FormControl(''), new FormControl('')]);
expect(Validators.maxLength(1)(fa)).toEqual({
'maxlength': {'requiredLength': 1, 'actualLength': 2}
});
});
});

describe('pattern', () => {
Expand Down

0 comments on commit 7383e4a

Please sign in to comment.