Code

Ticket #5481: choicefield_correct_type.diff

File choicefield_correct_type.diff, 5.2 KB (added by rcoup, 7 years ago)
Line 
1Index: django/newforms/fields.py
2===================================================================
3--- django/newforms/fields.py   (revision 6224)
4+++ django/newforms/fields.py   (working copy)
5@@ -492,10 +492,10 @@
6         value = smart_unicode(value)
7         if value == u'':
8             return value
9-        valid_values = set([smart_unicode(k) for k, v in self.choices])
10+        valid_values = dict([(smart_unicode(k),k) for k, v in self.choices])
11         if value not in valid_values:
12             raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.'))
13-        return value
14+        return valid_values[value]
15 
16 class MultipleChoiceField(ChoiceField):
17     hidden_widget = MultipleHiddenInput
18@@ -511,12 +511,14 @@
19             return []
20         if not isinstance(value, (list, tuple)):
21             raise ValidationError(ugettext(u'Enter a list of values.'))
22-        new_value = [smart_unicode(val) for val in value]
23         # Validate that each value in the value list is in self.choices.
24-        valid_values = set([smart_unicode(k) for k, v in self.choices])
25-        for val in new_value:
26+        valid_values = dict([(smart_unicode(k),k) for k, v in self.choices])
27+        new_value = []
28+        for val in [smart_unicode(val) for val in value]:
29             if val not in valid_values:
30                 raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val)
31+            else:
32+                new_value.append(valid_values[val])
33         return new_value
34 
35 class ComboField(Field):
36Index: tests/regressiontests/forms/tests.py
37===================================================================
38--- tests/regressiontests/forms/tests.py        (revision 6231)
39+++ tests/regressiontests/forms/tests.py        (working copy)
40@@ -1765,9 +1765,9 @@
41 ...
42 ValidationError: [u'This field is required.']
43 >>> f.clean(1)
44-u'1'
45+'1'
46 >>> f.clean('1')
47-u'1'
48+'1'
49 >>> f.clean('3')
50 Traceback (most recent call last):
51 ...
52@@ -1779,9 +1779,9 @@
53 >>> f.clean(None)
54 u''
55 >>> f.clean(1)
56-u'1'
57+'1'
58 >>> f.clean('1')
59-u'1'
60+'1'
61 >>> f.clean('3')
62 Traceback (most recent call last):
63 ...
64@@ -1789,12 +1789,24 @@
65 
66 >>> f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')])
67 >>> f.clean('J')
68-u'J'
69+'J'
70 >>> f.clean('John')
71 Traceback (most recent call last):
72 ...
73 ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
74 
75+>>> f = ChoiceField(choices=[(1, 'John'), (2, 'Paul')])
76+>>> f.clean('1')
77+1
78+>>> f.clean(1)
79+1
80+
81+>>> f = ChoiceField(choices=[(u'1', 'John'), (2, 'Paul')])
82+>>> f.clean('1')
83+u'1'
84+>>> f.clean(1)
85+u'1'
86+
87 # NullBooleanField ############################################################
88 
89 >>> f = NullBooleanField()
90@@ -1821,15 +1833,15 @@
91 ...
92 ValidationError: [u'This field is required.']
93 >>> f.clean([1])
94-[u'1']
95+['1']
96 >>> f.clean(['1'])
97-[u'1']
98+['1']
99 >>> f.clean(['1', '2'])
100-[u'1', u'2']
101+['1', '2']
102 >>> f.clean([1, '2'])
103-[u'1', u'2']
104+['1', '2']
105 >>> f.clean((1, '2'))
106-[u'1', u'2']
107+['1', '2']
108 >>> f.clean('hello')
109 Traceback (most recent call last):
110 ...
111@@ -1853,15 +1865,15 @@
112 >>> f.clean(None)
113 []
114 >>> f.clean([1])
115-[u'1']
116+['1']
117 >>> f.clean(['1'])
118-[u'1']
119+['1']
120 >>> f.clean(['1', '2'])
121-[u'1', u'2']
122+['1', '2']
123 >>> f.clean([1, '2'])
124-[u'1', u'2']
125+['1', '2']
126 >>> f.clean((1, '2'))
127-[u'1', u'2']
128+['1', '2']
129 >>> f.clean('hello')
130 Traceback (most recent call last):
131 ...
132@@ -1875,6 +1887,22 @@
133 ...
134 ValidationError: [u'Select a valid choice. 3 is not one of the available choices.']
135 
136+>>> f = MultipleChoiceField(choices=[(1, '1'), (2, '2')])
137+>>> f.clean(['1'])
138+[1]
139+>>> f.clean(['1', '2'])
140+[1, 2]
141+>>> f.clean([1, '2'])
142+[1, 2]
143+
144+>>> f = MultipleChoiceField(choices=[(u'1', '1'), (u'2', '2')])
145+>>> f.clean(['1'])
146+[u'1']
147+>>> f.clean(['1', '2'])
148+[u'1', u'2']
149+>>> f.clean([1, '2'])
150+[u'1', u'2']
151+
152 # ComboField ##################################################################
153 
154 ComboField takes a list of fields that should be used to validate a value,
155@@ -2529,12 +2557,12 @@
156 >>> f.errors
157 {}
158 >>> f.cleaned_data
159-{'composers': [u'J'], 'name': u'Yesterday'}
160+{'composers': ['J'], 'name': u'Yesterday'}
161 >>> f = SongForm({'name': 'Yesterday', 'composers': ['J', 'P']}, auto_id=False)
162 >>> f.errors
163 {}
164 >>> f.cleaned_data
165-{'composers': [u'J', u'P'], 'name': u'Yesterday'}
166+{'composers': ['J', 'P'], 'name': u'Yesterday'}
167 
168 Validation errors are HTML-escaped when output as HTML.
169 >>> class EscapingForm(Form):
170Index: tests/regressiontests/forms/regressions.py
171===================================================================
172--- tests/regressiontests/forms/regressions.py  (revision 6224)
173+++ tests/regressiontests/forms/regressions.py  (working copy)
174@@ -49,6 +49,13 @@
175 >>> UNITS = (('\xd0\xbc\xd0\xb5\xd1\x81.', '\xd0\xbc\xd0\xb5\xd1\x81.'), ('\xd1\x88\xd1\x82.', '\xd1\x88\xd1\x82.'))
176 >>> f = ChoiceField(choices=UNITS)
177 >>> f.clean(u'\u0448\u0442.')
178+'\xd1\x88\xd1\x82.'
179+>>> f.clean('\xd1\x88\xd1\x82.')
180+'\xd1\x88\xd1\x82.'
181+
182+>>> UNITS = ((u'\u043c\u0435\u0441.', u'\u043c\u0435\u0441.'), (u'\u0448\u0442.', u'\u0448\u0442.'))
183+>>> f = ChoiceField(choices=UNITS)
184+>>> f.clean(u'\u0448\u0442.')
185 u'\u0448\u0442.'
186 >>> f.clean('\xd1\x88\xd1\x82.')
187 u'\u0448\u0442.'