Skip to content

Commit f9bb86c

Browse files
committed
Trigger validation when array size changes
This at least validates max and minItems, but we should really initialize the array to minItems and disable remove and add buttons when max and min is met.
1 parent 1c51997 commit f9bb86c

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/directives/array.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,20 @@ function(sfSelect, schemaForm, sfValidator) {
8686
}
8787
list.push(dflt);
8888
}
89+
90+
// Trigger validation.
91+
if (scope.validateArray) {
92+
scope.validateArray();
93+
}
8994
};
9095

9196
scope.deleteFromArray = function(index) {
9297
list.splice(index,1);
98+
99+
// Trigger validation.
100+
if (scope.validateArray) {
101+
scope.validateArray();
102+
}
93103
};
94104

95105
// Always start with one empty form unless configured otherwise.
@@ -149,8 +159,7 @@ function(sfSelect, schemaForm, sfValidator) {
149159
if (ngModel) {
150160
var error;
151161

152-
// Listen to an event so we can validate the input on request
153-
scope.$on('schemaFormValidate',function(payload) {
162+
scope.validateArray = function() {
154163
// The actual content of the array is validated by each field
155164
// so we settle for checking validations specific to arrays
156165

@@ -161,12 +170,11 @@ function(sfSelect, schemaForm, sfValidator) {
161170
form,
162171
scope.modelArray.length > 0 ? scope.modelArray : undefined
163172
);
164-
console.log(result.error)
165173
if (result.valid === false &&
166174
result.error &&
167175
(result.error.dataPath === '' ||
168176
result.error.dataPath === '/'+form.key[form.key.length - 1])) {
169-
console.log('setting invlid')
177+
170178
// Set viewValue to trigger $dirty on field. If someone knows a
171179
// a better way to do it please tell.
172180
ngModel.$setViewValue(scope.modelArray);
@@ -176,8 +184,9 @@ function(sfSelect, schemaForm, sfValidator) {
176184
} else {
177185
ngModel.$setValidity('schema', true);
178186
}
187+
};
179188

180-
});
189+
scope.$on('schemaFormValidate',scope.validateArray);
181190

182191

183192
scope.hasSuccess = function(){

0 commit comments

Comments
 (0)