Skip to content

Commit 3809da8

Browse files
committed
Reverted issue #135
1 parent ed9cf6e commit 3809da8

File tree

1 file changed

+41
-51
lines changed

1 file changed

+41
-51
lines changed

client/src/js/ng-django-forms.js

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -137,63 +137,53 @@ djng_forms_module.directive('ngModel', function() {
137137
});
138138

139139

140+
// This directive is added automatically by django-angular for widgets of type RadioSelect and
141+
// CheckboxSelectMultiple. This is necessary to adjust the behavior of a collection of input fields,
142+
// which forms a group for one `django.forms.Field`.
140143
djng_forms_module.directive('validateMultipleFields', function() {
141144
return {
142145
restrict: 'A',
143146
require: '^?form',
144-
// create child scope for changed method
145-
scope: true,
146-
compile: function(element, attrs) {
147-
angular.forEach(element.find('input'), function(elem) {
148-
elem = angular.element(elem)
149-
elem.attr('ng-change', 'changed()');
150-
});
151-
152-
return {
153-
154-
post: function(scope, element, attrs, controller) {
155-
var formCtrl, subFields, checkboxCtrls = [];
156-
157-
scope.changed = function() {
158-
validate(true)
159-
}
160-
161-
function validate(trigger) {
162-
var valid = false;
163-
angular.forEach(checkboxCtrls, function(checkbox) {
164-
valid = valid || checkbox.$modelValue;
165-
if(checkbox.clearRejected) {
166-
checkbox.clearRejected();
167-
}
168-
});
169-
170-
formCtrl.$setValidity('required', valid);
171-
formCtrl.$setValidity('rejected', true);
172-
formCtrl.$message = ''
173-
174-
if (trigger && angular.isString(subFields)) {
175-
formCtrl[subFields].$dirty = true;
176-
formCtrl[subFields].$pristine = false;
177-
}
178-
}
179-
180-
if (!controller)
181-
return;
182-
formCtrl = controller;
183-
try {
184-
subFields = angular.fromJson(attrs.validateMultipleFields);
185-
} catch (SyntaxError) {
186-
subFields = attrs.validateMultipleFields;
187-
}
188-
angular.forEach(element.find('input'), function(elem) {
189-
if (subFields.indexOf(elem.name) >= 0) {
190-
checkboxCtrls.push(formCtrl[elem.name]);
191-
}
192-
});
147+
link: function(scope, element, attrs, formCtrl) {
148+
var subFields, checkboxElems = [];
193149

194-
validate();
150+
function validate(event) {
151+
var valid = false;
152+
angular.forEach(checkboxElems, function(checkbox) {
153+
valid = valid || checkbox.checked;
154+
});
155+
formCtrl.$setValidity('required', valid);
156+
if (event) {
157+
formCtrl.$dirty = true;
158+
formCtrl.$pristine = false;
159+
scope.$apply();
195160
}
196161
}
162+
163+
if (!formCtrl)
164+
return;
165+
try {
166+
subFields = angular.fromJson(attrs.validateMultipleFields);
167+
} catch (SyntaxError) {
168+
if (!angular.isString(attrs.validateMultipleFields))
169+
return;
170+
subFields = [attrs.validateMultipleFields];
171+
formCtrl = formCtrl[subFields];
172+
}
173+
angular.forEach(element.find('input'), function(elem) {
174+
if (subFields.indexOf(elem.name) >= 0) {
175+
checkboxElems.push(elem);
176+
angular.element(elem).on('change', validate);
177+
}
178+
});
179+
180+
// remove "change" event handlers from each input field
181+
element.on('$destroy', function() {
182+
angular.forEach(element.find('input'), function(elem) {
183+
angular.element(elem).off('change');
184+
});
185+
});
186+
validate();
197187
}
198188
};
199189
});
@@ -278,7 +268,7 @@ djng_forms_module.factory('djangoForm', function() {
278268

279269
return {
280270
// setErrors takes care of updating prepared placeholder fields for displaying form errors
281-
// deteced by an AJAX submission. Returns true if errors have been added to the form.
271+
// detected by an AJAX submission. Returns true if errors have been added to the form.
282272
setErrors: function(form, errors) {
283273
// remove errors from this form, which may have been rejected by an earlier validation
284274
form.$message = '';

0 commit comments

Comments
 (0)