Django

Code

Changeset 4113

Show
Ignore:
Timestamp:
11/26/06 18:49:26 (2 years ago)
Author:
adrian
Message:

newforms: Added a bunch of unit tests and fixed some bugs in the case of required=False for various Field subclasses

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/fields.py

    r4111 r4113  
    7777        """ 
    7878        super(IntegerField, self).clean(value) 
     79        if not self.required and value in EMPTY_VALUES: 
     80            return u'' 
    7981        try: 
    8082            return int(value) 
     
    171173        if value in EMPTY_VALUES: value = u'' 
    172174        value = smart_unicode(value) 
    173         if (value or self.required) and not self.regex.search(value): 
     175        if not self.required and value == u'': 
     176            return value 
     177        if not self.regex.search(value): 
    174178            raise ValidationError(self.error_message) 
    175179        return value 
     
    247251        if value in EMPTY_VALUES: value = u'' 
    248252        value = smart_unicode(value) 
     253        if not self.required and value == u'': 
     254            return value 
    249255        valid_values = set([str(k) for k, v in self.choices]) 
    250256        if value not in valid_values: 
     
    260266        Validates that the input is a list or tuple. 
    261267        """ 
     268        if self.required and not value: 
     269            raise ValidationError(u'This field is required.') 
     270        elif not self.required and not value: 
     271            return [] 
    262272        if not isinstance(value, (list, tuple)): 
    263273            raise ValidationError(u'Enter a list of values.') 
    264         if self.required and not value: 
    265             raise ValidationError(u'This field is required.') 
    266274        new_value = [] 
    267275        for val in value: 
     
    278286    def __init__(self, fields=(), required=True, widget=None): 
    279287        Field.__init__(self, required, widget) 
     288        # Set 'required' to False on the individual fields, because the 
     289        # required validation will be handled by ComboField, not by those 
     290        # individual fields. 
     291        for f in fields: 
     292            f.required = False 
    280293        self.fields = fields 
    281294 
  • django/trunk/tests/regressiontests/forms/tests.py

    r4111 r4113  
    478478# CharField ################################################################### 
    479479 
     480>>> f = CharField() 
     481>>> f.clean(1) 
     482u'1' 
     483>>> f.clean('hello') 
     484u'hello' 
     485>>> f.clean(None) 
     486Traceback (most recent call last): 
     487... 
     488ValidationError: [u'This field is required.'] 
     489>>> f.clean('') 
     490Traceback (most recent call last): 
     491... 
     492ValidationError: [u'This field is required.'] 
     493>>> f.clean([1, 2, 3]) 
     494u'[1, 2, 3]' 
     495 
    480496>>> f = CharField(required=False) 
    481497>>> f.clean(1) 
     
    519535 
    520536>>> f = IntegerField() 
     537>>> f.clean('') 
     538Traceback (most recent call last): 
     539... 
     540ValidationError: [u'This field is required.'] 
     541>>> f.clean(None) 
     542Traceback (most recent call last): 
     543... 
     544ValidationError: [u'This field is required.'] 
     545>>> f.clean('1') 
     5461 
     547>>> isinstance(f.clean('1'), int) 
     548True 
     549>>> f.clean('23') 
     55023 
     551>>> f.clean('a') 
     552Traceback (most recent call last): 
     553... 
     554ValidationError: [u'Enter a whole number.'] 
     555>>> f.clean('1 ') 
     5561 
     557>>> f.clean(' 1') 
     5581 
     559>>> f.clean(' 1 ') 
     5601 
     561>>> f.clean('1a') 
     562Traceback (most recent call last): 
     563... 
     564ValidationError: [u'Enter a whole number.'] 
     565 
     566>>> f = IntegerField(required=False) 
     567>>> f.clean('') 
     568u'' 
     569>>> f.clean(None) 
     570u'' 
    521571>>> f.clean('1') 
    5225721 
     
    682732ValidationError: [u'Enter a valid date/time.'] 
    683733 
     734>>> f = DateTimeField(required=False) 
     735>>> f.clean(None) 
     736>>> repr(f.clean(None)) 
     737'None' 
     738>>> f.clean('') 
     739>>> repr(f.clean('')) 
     740'None' 
     741 
    684742# RegexField ################################################################## 
    685743 
     
    753811 
    754812>>> f = EmailField() 
     813>>> f.clean('') 
     814Traceback (most recent call last): 
     815... 
     816ValidationError: [u'This field is required.'] 
     817>>> f.clean(None) 
     818Traceback (most recent call last): 
     819... 
     820ValidationError: [u'This field is required.'] 
    755821>>> f.clean('person@example.com') 
    756822u'person@example.com' 
     
    768834ValidationError: [u'Enter a valid e-mail address.'] 
    769835 
     836>>> f = EmailField(required=False) 
     837>>> f.clean('') 
     838u'' 
     839>>> f.clean(None) 
     840u'' 
     841>>> f.clean('person@example.com') 
     842u'person@example.com' 
     843>>> f.clean('foo') 
     844Traceback (most recent call last): 
     845... 
     846ValidationError: [u'Enter a valid e-mail address.'] 
     847>>> f.clean('foo@') 
     848Traceback (most recent call last): 
     849... 
     850ValidationError: [u'Enter a valid e-mail address.'] 
     851>>> f.clean('foo@bar') 
     852Traceback (most recent call last): 
     853... 
     854ValidationError: [u'Enter a valid e-mail address.'] 
     855 
    770856# URLField ################################################################## 
    771857 
    772858>>> f = URLField() 
     859>>> f.clean('') 
     860Traceback (most recent call last): 
     861... 
     862ValidationError: [u'This field is required.'] 
     863>>> f.clean(None) 
     864Traceback (most recent call last): 
     865... 
     866ValidationError: [u'This field is required.'] 
     867>>> f.clean('http://example.com') 
     868u'http://example.com' 
     869>>> f.clean('http://www.example.com') 
     870u'http://www.example.com' 
     871>>> f.clean('foo') 
     872Traceback (most recent call last): 
     873... 
     874ValidationError: [u'Enter a valid URL.'] 
     875>>> f.clean('example.com') 
     876Traceback (most recent call last): 
     877... 
     878ValidationError: [u'Enter a valid URL.'] 
     879>>> f.clean('http://') 
     880Traceback (most recent call last): 
     881... 
     882ValidationError: [u'Enter a valid URL.'] 
     883>>> f.clean('http://example') 
     884Traceback (most recent call last): 
     885... 
     886ValidationError: [u'Enter a valid URL.'] 
     887>>> f.clean('http://example.') 
     888Traceback (most recent call last): 
     889... 
     890ValidationError: [u'Enter a valid URL.'] 
     891>>> f.clean('http://.com') 
     892Traceback (most recent call last): 
     893... 
     894ValidationError: [u'Enter a valid URL.'] 
     895 
     896>>> f = URLField(required=False) 
     897>>> f.clean('') 
     898u'' 
     899>>> f.clean(None) 
     900u'' 
    773901>>> f.clean('http://example.com') 
    774902u'http://example.com' 
     
    821949 
    822950>>> f = BooleanField() 
     951>>> f.clean('') 
     952Traceback (most recent call last): 
     953... 
     954ValidationError: [u'This field is required.'] 
     955>>> f.clean(None) 
     956Traceback (most recent call last): 
     957... 
     958ValidationError: [u'This field is required.'] 
    823959>>> f.clean(True) 
    824960True 
     
    832968True 
    833969 
     970>>> f = BooleanField(required=False) 
     971>>> f.clean('') 
     972False 
     973>>> f.clean(None) 
     974False 
     975>>> f.clean(True) 
     976True 
     977>>> f.clean(False) 
     978False 
     979>>> f.clean(1) 
     980True 
     981>>> f.clean(0) 
     982False 
     983>>> f.clean('Django rocks') 
     984True 
     985 
    834986# ChoiceField ################################################################# 
    835987 
    836988>>> f = ChoiceField(choices=[('1', '1'), ('2', '2')]) 
     989>>> f.clean('') 
     990Traceback (most recent call last): 
     991... 
     992ValidationError: [u'This field is required.'] 
     993>>> f.clean(None) 
     994Traceback (most recent call last): 
     995... 
     996ValidationError: [u'This field is required.'] 
    837997>>> f.clean(1) 
    838998u'1' 
    839999>>> f.clean('1') 
    8401000u'1' 
    841 >>> f.clean(None) 
    842 Traceback (most recent call last): 
    843 ... 
    844 ValidationError: [u'This field is required.'] 
    845 >>> f.clean('') 
    846 Traceback (most recent call last): 
    847 ... 
    848 ValidationError: [u'This field is required.'] 
     1001>>> f.clean('3') 
     1002Traceback (most recent call last): 
     1003... 
     1004ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] 
     1005 
     1006>>> f = ChoiceField(choices=[('1', '1'), ('2', '2')], required=False) 
     1007>>> f.clean('') 
     1008u'' 
     1009>>> f.clean(None) 
     1010u'' 
     1011>>> f.clean(1) 
     1012u'1' 
     1013>>> f.clean('1') 
     1014u'1' 
    8491015>>> f.clean('3') 
    8501016Traceback (most recent call last): 
     
    8631029 
    8641030>>> f = MultipleChoiceField(choices=[('1', '1'), ('2', '2')]) 
     1031>>> f.clean('') 
     1032Traceback (most recent call last): 
     1033... 
     1034ValidationError: [u'This field is required.'] 
     1035>>> f.clean(None) 
     1036Traceback (most recent call last): 
     1037... 
     1038ValidationError: [u'This field is required.'] 
    8651039>>> f.clean([1]) 
    8661040[u'1'] 
     
    8901064ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] 
    8911065 
     1066>>> f = MultipleChoiceField(choices=[('1', '1'), ('2', '2')], required=False) 
     1067>>> f.clean('') 
     1068[] 
     1069>>> f.clean(None) 
     1070[] 
     1071>>> f.clean([1]) 
     1072[u'1'] 
     1073>>> f.clean(['1']) 
     1074[u'1'] 
     1075>>> f.clean(['1', '2']) 
     1076[u'1', u'2'] 
     1077>>> f.clean([1, '2']) 
     1078[u'1', u'2'] 
     1079>>> f.clean((1, '2')) 
     1080[u'1', u'2'] 
     1081>>> f.clean('hello') 
     1082Traceback (most recent call last): 
     1083... 
     1084ValidationError: [u'Enter a list of values.'] 
     1085>>> f.clean([]) 
     1086[] 
     1087>>> f.clean(()) 
     1088[] 
     1089>>> f.clean(['3']) 
     1090Traceback (most recent call last): 
     1091... 
     1092ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] 
     1093 
    8921094# ComboField ################################################################## 
    8931095 
    8941096ComboField takes a list of fields that should be used to validate a value, 
    895 in that order: 
     1097in that order. 
    8961098>>> f = ComboField(fields=[CharField(max_length=20), EmailField()]) 
    8971099>>> f.clean('test@example.com') 
     
    9131115... 
    9141116ValidationError: [u'This field is required.'] 
     1117 
     1118>>> f = ComboField(fields=[CharField(max_length=20), EmailField()], required=False) 
     1119>>> f.clean('test@example.com') 
     1120u'test@example.com' 
     1121>>> f.clean('longemailaddress@example.com') 
     1122Traceback (most recent call last): 
     1123... 
     1124ValidationError: [u'Ensure this value has at most 20 characters.'] 
     1125>>> f.clean('not an e-mail') 
     1126Traceback (most recent call last): 
     1127... 
     1128ValidationError: [u'Enter a valid e-mail address.'] 
     1129>>> f.clean('') 
     1130u'' 
     1131>>> f.clean(None) 
     1132u'' 
    9151133 
    9161134# Form ########################################################################