Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 6ea1ac7

Browse files
committed
added $invalidWidget service
1 parent ee327a1 commit 6ea1ac7

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

scenario/widgets.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<tr><th colspan="3">Input text field</th></tr>
1616
<tr>
1717
<td>basic</td>
18-
<td><input type="text" name="text.basic" ng-required /></td>
18+
<td><input type="text" name="text.basic" ng-required ng-validate="number" ng-format="number"/></td>
1919
<td>text.basic={{text.basic}}</td>
2020
</tr>
2121
<tr>

src/Widgets.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ function valueAccessor(scope, element) {
2222
var validatorName = element.attr('ng-validate') || NOOP,
2323
validator = compileValidator(validatorName),
2424
required = element.attr('ng-required'),
25-
lastError;
25+
lastError,
26+
invalidWidgets = scope.$invalidWidgets || {markValid:noop, markInvalid:noop};
2627
required = required || required === '';
2728
if (!validator) throw "Validator named '" + validatorName + "' not found.";
2829
function validate(value) {
2930
var error = required && !trim(value) ? "Required" : validator({self:scope, scope:{get:scope.$get, set:scope.$set}}, value);
3031
if (error !== lastError) {
3132
elementError(element, NG_VALIDATION_ERROR, error);
3233
lastError = error;
34+
if (error)
35+
invalidWidgets.markInvalid(element);
36+
else
37+
invalidWidgets.markValid(element);
3338
}
3439
return value;
3540
}

src/services.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,18 @@ angularService("$hover", function(browser) {
8787
}
8888
});
8989
}, {inject:['$browser']});
90+
91+
angularService("$invalidWidgets", function(){
92+
var invalidWidgets = [];
93+
invalidWidgets.markValid = function(element){
94+
var index = indexOf(invalidWidgets, element);
95+
if (index != -1)
96+
invalidWidgets.splice(index, 1);
97+
};
98+
invalidWidgets.markInvalid = function(element){
99+
var index = indexOf(invalidWidgets, element);
100+
if (index === -1)
101+
invalidWidgets.push(element);
102+
};
103+
return invalidWidgets;
104+
});

test/servicesSpec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,23 @@ describe("services", function(){
5454
});
5555

5656
});
57+
58+
describe("service $invalidWidgets", function(){
59+
var scope;
60+
beforeEach(function(){
61+
scope = null;
62+
});
63+
afterEach(function(){
64+
if (scope && scope.$element)
65+
scope.$element.remove();
66+
});
67+
68+
it("should count number of invalid widgets", function(){
69+
var scope = compile('<input name="price" ng-required></input>').$init();
70+
expect(scope.$invalidWidgets.length).toEqual(1);
71+
scope.price = 123;
72+
scope.$eval();
73+
expect(scope.$invalidWidgets.length).toEqual(0);
74+
scope.$element.remove();
75+
});
76+
});

0 commit comments

Comments
 (0)