Ticket #6033: is_empty_clean_field.2.diff
File is_empty_clean_field.2.diff, 6.3 KB (added by , 17 years ago) |
---|
-
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(): 120 if self.deletable and form.cleaned_data[DELETION_FIELD_NAME]: 121 self.deleted_data.append(form.cleaned_data) 119 if form.is_valid() or self.deletable: 120 if self.deletable and self.clean_field(form, DELETION_FIELD_NAME): 121 self.deleted_data.append({DELETION_FIELD_NAME: True}) 122 elif form.is_valid(): 123 self.cleaned_data.append(form.cleaned_data) 122 124 else: 123 self.cleaned_data.append(form.cleaned_data) 124 else: 125 self._is_valid = False 125 self._is_valid = False 126 126 errors.append(form.errors) 127 127 # Process add forms in reverse so we can easily tell when the remaining 128 128 # ones should be required. … … 168 168 """ 169 169 return self.cleaned_data 170 170 171 def clean_field(self, form, f): 172 if f in form.fields: 173 field = form.fields.get(f) 174 value = field.widget.value_from_datadict(self.data, self.files, '%s-%s' % (form.prefix, f)) 175 try: 176 value = field.clean(value) 177 if hasattr(self, 'clean_%s' % f): 178 value = getattr(self, 'clean_%s' % f)() 179 except ValidationError, e: 180 return False 181 return value 182 else: 183 return False 184 171 185 def add_fields(self, form, index): 172 186 """A hook for adding extra fields on to each form instance.""" 173 187 if self.orderable: -
newforms/models.py
294 294 existing_objects[obj._get_pk_val()] = obj 295 295 saved_instances = [] 296 296 for form in self.change_forms: 297 obj = existing_objects[ form.cleaned_data[self.model._meta.pk.attname]]298 if self.deletable and form.cleaned_data[DELETION_FIELD_NAME]:297 obj = existing_objects[self.clean_field(form, self.model._meta.pk.attname)] 298 if self.deletable and self.clean_field(form, DELETION_FIELD_NAME): 299 299 obj.delete() 300 el se:300 elif self.is_valid(): 301 301 saved_instances.append(self.save_instance(form, obj, commit=commit)) 302 302 return saved_instances 303 303 … … 309 309 # If someone has marked an add form for deletion, don't save the 310 310 # object. At some point it would be nice if we didn't display 311 311 # the deletion widget for add forms. 312 if self.deletable and form.cleaned_data[DELETION_FIELD_NAME]:312 if self.deletable and self.clean_field(form, DELETION_FIELD_NAME): 313 313 continue 314 new_objects.append(self.save_new(form, commit=commit)) 314 if self.is_valid(): 315 new_objects.append(self.save_new(form, commit=commit)) 315 316 return new_objects 316 317 317 318 def add_fields(self, form, index): -
newforms/forms.py
189 189 for name, field in self.fields.items(): 190 190 if name in exceptions: 191 191 continue 192 # value_from_datadict() gets the data from the d ictionary.192 # value_from_datadict() gets the data from the data dictionaries. 193 193 # Each widget type knows how to retrieve its own data, because some 194 194 # widgets split data over several HTML fields. 195 195 value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) 196 # HACK: ['', ''] and [None, None] deal with SplitDateTimeWidget. This should be more robust. 197 if value not in (None, '', ['', ''], [None, None]): 196 if not field.widget.is_empty(value): 198 197 return False 199 198 return True 200 199 -
newforms/widgets.py
164 164 of this widget. Returns None if it's not provided. 165 165 """ 166 166 return data.get(name, None) 167 168 def is_empty(self, value): 169 """ 170 Given a dictionary of data and this widget's name, return True if the 171 widget data is empty or False when not empty. 172 """ 173 if value not in (None, ''): 174 return False 175 return True 167 176 168 177 def id_for_label(self, id_): 169 178 """ … … 294 303 # send results for unselected checkboxes. 295 304 return False 296 305 return super(CheckboxInput, self).value_from_datadict(data, files, name) 306 307 def is_empty(self, value): 308 # this widget will always either be True or False, so always return the 309 # opposite value so False values will make the form empty 310 return not value 297 311 298 312 class Select(Widget): 299 313 def __init__(self, attrs=None, choices=()): … … 333 347 def value_from_datadict(self, data, files, name): 334 348 value = data.get(name, None) 335 349 return {u'2': True, u'3': False, True: True, False: False}.get(value, None) 350 351 def is_empty(self, value): 352 # this widget will always either be True, False or None, so always 353 # return the opposite value so False and None values will make the 354 # form empty. 355 return not value 336 356 337 357 class SelectMultiple(Widget): 338 358 def __init__(self, attrs=None, choices=()): … … 521 541 522 542 def value_from_datadict(self, data, files, name): 523 543 return [widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)] 544 545 def is_empty(self, value): 546 for widget, val in zip(self.widgets, value): 547 if not widget.is_empty(val): 548 return False 549 return True 524 550 525 551 def format_output(self, rendered_widgets): 526 552 """