Skip to content

Commit

Permalink
fix(forms): Validators.required properly validate arrays (angular#13362)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzmitry Shylovich authored and IgorMinar committed Jan 5, 2017
1 parent 2dd6280 commit 9898d8f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions modules/@angular/forms/src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import {isPresent} from './facade/lang';
import {AbstractControl} from './model';
import {isPromise} from './private_import_core';

function isEmptyInputValue(value: any) {
return value == null || typeof value === 'string' && value.length === 0;
function isEmptyInputValue(value: any): boolean {
// we don't check for string here so it also works with arrays
return value == null || value.length === 0;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions modules/@angular/forms/test/validators_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ export function main() {

it('should accept zero as valid',
() => { expect(Validators.required(new FormControl(0))).toBeNull(); });

it('should error on an empty array',
() => expect(Validators.required(new FormControl([]))).toEqual({'required': true}));

it('should not error on a non-empty array',
() => expect(Validators.required(new FormControl([1, 2]))).toBeNull());
});

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

0 comments on commit 9898d8f

Please sign in to comment.