Ticket #5481: choicefield_correct_type.3.diff
File choicefield_correct_type.3.diff, 4.6 KB (added by , 17 years ago) |
---|
-
django/newforms/fields.py
479 479 # choices can be any iterable, but we call list() on it because 480 480 # it will be consumed more than once. 481 481 self._choices = self.widget.choices = list(value) 482 # Build an index mapping the unicode value of each choice key to the 483 # original key. Strings are forced to unicode as per django policy, 484 # but other types are left alone. 485 self._choices_idx = dict([(smart_unicode(k),(isinstance(k, str) and unicode(k) or k)) for k, v in self._choices]) 482 486 483 487 choices = property(_get_choices, _set_choices) 484 488 … … 492 496 value = smart_unicode(value) 493 497 if value == u'': 494 498 return value 495 valid_values = set([smart_unicode(k) for k, v in self.choices])496 if value not in valid_values:499 500 if value not in self._choices_idx: 497 501 raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.')) 498 return value502 return self._choices_idx[value] 499 503 500 504 class MultipleChoiceField(ChoiceField): 501 505 hidden_widget = MultipleHiddenInput … … 511 515 return [] 512 516 if not isinstance(value, (list, tuple)): 513 517 raise ValidationError(ugettext(u'Enter a list of values.')) 514 new_value = [smart_unicode(val) for val in value]515 518 # Validate that each value in the value list is in self.choices. 516 valid_values = set([smart_unicode(k) for k, v in self.choices]) 517 for val in new_value: 518 if val not in valid_values: 519 raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val) 520 return new_value 519 try: 520 return [self._choices_idx[smart_unicode(val)] for val in value] 521 except KeyError: 522 raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val) 521 523 522 524 class ComboField(Field): 523 525 """ -
tests/regressiontests/forms/tests.py
1795 1795 ... 1796 1796 ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] 1797 1797 1798 >>> class TestX(object): 1799 ... def __unicode__(self): 1800 ... return u'TestX-U' 1801 ... def __str__(self): 1802 ... return 'TestX-S' 1803 >>> f = ChoiceField(choices=[(1, '1'), (True, 'Good'), (TestX(), 'X')]) 1804 >>> f.clean('1') 1805 1 1806 >>> f.clean('True') 1807 True 1808 >>> type(f.clean('TestX-U')) 1809 <class '__main__.TestX'> 1810 >>> f.clean('TestX-S') 1811 Traceback (most recent call last): 1812 ... 1813 ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] 1814 >>> f.clean(True) 1815 True 1816 >>> f.clean(1) 1817 1 1818 1798 1819 # NullBooleanField ############################################################ 1799 1820 1800 1821 >>> f = NullBooleanField() … … 1875 1896 ... 1876 1897 ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] 1877 1898 1899 >>> class TestX(object): 1900 ... def __unicode__(self): 1901 ... return u'TestX-U' 1902 ... def __str__(self): 1903 ... return 'TestX-S' 1904 >>> f = MultipleChoiceField(choices=[(1, '1'), (True, 'Good'), (TestX(), 'X')]) 1905 >>> f.clean(['1']) 1906 [1] 1907 >>> f.clean(['True']) 1908 [True] 1909 >>> type(f.clean(['TestX-U'])[0]) 1910 <class '__main__.TestX'> 1911 >>> f.clean(['TestX-S']) 1912 Traceback (most recent call last): 1913 ... 1914 ValidationError: [u'Select a valid choice. TestX-S is not one of the available choices.'] 1915 >>> f.clean(['True', '1']) 1916 [True, 1] 1917 >>> f.clean([True, 1]) 1918 [True, 1] 1919 1878 1920 # ComboField ################################################################## 1879 1921 1880 1922 ComboField takes a list of fields that should be used to validate a value, -
tests/regressiontests/forms/regressions.py
53 53 >>> f.clean('\xd1\x88\xd1\x82.') 54 54 u'\u0448\u0442.' 55 55 56 >>> UNITS = ((u'\u043c\u0435\u0441.', u'\u043c\u0435\u0441.'), (u'\u0448\u0442.', u'\u0448\u0442.')) 57 >>> f = ChoiceField(choices=UNITS) 58 >>> f.clean(u'\u0448\u0442.') 59 u'\u0448\u0442.' 60 >>> f.clean('\xd1\x88\xd1\x82.') 61 u'\u0448\u0442.' 62 56 63 Translated error messages used to be buggy. 57 64 >>> activate('ru') 58 65 >>> f = SomeForm({})