@@ -137,63 +137,53 @@ djng_forms_module.directive('ngModel', function() {
137
137
} ) ;
138
138
139
139
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`.
140
143
djng_forms_module . directive ( 'validateMultipleFields' , function ( ) {
141
144
return {
142
145
restrict : 'A' ,
143
146
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 = [ ] ;
193
149
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 ( ) ;
195
160
}
196
161
}
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 ( ) ;
197
187
}
198
188
} ;
199
189
} ) ;
@@ -278,7 +268,7 @@ djng_forms_module.factory('djangoForm', function() {
278
268
279
269
return {
280
270
// 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.
282
272
setErrors : function ( form , errors ) {
283
273
// remove errors from this form, which may have been rejected by an earlier validation
284
274
form . $message = '' ;
0 commit comments