Changeset 6837
- Timestamp:
- 12/02/07 12:14:11 (9 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/newforms-admin/django/newforms/forms.py
r6776 r6837 183 183 if name in exceptions: 184 184 continue 185 # value_from_datadict() gets the data from the d ictionary.185 # value_from_datadict() gets the data from the data dictionaries. 186 186 # Each widget type knows how to retrieve its own data, because some 187 187 # widgets split data over several HTML fields. 188 188 value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) 189 # HACK: ['', ''] and [None, None] deal with SplitDateTimeWidget. This should be more robust. 190 if value not in (None, '', ['', ''], [None, None]): 189 if not field.widget.is_empty(value): 191 190 return False 192 191 return True django/branches/newforms-admin/django/newforms/widgets.py
r6777 r6837 166 166 """ 167 167 return data.get(name, None) 168 169 def is_empty(self, value): 170 """ 171 Given a dictionary of data and this widget's name, return True if the 172 widget data is empty or False when not empty. 173 """ 174 if value not in (None, ''): 175 return False 176 return True 168 177 169 178 def id_for_label(self, id_): … … 302 311 return False 303 312 return super(CheckboxInput, self).value_from_datadict(data, files, name) 313 314 def is_empty(self, value): 315 # this widget will always either be True or False, so always return the 316 # opposite value so False values will make the form empty 317 return not value 304 318 305 319 class Select(Widget): … … 344 358 value = data.get(name, None) 345 359 return {u'2': True, u'3': False, True: True, False: False}.get(value, None) 360 361 def is_empty(self, value): 362 # this widget will always either be True, False or None, so always 363 # return the opposite value so False and None values will make the 364 # form empty. 365 return not value 346 366 347 367 class SelectMultiple(Widget): … … 540 560 def value_from_datadict(self, data, files, name): 541 561 return [widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)] 562 563 def is_empty(self, value): 564 for widget, val in zip(self.widgets, value): 565 if not widget.is_empty(val): 566 return False 567 return True 542 568 543 569 def format_output(self, rendered_widgets): django/branches/newforms-admin/tests/regressiontests/forms/widgets.py
r6777 r6837 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 … … 453 459 <option value="3" selected="selected">No</option> 454 460 </select> 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 455 470 456 471 """ + \ … … 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
