Skip to content

Commit 4370d75

Browse files
committed
refactor(directive.ngModel): rename emitValidity -> setValidity
1 parent 4e83399 commit 4370d75

File tree

6 files changed

+52
-51
lines changed

6 files changed

+52
-51
lines changed

docs/content/guide/dev_guide.forms.ngdoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ This example shows how to implement a custom HTML editor widget in Angular.
297297
ctrl.formatters.push(function(value) {
298298
try {
299299
value = $sanitize(value || '');
300-
ctrl.emitValidity('HTML', true);
300+
ctrl.setValidity('HTML', true);
301301
} catch (e) {
302-
ctrl.emitValidity('HTML', false);
302+
ctrl.setValidity('HTML', false);
303303
}
304304

305305
});

src/widget/form.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
* - values are arrays of widgets that are invalid with given error.
1717
*
1818
* @description
19-
* Form is a controller that keeps track of all registered widgets.
20-
*
19+
* `FormController` keeps track of all its widgets as well as state of them form, such as being valid/invalid or dirty/pristine.
20+
*
2121
* Each {@link angular.module.ng.$compileProvider.directive.form form} directive creates an instance
2222
* of `FormController`.
2323
*
@@ -123,7 +123,7 @@ FormController.prototype.registerWidget = function(widget, alias) {
123123
* @scope
124124
* @description
125125
* Directive that instantiates
126-
* {@link angular.module.ng.$compileProvider.directive.form.FormController Form} controller.
126+
* {@link angular.module.ng.$compileProvider.directive.form.FormController FormController}.
127127
*
128128
* If `name` attribute is specified, the controller is published to the scope as well.
129129
*

src/widget/input.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,10 @@ function textInputType(scope, element, attr, ctrl) {
391391

392392
var emit = function(regexp, value) {
393393
if (isEmpty(value) || regexp.test(value)) {
394-
ctrl.emitValidity('PATTERN', true);
394+
ctrl.setValidity('PATTERN', true);
395395
return value;
396396
} else {
397-
ctrl.emitValidity('PATTERN', false);
397+
ctrl.setValidity('PATTERN', false);
398398
return undefined;
399399
}
400400
};
@@ -425,10 +425,10 @@ function textInputType(scope, element, attr, ctrl) {
425425
var minlength = parseInt(attr.ngMinlength, 10);
426426
var minLengthValidator = function(value) {
427427
if (!isEmpty(value) && value.length < minlength) {
428-
ctrl.emitValidity('MINLENGTH', false);
428+
ctrl.setValidity('MINLENGTH', false);
429429
return undefined;
430430
} else {
431-
ctrl.emitValidity('MINLENGTH', true);
431+
ctrl.setValidity('MINLENGTH', true);
432432
return value;
433433
}
434434
};
@@ -442,10 +442,10 @@ function textInputType(scope, element, attr, ctrl) {
442442
var maxlength = parseInt(attr.ngMaxlength, 10);
443443
var maxLengthValidator = function(value) {
444444
if (!isEmpty(value) && value.length > maxlength) {
445-
ctrl.emitValidity('MAXLENGTH', false);
445+
ctrl.setValidity('MAXLENGTH', false);
446446
return undefined;
447447
} else {
448-
ctrl.emitValidity('MAXLENGTH', true);
448+
ctrl.setValidity('MAXLENGTH', true);
449449
return value;
450450
}
451451
};
@@ -461,10 +461,10 @@ function numberInputType(scope, element, attr, ctrl) {
461461
ctrl.parsers.push(function(value) {
462462
var empty = isEmpty(value);
463463
if (empty || NUMBER_REGEXP.test(value)) {
464-
ctrl.emitValidity('NUMBER', true);
464+
ctrl.setValidity('NUMBER', true);
465465
return value === '' ? null : (empty ? value : parseFloat(value));
466466
} else {
467-
ctrl.emitValidity('NUMBER', false);
467+
ctrl.setValidity('NUMBER', false);
468468
return undefined;
469469
}
470470
});
@@ -477,10 +477,10 @@ function numberInputType(scope, element, attr, ctrl) {
477477
var min = parseFloat(attr.min);
478478
var minValidator = function(value) {
479479
if (!isEmpty(value) && value < min) {
480-
ctrl.emitValidity('MIN', false);
480+
ctrl.setValidity('MIN', false);
481481
return undefined;
482482
} else {
483-
ctrl.emitValidity('MIN', true);
483+
ctrl.setValidity('MIN', true);
484484
return value;
485485
}
486486
};
@@ -493,10 +493,10 @@ function numberInputType(scope, element, attr, ctrl) {
493493
var max = parseFloat(attr.max);
494494
var maxValidator = function(value) {
495495
if (!isEmpty(value) && value > max) {
496-
ctrl.emitValidity('MAX', false);
496+
ctrl.setValidity('MAX', false);
497497
return undefined;
498498
} else {
499-
ctrl.emitValidity('MAX', true);
499+
ctrl.setValidity('MAX', true);
500500
return value;
501501
}
502502
};
@@ -508,10 +508,10 @@ function numberInputType(scope, element, attr, ctrl) {
508508
ctrl.formatters.push(function(value) {
509509

510510
if (isEmpty(value) || isNumber(value)) {
511-
ctrl.emitValidity('NUMBER', true);
511+
ctrl.setValidity('NUMBER', true);
512512
return value;
513513
} else {
514-
ctrl.emitValidity('NUMBER', false);
514+
ctrl.setValidity('NUMBER', false);
515515
return undefined;
516516
}
517517
});
@@ -522,10 +522,10 @@ function urlInputType(scope, element, attr, ctrl) {
522522

523523
var urlValidator = function(value) {
524524
if (isEmpty(value) || URL_REGEXP.test(value)) {
525-
ctrl.emitValidity('URL', true);
525+
ctrl.setValidity('URL', true);
526526
return value;
527527
} else {
528-
ctrl.emitValidity('URL', false);
528+
ctrl.setValidity('URL', false);
529529
return undefined;
530530
}
531531
};
@@ -539,10 +539,10 @@ function emailInputType(scope, element, attr, ctrl) {
539539

540540
var emailValidator = function(value) {
541541
if (isEmpty(value) || EMAIL_REGEXP.test(value)) {
542-
ctrl.emitValidity('EMAIL', true);
542+
ctrl.setValidity('EMAIL', true);
543543
return value;
544544
} else {
545-
ctrl.emitValidity('EMAIL', false);
545+
ctrl.setValidity('EMAIL', false);
546546
return undefined;
547547
}
548548
};
@@ -794,7 +794,7 @@ var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',
794794

795795
/**
796796
* @ngdoc function
797-
* @name angular.module.ng.$compileProvider.directive.ng:model.NgModelController#emitValidity
797+
* @name angular.module.ng.$compileProvider.directive.ng:model.NgModelController#setValidity
798798
* @methodOf angular.module.ng.$compileProvider.directive.ng:model.NgModelController
799799
*
800800
* @description
@@ -806,7 +806,7 @@ var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',
806806
* @param {string} name Name of the validator.
807807
* @param {boolean} isValid Whether it should $emit `$valid` (true) or `$invalid` (false) event.
808808
*/
809-
this.emitValidity = function(name, isValid) {
809+
this.setValidity = function(name, isValid) {
810810

811811
if (!isValid && this.error[name]) return;
812812
if (isValid && !this.error[name]) return;
@@ -890,7 +890,8 @@ var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',
890890
* @element input
891891
*
892892
* @description
893-
* Is directive that tells Angular to do two-way data binding. It works together with `input`, `select`, `textarea`. You can easily write your own directives to use `ng:model` pretty easily.
893+
* Is directive that tells Angular to do two-way data binding. It works together with `input`,
894+
* `select`, `textarea`. You can easily write your own directives to use `ng:model` as well.
894895
*
895896
* `ng:model` is responsible for:
896897
*
@@ -901,7 +902,7 @@ var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',
901902
* - setting related css class onto the element (`ng-valid`, `ng-invalid`, `ng-dirty`, `ng-pristine`),
902903
* - register the widget with parent {@link angular.module.ng.$compileProvider.directive.form form}.
903904
*
904-
* For examples, how to use `ng:model`, see:
905+
* For basic examples, how to use `ng:model`, see:
905906
*
906907
* - {@link angular.module.ng.$compileProvider.directive.input input}
907908
* - {@link angular.module.ng.$compileProvider.directive.input.text text}
@@ -1080,10 +1081,10 @@ var requiredDirective = [function() {
10801081

10811082
var validator = function(value) {
10821083
if (attr.required && isEmpty(value)) {
1083-
ctrl.emitValidity('REQUIRED', false);
1084+
ctrl.setValidity('REQUIRED', false);
10841085
return null;
10851086
} else {
1086-
ctrl.emitValidity('REQUIRED', true);
1087+
ctrl.setValidity('REQUIRED', true);
10871088
return value;
10881089
}
10891090
};

src/widget/select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
139139
// required validator
140140
if (multiple && (attr.required || attr.ngRequired)) {
141141
var requiredValidator = function(value) {
142-
ctrl.emitValidity('REQUIRED', !attr.required || (value && value.length));
142+
ctrl.setValidity('REQUIRED', !attr.required || (value && value.length));
143143
return value;
144144
};
145145

test/widget/formSpec.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('form', function() {
3838
'</form>')(scope);
3939

4040
var form = scope.form;
41-
widget.emitValidity('REQUIRED', false);
41+
widget.setValidity('REQUIRED', false);
4242
expect(form.alias).toBe(widget);
4343
expect(form.error.REQUIRED).toEqual([widget]);
4444

@@ -107,11 +107,11 @@ describe('form', function() {
107107
var child = scope.child;
108108
var input = child.text;
109109

110-
input.emitValidity('MyError', false);
110+
input.setValidity('MyError', false);
111111
expect(parent.error.MyError).toEqual([input]);
112112
expect(child.error.MyError).toEqual([input]);
113113

114-
input.emitValidity('MyError', true);
114+
input.setValidity('MyError', true);
115115
expect(parent.error.MyError).toBeUndefined();
116116
expect(child.error.MyError).toBeUndefined();
117117
});
@@ -138,12 +138,12 @@ describe('form', function() {
138138
expect(child).toBeDefined();
139139
expect(input).toBeDefined();
140140

141-
input.emitValidity('myRule', false);
141+
input.setValidity('myRule', false);
142142
expect(input.error.myRule).toEqual(true);
143143
expect(child.error.myRule).toEqual([input]);
144144
expect(parent.error.myRule).toEqual([input]);
145145

146-
input.emitValidity('myRule', true);
146+
input.setValidity('myRule', true);
147147
expect(parent.error.myRule).toBeUndefined();
148148
expect(child.error.myRule).toBeUndefined();
149149
});
@@ -177,18 +177,18 @@ describe('form', function() {
177177
it('should have ng-valid/ng-invalid css class', function() {
178178
expect(doc).toBeValid();
179179

180-
widget.emitValidity('ERROR', false);
180+
widget.setValidity('ERROR', false);
181181
scope.$apply();
182182
expect(doc).toBeInvalid();
183183

184-
widget.emitValidity('ANOTHER', false);
184+
widget.setValidity('ANOTHER', false);
185185
scope.$apply();
186186

187-
widget.emitValidity('ERROR', true);
187+
widget.setValidity('ERROR', true);
188188
scope.$apply();
189189
expect(doc).toBeInvalid();
190190

191-
widget.emitValidity('ANOTHER', true);
191+
widget.setValidity('ANOTHER', true);
192192
scope.$apply();
193193
expect(doc).toBeValid();
194194
});

test/widget/inputSpec.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,44 +49,44 @@ describe('NgModelController', function() {
4949
});
5050

5151

52-
describe('emitValidity', function() {
52+
describe('setValidity', function() {
5353

5454
it('should emit $invalid only when $valid', function() {
5555
var spy = jasmine.createSpy('$invalid');
5656
scope.$on('$invalid', spy);
5757

58-
ctrl.emitValidity('ERROR', false);
58+
ctrl.setValidity('ERROR', false);
5959
expect(spy).toHaveBeenCalledOnce();
6060

6161
spy.reset();
62-
ctrl.emitValidity('ERROR', false);
62+
ctrl.setValidity('ERROR', false);
6363
expect(spy).not.toHaveBeenCalled();
6464
});
6565

6666

6767
it('should set and unset the error', function() {
68-
ctrl.emitValidity('REQUIRED', false);
68+
ctrl.setValidity('REQUIRED', false);
6969
expect(ctrl.error.REQUIRED).toBe(true);
7070

71-
ctrl.emitValidity('REQUIRED', true);
71+
ctrl.setValidity('REQUIRED', true);
7272
expect(ctrl.error.REQUIRED).toBeUndefined();
7373
});
7474

7575

7676
it('should set valid/invalid', function() {
77-
ctrl.emitValidity('FIRST', false);
77+
ctrl.setValidity('FIRST', false);
7878
expect(ctrl.valid).toBe(false);
7979
expect(ctrl.invalid).toBe(true);
8080

81-
ctrl.emitValidity('SECOND', false);
81+
ctrl.setValidity('SECOND', false);
8282
expect(ctrl.valid).toBe(false);
8383
expect(ctrl.invalid).toBe(true);
8484

85-
ctrl.emitValidity('SECOND', true);
85+
ctrl.setValidity('SECOND', true);
8686
expect(ctrl.valid).toBe(false);
8787
expect(ctrl.invalid).toBe(true);
8888

89-
ctrl.emitValidity('FIRST', true);
89+
ctrl.setValidity('FIRST', true);
9090
expect(ctrl.valid).toBe(true);
9191
expect(ctrl.invalid).toBe(false);
9292
});
@@ -96,11 +96,11 @@ describe('NgModelController', function() {
9696
var spy = jasmine.createSpy('$valid');
9797
scope.$on('$valid', spy);
9898

99-
ctrl.emitValidity('ERROR', true);
99+
ctrl.setValidity('ERROR', true);
100100
expect(spy).not.toHaveBeenCalled();
101101

102-
ctrl.emitValidity('ERROR', false);
103-
ctrl.emitValidity('ERROR', true);
102+
ctrl.setValidity('ERROR', false);
103+
ctrl.setValidity('ERROR', true);
104104
expect(spy).toHaveBeenCalledOnce();
105105
});
106106
});

0 commit comments

Comments
 (0)