Skip to content

Commit 5c6630b

Browse files
committed
Validate checkboxes on change
Fix for #377
1 parent 141101e commit 5c6630b

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

src/directives/array.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
1919
link: function(scope, element, attrs, ngModel) {
2020
var formDefCache = {};
2121

22+
scope.validateArray = angular.noop;
2223

2324
if (ngModel) {
2425
// We need the ngModelController on several places,
@@ -41,9 +42,8 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
4142
// We only modify the same array instance but someone might change the array from
4243
// the outside so let's watch for that. We use an ordinary watch since the only case
4344
// we're really interested in is if its a new instance.
44-
scope.$watch('model' + sfPath.normalize(form.key), function() {
45-
list = sfSelect(form.key, scope.model);
46-
scope.modelArray = list;
45+
scope.$watch('model' + sfPath.normalize(form.key), function(value) {
46+
scope.modelArray = value;
4747
});
4848

4949
// Since ng-model happily creates objects in a deep path when setting a
@@ -126,19 +126,15 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
126126
}
127127

128128
// Trigger validation.
129-
if (scope.validateArray) {
130-
scope.validateArray();
131-
}
129+
scope.validateArray();
132130
return list;
133131
};
134132

135133
scope.deleteFromArray = function(index) {
136134
list.splice(index, 1);
137135

138136
// Trigger validation.
139-
if (scope.validateArray) {
140-
scope.validateArray();
141-
}
137+
scope.validateArray();
142138

143139
// Angular 1.2 lacks setDirty
144140
if (ngModel && ngModel.$setDirty) {
@@ -172,29 +168,29 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
172168
form.titleMap.forEach(function(item) {
173169
scope.titleMapValues.push(arr.indexOf(item.value) !== -1);
174170
});
175-
176171
};
177172
//Catch default values
178173
updateTitleMapValues(scope.modelArray);
179174
scope.$watchCollection('modelArray', updateTitleMapValues);
180175

181176
//To get two way binding we also watch our titleMapValues
182-
scope.$watchCollection('titleMapValues', function(vals) {
183-
if (vals) {
177+
scope.$watchCollection('titleMapValues', function(vals, old) {
178+
if (vals && vals !== old) {
184179
var arr = scope.modelArray;
185180

186181
// Apparently the fastest way to clear an array, readable too.
187182
// http://jsperf.com/array-destroy/32
188183
while (arr.length > 0) {
189184
arr.pop();
190185
}
191-
192186
form.titleMap.forEach(function(item, index) {
193187
if (vals[index]) {
194188
arr.push(item.value);
195189
}
196190
});
197191

192+
// Time to validate the rebuilt array.
193+
scope.validateArray();
198194
}
199195
});
200196
}

src/directives/decorators/bootstrap/checkboxes.html

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
sf-changed="form"
1010
class="{{form.fieldHtmlClass}}"
1111
ng-model="titleMapValues[$index]"
12-
schema-vaidate="form"
1312
name="{{form.key.slice(-1)[0]}}">
1413
<span ng-bind-html="form.titleMap[$index].name"></span>
1514
</label>

src/directives/schema-validate.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse',
2-
function(sfValidator, $parse) {
1+
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse', 'sfSelect',
2+
function(sfValidator, $parse, sfSelect) {
33

44
return {
55
restrict: 'A',

0 commit comments

Comments
 (0)