Skip to content

Commit 2e75bf7

Browse files
committed
Let schema-validate export a valdateField function
So that arrays and others can trigger it when needed.
1 parent 71712a2 commit 2e75bf7

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/directives/schema-validate.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
3939
if (!form) {
4040
return viewValue;
4141
}
42-
42+
4343
// Omit TV4 validation
4444
if (scope.options && scope.options.tv4Validation === false) {
4545
return viewValue;
@@ -103,12 +103,23 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
103103
if (ngModel.$validators) {
104104
ngModel.$validators.schemaForm = function() {
105105
// Any error and we're out of here!
106-
return !Object.keys(ngModel.$error).some(function(e) { return e !== 'schemaForm'});
107-
}
106+
return !Object.keys(ngModel.$error).some(function(e) { return e !== 'schemaForm';});
107+
};
108108
}
109109

110-
// Listen to an event so we can validate the input on request
111-
scope.$on('schemaFormValidate', function() {
110+
var schema = form.schema;
111+
112+
// A bit ugly but useful.
113+
scope.validateField = function() {
114+
115+
// Special case: arrays
116+
// TODO: Can this be generalized in a way that works consistently?
117+
// Just setting the viewValue isn't enough to trigger validation
118+
// since it's the same value. This will be better when we drop
119+
// 1.2 support.
120+
if (schema && schema.type.indexOf('array') !== -1) {
121+
validate(ngModel.$modelValue);
122+
}
112123

113124
// We set the viewValue to trigger parsers,
114125
// since modelValue might be empty and validating just that
@@ -132,8 +143,10 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse
132143
// hence required works.
133144
ngModel.$setViewValue(ngModel.$viewValue);
134145
}
146+
}
135147

136-
});
148+
// Listen to an event so we can validate the input on request
149+
scope.$on('schemaFormValidate', scope.validateField);
137150

138151
scope.schemaError = function() {
139152
return error;

0 commit comments

Comments
 (0)