Ticket #6033: is_empty_clean_field.4.diff
File is_empty_clean_field.4.diff, 5.7 KB (added by , 17 years ago) |
---|
-
django/newforms/formsets.py
116 116 self.deleted_data = [] 117 117 # Process change forms 118 118 for form in self.change_forms: 119 if form.is_valid(): 119 if form.is_bound: 120 form.full_clean(keep_invalid=True) 121 if hasattr(form, 'cleaned_data'): 120 122 if self.deletable and form.cleaned_data[DELETION_FIELD_NAME]: 121 123 self.deleted_data.append(form.cleaned_data) 124 elif form.is_valid(): 125 self.cleaned_data.append(form.cleaned_data) 122 126 else: 123 self.cleaned_data.append(form.cleaned_data) 127 self._is_valid = False 128 errors.append(form.errors) 124 129 else: 125 self._is_valid = False 126 errors.append(form.errors)130 self._is_valid = False 131 errors.append(form.errors) 127 132 # Process add forms in reverse so we can easily tell when the remaining 128 133 # ones should be required. 129 134 reamining_forms_required = False … … 168 173 """ 169 174 return self.cleaned_data 170 175 176 def clean_field(self, form, f): 177 if f in form.fields: 178 field = form.fields.get(f) 179 value = field.widget.value_from_datadict(self.data, self.files, '%s-%s' % (form.prefix, f)) 180 try: 181 value = field.clean(value) 182 if hasattr(self, 'clean_%s' % f): 183 value = getattr(self, 'clean_%s' % f)() 184 except ValidationError, e: 185 return False 186 return value 187 else: 188 return False 189 171 190 def add_fields(self, form, index): 172 191 """A hook for adding extra fields on to each form instance.""" 173 192 if self.orderable: -
django/newforms/models.py
310 310 existing_objects[obj._get_pk_val()] = obj 311 311 saved_instances = [] 312 312 for form in self.change_forms: 313 obj = existing_objects[ form.cleaned_data[self.model._meta.pk.attname]]314 if self.deletable and form.cleaned_data[DELETION_FIELD_NAME]:313 obj = existing_objects[self.clean_field(form, self.model._meta.pk.attname)] 314 if self.deletable and self.clean_field(form, DELETION_FIELD_NAME): 315 315 obj.delete() 316 el se:316 elif self.is_valid(): 317 317 saved_instances.append(self.save_instance(form, obj, commit=commit)) 318 318 return saved_instances 319 319 … … 325 325 # If someone has marked an add form for deletion, don't save the 326 326 # object. At some point it would be nice if we didn't display 327 327 # the deletion widget for add forms. 328 if self.deletable and form.cleaned_data[DELETION_FIELD_NAME]:328 if self.deletable and self.clean_field(form, DELETION_FIELD_NAME): 329 329 continue 330 new_objects.append(self.save_new(form, commit=commit)) 330 if self.is_valid(): 331 new_objects.append(self.save_new(form, commit=commit)) 331 332 return new_objects 332 333 333 334 def add_fields(self, form, index): -
django/newforms/forms.py
191 191 return False 192 192 return True 193 193 194 def full_clean(self ):194 def full_clean(self, keep_invalid=False): 195 195 """ 196 196 Cleans all of self.data and populates self._errors and 197 197 self.cleaned_data. … … 219 219 self.cleaned_data = self.clean() 220 220 except ValidationError, e: 221 221 self._errors[NON_FIELD_ERRORS] = e.messages 222 if self._errors:222 if not keep_invalid and self._errors: 223 223 delattr(self, 'cleaned_data') 224 224 225 225 def clean(self): -
tests/regressiontests/forms/widgets.py
292 292 >>> w.value_from_datadict({}, {}, 'testing') 293 293 False 294 294 295 The CheckboxInput widget will always be empty when there is a False value 296 >>> w.is_empty(False) 297 True 298 >>> w.is_empty(True) 299 False 300 295 301 # Select Widget ############################################################### 296 302 297 303 >>> w = Select() … … 453 459 <option value="3" selected="selected">No</option> 454 460 </select> 455 461 462 The NullBooleanSelect widget will always be empty when Unknown or No is selected 463 as its value. This is to stay compliant with the CheckboxInput behavior 464 >>> w.is_empty(False) 465 True 466 >>> w.is_empty(None) 467 True 468 >>> w.is_empty(True) 469 False 470 456 471 """ + \ 457 472 r""" # [This concatenation is to keep the string below the jython's 32K limit]. 458 473 # SelectMultiple Widget ####################################################### … … 895 910 >>> w.render('name', ['john', 'lennon']) 896 911 u'<input id="bar_0" type="text" class="big" value="john" name="name_0" /><br /><input id="bar_1" type="text" class="small" value="lennon" name="name_1" />' 897 912 913 The MultiWidget will be empty only when all widgets are considered empty. 914 >>> w.is_empty(['john', 'lennon']) 915 False 916 >>> w.is_empty(['john', '']) 917 False 918 >>> w.is_empty(['', '']) 919 True 920 >>> w.is_empty([None, None]) 921 True 922 898 923 # SplitDateTimeWidget ######################################################### 899 924 900 925 >>> w = SplitDateTimeWidget()