Ticket #20039: 20039-1.diff

File 20039-1.diff, 4.3 KB (added by Claude Paroz, 12 years ago)
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index bc37701..8c0a061 100644
    a b class ChoiceField(Field):  
    782782            if isinstance(v, (list, tuple)):
    783783                # This is an optgroup, so look inside the group for options
    784784                for k2, v2 in v:
    785                     if value == smart_text(k2):
     785                    if value == k2 or force_text(value) == force_text(k2):
    786786                        return True
    787787            else:
    788                 if value == smart_text(k):
     788                if value == k or force_text(value) == force_text(k):
    789789                    return True
    790790        return False
    791791
    class TypedChoiceField(ChoiceField):  
    801801        right type.
    802802        """
    803803        value = super(TypedChoiceField, self).to_python(value)
    804         super(TypedChoiceField, self).validate(value)
    805804        if value == self.empty_value or value in self.empty_values:
    806805            return self.empty_value
    807806        try:
    class TypedChoiceField(ChoiceField):  
    810809            raise ValidationError(self.error_messages['invalid_choice'] % {'value': value})
    811810        return value
    812811
    813     def validate(self, value):
    814         pass
    815 
    816812
    817813class MultipleChoiceField(ChoiceField):
    818814    hidden_widget = MultipleHiddenInput
    class TypedMultipleChoiceField(MultipleChoiceField):  
    864860        right type.
    865861        """
    866862        value = super(TypedMultipleChoiceField, self).to_python(value)
    867         super(TypedMultipleChoiceField, self).validate(value)
    868863        if value == self.empty_value or value in self.empty_values:
    869864            return self.empty_value
    870865        new_value = []
    class TypedMultipleChoiceField(MultipleChoiceField):  
    876871        return new_value
    877872
    878873    def validate(self, value):
    879         pass
     874        if value == self.empty_value:
     875            if self.required:
     876                raise ValidationError(self.error_messages['required'])
     877        else:
     878            super(TypedMultipleChoiceField, self).validate(value)
     879
    880880
    881881class ComboField(Field):
    882882    """
  • tests/forms_tests/tests/fields.py

    diff --git a/tests/forms_tests/tests/fields.py b/tests/forms_tests/tests/fields.py
    index 3d3206e..95e14c4 100644
    a b class FieldsTests(SimpleTestCase):  
    911911        f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None)
    912912        self.assertEqual(None, f.clean(''))
    913913
     914    def test_typedchoicefield_has_changed(self):
     915        # has_changed should not trigger required validation
     916        f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=True)
     917        self.assertFalse(f._has_changed(None, ''))
     918
    914919    # NullBooleanField ############################################################
    915920
    916921    def test_nullbooleanfield_1(self):
    class FieldsTests(SimpleTestCase):  
    10601065        f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None)
    10611066        self.assertEqual(None, f.clean([]))
    10621067
     1068    def test_typedmultiplechoicefield_has_changed(self):
     1069        # has_changed should not trigger required validation
     1070        f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=True)
     1071        self.assertFalse(f._has_changed(None, ''))
     1072
    10631073   # ComboField ##################################################################
    10641074
    10651075    def test_combofield_1(self):
  • tests/forms_tests/tests/regressions.py

    diff --git a/tests/forms_tests/tests/regressions.py b/tests/forms_tests/tests/regressions.py
    index 4249b8a..ba741bc 100644
    a b class FormsRegressionsTestCase(TestCase):  
    6161        UNITS = ((b'\xd0\xbc\xd0\xb5\xd1\x81.', b'\xd0\xbc\xd0\xb5\xd1\x81.'),
    6262                 (b'\xd1\x88\xd1\x82.', b'\xd1\x88\xd1\x82.'))
    6363        f = ChoiceField(choices=UNITS)
    64         self.assertEqual(f.clean('\u0448\u0442.'), '\u0448\u0442.')
    6564        with warnings.catch_warnings():
    6665            # Ignore UnicodeWarning
    6766            warnings.simplefilter("ignore")
     67            self.assertEqual(f.clean('\u0448\u0442.'), '\u0448\u0442.')
    6868            self.assertEqual(f.clean(b'\xd1\x88\xd1\x82.'), '\u0448\u0442.')
    6969
    7070        # Translated error messages used to be buggy.
Back to Top