Changeset 4113
- Timestamp:
- 11/26/06 18:49:26 (2 years ago)
- Files:
-
- django/trunk/django/newforms/fields.py (modified) (5 diffs)
- django/trunk/tests/regressiontests/forms/tests.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/newforms/fields.py
r4111 r4113 77 77 """ 78 78 super(IntegerField, self).clean(value) 79 if not self.required and value in EMPTY_VALUES: 80 return u'' 79 81 try: 80 82 return int(value) … … 171 173 if value in EMPTY_VALUES: value = u'' 172 174 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): 174 178 raise ValidationError(self.error_message) 175 179 return value … … 247 251 if value in EMPTY_VALUES: value = u'' 248 252 value = smart_unicode(value) 253 if not self.required and value == u'': 254 return value 249 255 valid_values = set([str(k) for k, v in self.choices]) 250 256 if value not in valid_values: … … 260 266 Validates that the input is a list or tuple. 261 267 """ 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 [] 262 272 if not isinstance(value, (list, tuple)): 263 273 raise ValidationError(u'Enter a list of values.') 264 if self.required and not value:265 raise ValidationError(u'This field is required.')266 274 new_value = [] 267 275 for val in value: … … 278 286 def __init__(self, fields=(), required=True, widget=None): 279 287 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 280 293 self.fields = fields 281 294 django/trunk/tests/regressiontests/forms/tests.py
r4111 r4113 478 478 # CharField ################################################################### 479 479 480 >>> f = CharField() 481 >>> f.clean(1) 482 u'1' 483 >>> f.clean('hello') 484 u'hello' 485 >>> f.clean(None) 486 Traceback (most recent call last): 487 ... 488 ValidationError: [u'This field is required.'] 489 >>> f.clean('') 490 Traceback (most recent call last): 491 ... 492 ValidationError: [u'This field is required.'] 493 >>> f.clean([1, 2, 3]) 494 u'[1, 2, 3]' 495 480 496 >>> f = CharField(required=False) 481 497 >>> f.clean(1) … … 519 535 520 536 >>> f = IntegerField() 537 >>> f.clean('') 538 Traceback (most recent call last): 539 ... 540 ValidationError: [u'This field is required.'] 541 >>> f.clean(None) 542 Traceback (most recent call last): 543 ... 544 ValidationError: [u'This field is required.'] 545 >>> f.clean('1') 546 1 547 >>> isinstance(f.clean('1'), int) 548 True 549 >>> f.clean('23') 550 23 551 >>> f.clean('a') 552 Traceback (most recent call last): 553 ... 554 ValidationError: [u'Enter a whole number.'] 555 >>> f.clean('1 ') 556 1 557 >>> f.clean(' 1') 558 1 559 >>> f.clean(' 1 ') 560 1 561 >>> f.clean('1a') 562 Traceback (most recent call last): 563 ... 564 ValidationError: [u'Enter a whole number.'] 565 566 >>> f = IntegerField(required=False) 567 >>> f.clean('') 568 u'' 569 >>> f.clean(None) 570 u'' 521 571 >>> f.clean('1') 522 572 1 … … 682 732 ValidationError: [u'Enter a valid date/time.'] 683 733 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 684 742 # RegexField ################################################################## 685 743 … … 753 811 754 812 >>> f = EmailField() 813 >>> f.clean('') 814 Traceback (most recent call last): 815 ... 816 ValidationError: [u'This field is required.'] 817 >>> f.clean(None) 818 Traceback (most recent call last): 819 ... 820 ValidationError: [u'This field is required.'] 755 821 >>> f.clean('person@example.com') 756 822 u'person@example.com' … … 768 834 ValidationError: [u'Enter a valid e-mail address.'] 769 835 836 >>> f = EmailField(required=False) 837 >>> f.clean('') 838 u'' 839 >>> f.clean(None) 840 u'' 841 >>> f.clean('person@example.com') 842 u'person@example.com' 843 >>> f.clean('foo') 844 Traceback (most recent call last): 845 ... 846 ValidationError: [u'Enter a valid e-mail address.'] 847 >>> f.clean('foo@') 848 Traceback (most recent call last): 849 ... 850 ValidationError: [u'Enter a valid e-mail address.'] 851 >>> f.clean('foo@bar') 852 Traceback (most recent call last): 853 ... 854 ValidationError: [u'Enter a valid e-mail address.'] 855 770 856 # URLField ################################################################## 771 857 772 858 >>> f = URLField() 859 >>> f.clean('') 860 Traceback (most recent call last): 861 ... 862 ValidationError: [u'This field is required.'] 863 >>> f.clean(None) 864 Traceback (most recent call last): 865 ... 866 ValidationError: [u'This field is required.'] 867 >>> f.clean('http://example.com') 868 u'http://example.com' 869 >>> f.clean('http://www.example.com') 870 u'http://www.example.com' 871 >>> f.clean('foo') 872 Traceback (most recent call last): 873 ... 874 ValidationError: [u'Enter a valid URL.'] 875 >>> f.clean('example.com') 876 Traceback (most recent call last): 877 ... 878 ValidationError: [u'Enter a valid URL.'] 879 >>> f.clean('http://') 880 Traceback (most recent call last): 881 ... 882 ValidationError: [u'Enter a valid URL.'] 883 >>> f.clean('http://example') 884 Traceback (most recent call last): 885 ... 886 ValidationError: [u'Enter a valid URL.'] 887 >>> f.clean('http://example.') 888 Traceback (most recent call last): 889 ... 890 ValidationError: [u'Enter a valid URL.'] 891 >>> f.clean('http://.com') 892 Traceback (most recent call last): 893 ... 894 ValidationError: [u'Enter a valid URL.'] 895 896 >>> f = URLField(required=False) 897 >>> f.clean('') 898 u'' 899 >>> f.clean(None) 900 u'' 773 901 >>> f.clean('http://example.com') 774 902 u'http://example.com' … … 821 949 822 950 >>> f = BooleanField() 951 >>> f.clean('') 952 Traceback (most recent call last): 953 ... 954 ValidationError: [u'This field is required.'] 955 >>> f.clean(None) 956 Traceback (most recent call last): 957 ... 958 ValidationError: [u'This field is required.'] 823 959 >>> f.clean(True) 824 960 True … … 832 968 True 833 969 970 >>> f = BooleanField(required=False) 971 >>> f.clean('') 972 False 973 >>> f.clean(None) 974 False 975 >>> f.clean(True) 976 True 977 >>> f.clean(False) 978 False 979 >>> f.clean(1) 980 True 981 >>> f.clean(0) 982 False 983 >>> f.clean('Django rocks') 984 True 985 834 986 # ChoiceField ################################################################# 835 987 836 988 >>> f = ChoiceField(choices=[('1', '1'), ('2', '2')]) 989 >>> f.clean('') 990 Traceback (most recent call last): 991 ... 992 ValidationError: [u'This field is required.'] 993 >>> f.clean(None) 994 Traceback (most recent call last): 995 ... 996 ValidationError: [u'This field is required.'] 837 997 >>> f.clean(1) 838 998 u'1' 839 999 >>> f.clean('1') 840 1000 u'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') 1002 Traceback (most recent call last): 1003 ... 1004 ValidationError: [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('') 1008 u'' 1009 >>> f.clean(None) 1010 u'' 1011 >>> f.clean(1) 1012 u'1' 1013 >>> f.clean('1') 1014 u'1' 849 1015 >>> f.clean('3') 850 1016 Traceback (most recent call last): … … 863 1029 864 1030 >>> f = MultipleChoiceField(choices=[('1', '1'), ('2', '2')]) 1031 >>> f.clean('') 1032 Traceback (most recent call last): 1033 ... 1034 ValidationError: [u'This field is required.'] 1035 >>> f.clean(None) 1036 Traceback (most recent call last): 1037 ... 1038 ValidationError: [u'This field is required.'] 865 1039 >>> f.clean([1]) 866 1040 [u'1'] … … 890 1064 ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] 891 1065 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') 1082 Traceback (most recent call last): 1083 ... 1084 ValidationError: [u'Enter a list of values.'] 1085 >>> f.clean([]) 1086 [] 1087 >>> f.clean(()) 1088 [] 1089 >>> f.clean(['3']) 1090 Traceback (most recent call last): 1091 ... 1092 ValidationError: [u'Select a valid choice. 3 is not one of the available choices.'] 1093 892 1094 # ComboField ################################################################## 893 1095 894 1096 ComboField takes a list of fields that should be used to validate a value, 895 in that order :1097 in that order. 896 1098 >>> f = ComboField(fields=[CharField(max_length=20), EmailField()]) 897 1099 >>> f.clean('test@example.com') … … 913 1115 ... 914 1116 ValidationError: [u'This field is required.'] 1117 1118 >>> f = ComboField(fields=[CharField(max_length=20), EmailField()], required=False) 1119 >>> f.clean('test@example.com') 1120 u'test@example.com' 1121 >>> f.clean('longemailaddress@example.com') 1122 Traceback (most recent call last): 1123 ... 1124 ValidationError: [u'Ensure this value has at most 20 characters.'] 1125 >>> f.clean('not an e-mail') 1126 Traceback (most recent call last): 1127 ... 1128 ValidationError: [u'Enter a valid e-mail address.'] 1129 >>> f.clean('') 1130 u'' 1131 >>> f.clean(None) 1132 u'' 915 1133 916 1134 # Form ########################################################################
