aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--scenario/widgets.html2
-rw-r--r--src/services.js15
-rw-r--r--src/widgets.js7
-rw-r--r--test/servicesSpec.js20
4 files changed, 42 insertions, 2 deletions
diff --git a/scenario/widgets.html b/scenario/widgets.html
index ab27e490..b83670b8 100644
--- a/scenario/widgets.html
+++ b/scenario/widgets.html
@@ -15,7 +15,7 @@
<tr><th colspan="3">Input text field</th></tr>
<tr>
<td>basic</td>
- <td><input type="text" name="text.basic" ng-required /></td>
+ <td><input type="text" name="text.basic" ng-required ng-validate="number" ng-format="number"/></td>
<td>text.basic={{text.basic}}</td>
</tr>
<tr>
diff --git a/src/services.js b/src/services.js
index 173cee98..9d60f795 100644
--- a/src/services.js
+++ b/src/services.js
@@ -87,3 +87,18 @@ angularService("$hover", function(browser) {
}
});
}, {inject:['$browser']});
+
+angularService("$invalidWidgets", function(){
+ var invalidWidgets = [];
+ invalidWidgets.markValid = function(element){
+ var index = indexOf(invalidWidgets, element);
+ if (index != -1)
+ invalidWidgets.splice(index, 1);
+ };
+ invalidWidgets.markInvalid = function(element){
+ var index = indexOf(invalidWidgets, element);
+ if (index === -1)
+ invalidWidgets.push(element);
+ };
+ return invalidWidgets;
+});
diff --git a/src/widgets.js b/src/widgets.js
index 0f781f2c..376518e9 100644
--- a/src/widgets.js
+++ b/src/widgets.js
@@ -22,7 +22,8 @@ function valueAccessor(scope, element) {
var validatorName = element.attr('ng-validate') || NOOP,
validator = compileValidator(validatorName),
required = element.attr('ng-required'),
- lastError;
+ lastError,
+ invalidWidgets = scope.$invalidWidgets || {markValid:noop, markInvalid:noop};
required = required || required === '';
if (!validator) throw "Validator named '" + validatorName + "' not found.";
function validate(value) {
@@ -30,6 +31,10 @@ function valueAccessor(scope, element) {
if (error !== lastError) {
elementError(element, NG_VALIDATION_ERROR, error);
lastError = error;
+ if (error)
+ invalidWidgets.markInvalid(element);
+ else
+ invalidWidgets.markValid(element);
}
return value;
}
diff --git a/test/servicesSpec.js b/test/servicesSpec.js
index 43511853..b7dfe4c8 100644
--- a/test/servicesSpec.js
+++ b/test/servicesSpec.js
@@ -54,3 +54,23 @@ describe("services", function(){
});
});
+
+describe("service $invalidWidgets", function(){
+ var scope;
+ beforeEach(function(){
+ scope = null;
+ });
+ afterEach(function(){
+ if (scope && scope.$element)
+ scope.$element.remove();
+ });
+
+ it("should count number of invalid widgets", function(){
+ var scope = compile('<input name="price" ng-required></input>').$init();
+ expect(scope.$invalidWidgets.length).toEqual(1);
+ scope.price = 123;
+ scope.$eval();
+ expect(scope.$invalidWidgets.length).toEqual(0);
+ scope.$element.remove();
+ });
+});