X Tutup
Skip to content
Closed
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
27 changes: 27 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ gulp.task('test.unit.js', ['build.js.dev'], function (done) {
);
});

gulp.task('watch.js.dev', ['build.js.dev'], function (done) {
watch('modules/**', ['!broccoli.js.dev']);
});

gulp.task('test.unit.js.sauce', ['build.js.dev'], function (done) {
var browserConf = getBrowsersFromCLI();
if (browserConf.isSauce) {
Expand Down Expand Up @@ -597,6 +601,29 @@ gulp.task('test.unit.dart', function (done) {
);
});

gulp.task('watch.dart.dev', function (done) {
runSequence(
'build/tree.dart',
'build/pure-packages.dart',
'!build/pubget.angular2.dart',
'!build/change_detect.dart',
'!build/remove-pub-symlinks',
'build.dart.material.css',
function(error) {
// if initial build failed (likely due to build or formatting step) then exit
// otherwise karma server doesn't start and we can't continue running properly
if (error) {
done(error);
return;
}

watch(['modules/angular2/**'], { ignoreInitial: true }, [
'!build/tree.dart'
]);
}
);
});

gulp.task('!test.unit.dart/karma-run', function (done) {
// run the run command in a new process to avoid duplicate logging by both server and runner from
// a single process
Expand Down
6 changes: 5 additions & 1 deletion modules/angular2/src/core/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export {
} from './forms/directives/select_control_value_accessor';
export {FORM_DIRECTIVES} from './forms/directives';
export {NG_VALIDATORS, Validators} from './forms/validators';
export {DefaultValidators} from './forms/directives/validators';
export {
RequiredValidator,
MinLengthValidator,
MaxLengthValidator
} from './forms/directives/validators';
export {FormBuilder} from './forms/form_builder';

import {FormBuilder} from './forms/form_builder';
Expand Down
8 changes: 5 additions & 3 deletions modules/angular2/src/core/forms/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
SelectControlValueAccessor,
NgSelectOption
} from './directives/select_control_value_accessor';
import {DefaultValidators} from './directives/validators';
import {RequiredValidator, MinLengthValidator, MaxLengthValidator} from './directives/validators';

export {NgControlName} from './directives/ng_control_name';
export {NgFormControl} from './directives/ng_form_control';
Expand All @@ -28,7 +28,7 @@ export {
SelectControlValueAccessor,
NgSelectOption
} from './directives/select_control_value_accessor';
export {DefaultValidators} from './directives/validators';
export {RequiredValidator, MinLengthValidator, MaxLengthValidator} from './directives/validators';
export {NgControlStatus} from './directives/ng_control_status';

/**
Expand Down Expand Up @@ -62,5 +62,7 @@ export const FORM_DIRECTIVES: Type[] = CONST_EXPR([
SelectControlValueAccessor,
NgControlStatus,

DefaultValidators
RequiredValidator,
MinLengthValidator,
MaxLengthValidator
]);
47 changes: 43 additions & 4 deletions modules/angular2/src/core/forms/directives/validators.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
import {forwardRef, Provider, OpaqueToken} from 'angular2/src/core/di';
import {CONST_EXPR} from 'angular2/src/core/facade/lang';
import {Directive} from 'angular2/src/core/metadata';
import {Attribute, Directive} from 'angular2/src/core/metadata';
import {Validators, NG_VALIDATORS} from '../validators';
import {NumberWrapper} from "angular2/src/core/facade/lang";

const DEFAULT_VALIDATORS =
const REQUIRED_VALIDATOR =
CONST_EXPR(new Provider(NG_VALIDATORS, {useValue: Validators.required, multi: true}));

@Directive({
selector: '[required][ng-control],[required][ng-form-control],[required][ng-model]',
bindings: [DEFAULT_VALIDATORS]
providers: [REQUIRED_VALIDATOR]
})
export class DefaultValidators {
export class RequiredValidator {
}

function createMinLengthValidator(dir): any {
return Validators.minLength(dir.minLength);
}
const MIN_LENGTH_VALIDATOR = CONST_EXPR(new Provider(NG_VALIDATORS, {
useFactory: createMinLengthValidator,
deps: [forwardRef(() => MinLengthValidator)],
multi: true
}));
@Directive({
selector: '[minlength][ng-control],[minlength][ng-form-control],[minlength][ng-model]',
providers: [MIN_LENGTH_VALIDATOR]
})
export class MinLengthValidator {
minLength: number;
constructor(@Attribute("minlength") minLength: string) {
this.minLength = NumberWrapper.parseInt(minLength, 10);
}
}

function createMaxLengthValidator(dir): any {
return Validators.maxLength(dir.maxLength);
}
const MAX_LENGTH_VALIDATOR = CONST_EXPR(new Provider(NG_VALIDATORS, {
useFactory: createMaxLengthValidator,
deps: [forwardRef(() => MaxLengthValidator)],
multi: true
}));
@Directive({
selector: '[maxlength][ng-control],[maxlength][ng-form-control],[maxlength][ng-model]',
providers: [MAX_LENGTH_VALIDATOR]
})
export class MaxLengthValidator {
maxLength: number;
constructor(@Attribute("maxlength") maxLength: string) {
this.maxLength = NumberWrapper.parseInt(maxLength, 10);
}
}
20 changes: 20 additions & 0 deletions modules/angular2/src/core/forms/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ export class Validators {
return isBlank(control.value) || control.value == "" ? {"required": true} : null;
}

static minLength(minLength: number): Function {
return (control: modelModule.Control): {[key: string]: any} => {
if (isPresent(Validators.required(control))) return null;
var v: string = control.value;
return v.length < minLength ?
{"minlength": {"requiredLength": minLength, "actualLength": v.length}} :
null;
};
}

static maxLength(maxLength: number): Function {
return (control: modelModule.Control): {[key: string]: any} => {
if (isPresent(Validators.required(control))) return null;
var v: string = control.value;
return v.length > maxLength ?
{"maxlength": {"requiredLength": maxLength, "actualLength": v.length}} :
null;
};
}

static nullValidator(c: any): {[key: string]: boolean} { return null; }

static compose(validators: Function[]): Function {
Expand Down
1 change: 0 additions & 1 deletion modules/angular2/test/core/forms/directives_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
NgForm,
NgModel,
NgFormControl,
DefaultValidators,
NgControl,
DefaultValueAccessor,
CheckboxControlValueAccessor,
Expand Down
32 changes: 26 additions & 6 deletions modules/angular2/test/core/forms/integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
expect(form.valid).toEqual(true);

var input = rootTC.debugElement.query(By.css("input"));
var required = rootTC.debugElement.query(By.css("[required]"));
var minLength = rootTC.debugElement.query(By.css("[minlength]"));
var maxLength = rootTC.debugElement.query(By.css("[maxlength]"));

input.nativeElement.value = "";
dispatchEvent(input.nativeElement, "change");
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);

expect(form.valid).toEqual(false);
async.done();
});
}));
Expand Down
32 changes: 32 additions & 0 deletions modules/angular2/test/core/forms/validators_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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); });
Expand Down
10 changes: 9 additions & 1 deletion modules/angular2/test/public_api_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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=',
Expand Down Expand Up @@ -995,6 +1001,8 @@ var NG_API = [
'Validators#group()',
'Validators#nullValidator()',
'Validators#required()',
'Validators#minLength()',
'Validators#maxLength()',
'Validators',
'View',
'View.directives',
Expand Down
X Tutup