Ticket #5481: choicefield_correct_type.diff

File choicefield_correct_type.diff, 5.2 KB (added by Robert Coup, 17 years ago)
  • django/newforms/fields.py

     
    492492        value = smart_unicode(value)
    493493        if value == u'':
    494494            return value
    495         valid_values = set([smart_unicode(k) for k, v in self.choices])
     495        valid_values = dict([(smart_unicode(k),k) for k, v in self.choices])
    496496        if value not in valid_values:
    497497            raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.'))
    498         return value
     498        return valid_values[value]
    499499
    500500class MultipleChoiceField(ChoiceField):
    501501    hidden_widget = MultipleHiddenInput
     
    511511            return []
    512512        if not isinstance(value, (list, tuple)):
    513513            raise ValidationError(ugettext(u'Enter a list of values.'))
    514         new_value = [smart_unicode(val) for val in value]
    515514        # 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:
     515        valid_values = dict([(smart_unicode(k),k) for k, v in self.choices])
     516        new_value = []
     517        for val in [smart_unicode(val) for val in value]:
    518518            if val not in valid_values:
    519519                raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val)
     520            else:
     521                new_value.append(valid_values[val])
    520522        return new_value
    521523
    522524class ComboField(Field):
  • tests/regressiontests/forms/tests.py

     
    17651765...
    17661766ValidationError: [u'This field is required.']
    17671767>>> f.clean(1)
    1768 u'1'
     1768'1'
    17691769>>> f.clean('1')
    1770 u'1'
     1770'1'
    17711771>>> f.clean('3')
    17721772Traceback (most recent call last):
    17731773...
     
    17791779>>> f.clean(None)
    17801780u''
    17811781>>> f.clean(1)
    1782 u'1'
     1782'1'
    17831783>>> f.clean('1')
    1784 u'1'
     1784'1'
    17851785>>> f.clean('3')
    17861786Traceback (most recent call last):
    17871787...
     
    17891789
    17901790>>> f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')])
    17911791>>> f.clean('J')
    1792 u'J'
     1792'J'
    17931793>>> f.clean('John')
    17941794Traceback (most recent call last):
    17951795...
    17961796ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
    17971797
     1798>>> f = ChoiceField(choices=[(1, 'John'), (2, 'Paul')])
     1799>>> f.clean('1')
     18001
     1801>>> f.clean(1)
     18021
     1803
     1804>>> f = ChoiceField(choices=[(u'1', 'John'), (2, 'Paul')])
     1805>>> f.clean('1')
     1806u'1'
     1807>>> f.clean(1)
     1808u'1'
     1809
    17981810# NullBooleanField ############################################################
    17991811
    18001812>>> f = NullBooleanField()
     
    18211833...
    18221834ValidationError: [u'This field is required.']
    18231835>>> f.clean([1])
    1824 [u'1']
     1836['1']
    18251837>>> f.clean(['1'])
    1826 [u'1']
     1838['1']
    18271839>>> f.clean(['1', '2'])
    1828 [u'1', u'2']
     1840['1', '2']
    18291841>>> f.clean([1, '2'])
    1830 [u'1', u'2']
     1842['1', '2']
    18311843>>> f.clean((1, '2'))
    1832 [u'1', u'2']
     1844['1', '2']
    18331845>>> f.clean('hello')
    18341846Traceback (most recent call last):
    18351847...
     
    18531865>>> f.clean(None)
    18541866[]
    18551867>>> f.clean([1])
    1856 [u'1']
     1868['1']
    18571869>>> f.clean(['1'])
    1858 [u'1']
     1870['1']
    18591871>>> f.clean(['1', '2'])
    1860 [u'1', u'2']
     1872['1', '2']
    18611873>>> f.clean([1, '2'])
    1862 [u'1', u'2']
     1874['1', '2']
    18631875>>> f.clean((1, '2'))
    1864 [u'1', u'2']
     1876['1', '2']
    18651877>>> f.clean('hello')
    18661878Traceback (most recent call last):
    18671879...
     
    18751887...
    18761888ValidationError: [u'Select a valid choice. 3 is not one of the available choices.']
    18771889
     1890>>> f = MultipleChoiceField(choices=[(1, '1'), (2, '2')])
     1891>>> f.clean(['1'])
     1892[1]
     1893>>> f.clean(['1', '2'])
     1894[1, 2]
     1895>>> f.clean([1, '2'])
     1896[1, 2]
     1897
     1898>>> f = MultipleChoiceField(choices=[(u'1', '1'), (u'2', '2')])
     1899>>> f.clean(['1'])
     1900[u'1']
     1901>>> f.clean(['1', '2'])
     1902[u'1', u'2']
     1903>>> f.clean([1, '2'])
     1904[u'1', u'2']
     1905
    18781906# ComboField ##################################################################
    18791907
    18801908ComboField takes a list of fields that should be used to validate a value,
     
    25292557>>> f.errors
    25302558{}
    25312559>>> f.cleaned_data
    2532 {'composers': [u'J'], 'name': u'Yesterday'}
     2560{'composers': ['J'], 'name': u'Yesterday'}
    25332561>>> f = SongForm({'name': 'Yesterday', 'composers': ['J', 'P']}, auto_id=False)
    25342562>>> f.errors
    25352563{}
    25362564>>> f.cleaned_data
    2537 {'composers': [u'J', u'P'], 'name': u'Yesterday'}
     2565{'composers': ['J', 'P'], 'name': u'Yesterday'}
    25382566
    25392567Validation errors are HTML-escaped when output as HTML.
    25402568>>> class EscapingForm(Form):
  • tests/regressiontests/forms/regressions.py

     
    4949>>> UNITS = (('\xd0\xbc\xd0\xb5\xd1\x81.', '\xd0\xbc\xd0\xb5\xd1\x81.'), ('\xd1\x88\xd1\x82.', '\xd1\x88\xd1\x82.'))
    5050>>> f = ChoiceField(choices=UNITS)
    5151>>> f.clean(u'\u0448\u0442.')
     52'\xd1\x88\xd1\x82.'
     53>>> f.clean('\xd1\x88\xd1\x82.')
     54'\xd1\x88\xd1\x82.'
     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.')
    5259u'\u0448\u0442.'
    5360>>> f.clean('\xd1\x88\xd1\x82.')
    5461u'\u0448\u0442.'
Back to Top