feat(forms): add minlength and maxlength validators

Closes #4705
This commit is contained in:
vsavkin
2015-10-13 14:38:13 -07:00
committed by Victor Savkin
parent 50e922f37b
commit e82a35d1fd
8 changed files with 141 additions and 17 deletions

View File

@ -28,7 +28,6 @@ import {
NgForm,
NgModel,
NgFormControl,
DefaultValidators,
NgControl,
DefaultValueAccessor,
CheckboxControlValueAccessor,

View File

@ -349,23 +349,43 @@ export function main() {
describe("validations", () => {
it("should use validators defined in html",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var form = new ControlGroup({"login": new Control("aa")});
var form = new ControlGroup(
{"login": new Control(""), "min": new Control(""), "max": new Control("")});
var t = `<div [ng-form-model]="form">
<input type="text" ng-control="login" required>
<input type="text" ng-control="min" minlength="3">
<input type="text" ng-control="max" maxlength="3">
</div>`;
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => {
rootTC.debugElement.componentInstance.form = form;
rootTC.detectChanges();
var required = rootTC.debugElement.query(By.css("[required]"));
var minLength = rootTC.debugElement.query(By.css("[minlength]"));
var maxLength = rootTC.debugElement.query(By.css("[maxlength]"));
required.nativeElement.value = "";
minLength.nativeElement.value = "1";
maxLength.nativeElement.value = "1234";
dispatchEvent(required.nativeElement, "change");
dispatchEvent(minLength.nativeElement, "change");
dispatchEvent(maxLength.nativeElement, "change");
expect(form.hasError("required", ["login"])).toEqual(true);
expect(form.hasError("minlength", ["min"])).toEqual(true);
expect(form.hasError("maxlength", ["max"])).toEqual(true);
required.nativeElement.value = "1";
minLength.nativeElement.value = "123";
maxLength.nativeElement.value = "123";
dispatchEvent(required.nativeElement, "change");
dispatchEvent(minLength.nativeElement, "change");
dispatchEvent(maxLength.nativeElement, "change");
expect(form.valid).toEqual(true);
var input = rootTC.debugElement.query(By.css("input"));
input.nativeElement.value = "";
dispatchEvent(input.nativeElement, "change");
expect(form.valid).toEqual(false);
async.done();
});
}));

View File

@ -32,6 +32,38 @@ export function main() {
() => { expect(Validators.required(new Control("not empty"))).toEqual(null); });
});
describe("minLength", () => {
it("should not error on an empty string",
() => { expect(Validators.minLength(2)(new Control(""))).toEqual(null); });
it("should not error on null",
() => { expect(Validators.minLength(2)(new Control(null))).toEqual(null); });
it("should not error on valid strings",
() => { expect(Validators.minLength(2)(new Control("aa"))).toEqual(null); });
it("should error on short strings", () => {
expect(Validators.minLength(2)(new Control("a")))
.toEqual({"minlength": {"requiredLength": 2, "actualLength": 1}});
});
});
describe("maxLength", () => {
it("should not error on an empty string",
() => { expect(Validators.maxLength(2)(new Control(""))).toEqual(null); });
it("should not error on null",
() => { expect(Validators.maxLength(2)(new Control(null))).toEqual(null); });
it("should not error on valid strings",
() => { expect(Validators.maxLength(2)(new Control("aa"))).toEqual(null); });
it("should error on short strings", () => {
expect(Validators.maxLength(2)(new Control("aaa")))
.toEqual({"maxlength": {"requiredLength": 2, "actualLength": 3}});
});
});
describe("compose", () => {
it("should return a null validator when given null",
() => { expect(Validators.compose(null)).toBe(Validators.nullValidator); });

View File

@ -396,7 +396,13 @@ var NG_API = [
'DebugElement.queryAll()',
'DecimalPipe',
'DecimalPipe.transform()',
'DefaultValidators',
'RequiredValidator',
'MinLengthValidator',
'MinLengthValidator.minLength',
'MinLengthValidator.minLength=',
'MaxLengthValidator',
'MaxLengthValidator.maxLength',
'MaxLengthValidator.maxLength=',
'DefaultValueAccessor',
'DefaultValueAccessor.onChange',
'DefaultValueAccessor.onChange=',
@ -995,6 +1001,8 @@ var NG_API = [
'Validators#group()',
'Validators#nullValidator()',
'Validators#required()',
'Validators#minLength()',
'Validators#maxLength()',
'Validators',
'View',
'View.directives',