Ticket #3026: newforms_errors.2.patch
File newforms_errors.2.patch, 10.9 KB (added by , 18 years ago) |
---|
-
django/newforms/forms.py
24 24 __metaclass__ = DeclarativeFieldsMetaclass 25 25 26 26 def __init__(self, data=None, auto_id=False): # TODO: prefix stuff 27 self.ignore_errors = data is None 27 28 self.data = data or {} 28 29 self.auto_id = auto_id 29 30 self.clean_data = None # Stores the data after clean() has been called. … … 44 45 raise KeyError('Key %r not found in Form' % name) 45 46 return BoundField(self, field, name) 46 47 47 def clean(self): 48 if self.__errors is None: 49 self.full_clean() 50 return self.clean_data 51 52 def errors(self): 48 def _errors(self): 53 49 "Returns an ErrorDict for self.data" 54 50 if self.__errors is None: 55 51 self.full_clean() 56 return self.__errors 52 if self.ignore_errors: 53 return ErrorDict() 54 else: 55 return self.__errors 56 errors = property(_errors) 57 57 58 58 def is_valid(self): 59 59 """ 60 60 Returns True if the form has no errors. Otherwise, False. This exists 61 61 solely for convenience, so client code can use positive logic rather 62 than confusing negative logic ("if not form.errors ()").62 than confusing negative logic ("if not form.errors"). 63 63 """ 64 return not bool(self.errors ())64 return not bool(self.errors) 65 65 66 66 def as_table(self): 67 "Returns this form rendered as HTML <tr>s -- excluding the <table></table>." 68 return u'\n'.join(['<tr><td>%s:</td><td>%s</td></tr>' % (pretty_name(name), BoundField(self, field, name)) for name, field in self.fields.items()]) 69 70 def as_ul(self): 71 "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>." 72 return u'\n'.join(['<li>%s: %s</li>' % (pretty_name(name), BoundField(self, field, name)) for name, field in self.fields.items()]) 73 74 def as_table_with_errors(self): 75 "Returns this form rendered as HTML <tr>s, with errors." 67 "Returns this form rendered as HTML <tr>s -- excluding the <table></table>, with errors." 76 68 output = [] 77 if self.errors ().get(NON_FIELD_ERRORS):69 if self.errors.get(NON_FIELD_ERRORS): 78 70 # Errors not corresponding to a particular field are displayed at the top. 79 output.append('<tr><td colspan="2"><ul>%s</ul></td></tr>' % '\n'.join(['<li>%s</li>' % e for e in self.errors ()[NON_FIELD_ERRORS]]))71 output.append('<tr><td colspan="2"><ul>%s</ul></td></tr>' % '\n'.join(['<li>%s</li>' % e for e in self.errors[NON_FIELD_ERRORS]])) 80 72 for name, field in self.fields.items(): 81 73 bf = BoundField(self, field, name) 82 74 if bf.errors: … … 84 76 output.append('<tr><td>%s:</td><td>%s</td></tr>' % (pretty_name(name), bf)) 85 77 return u'\n'.join(output) 86 78 87 def as_ul _with_errors(self):88 "Returns this form rendered as HTML <li>s , with errors."79 def as_ul(self): 80 "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>, with errors." 89 81 output = [] 90 if self.errors ().get(NON_FIELD_ERRORS):82 if self.errors.get(NON_FIELD_ERRORS): 91 83 # Errors not corresponding to a particular field are displayed at the top. 92 output.append('<li><ul>%s</ul></li>' % '\n'.join(['<li>%s</li>' % e for e in self.errors ()[NON_FIELD_ERRORS]]))84 output.append('<li><ul>%s</ul></li>' % '\n'.join(['<li>%s</li>' % e for e in self.errors[NON_FIELD_ERRORS]])) 93 85 for name, field in self.fields.items(): 94 86 bf = BoundField(self, field, name) 95 87 line = '<li>' … … 149 141 if there are none. 150 142 """ 151 143 try: 152 return self._form.errors ()[self._name]144 return self._form.errors[self._name] 153 145 except KeyError: 154 146 return ErrorList() 155 147 errors = property(_errors) -
tests/regressiontests/forms/tests.py
879 879 ... first_name = CharField() 880 880 ... last_name = CharField() 881 881 ... birthday = DateField() 882 >>> p = Person() 882 >>> p_new = Person() # Not passed data, so no validation will take place. 883 >>> p = Person({}) # Even passing an empty dictionary will cause validation. 884 >>> print p_new 885 <tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr> 886 <tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr> 887 <tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr> 883 888 >>> print p 889 <tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> 884 890 <tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr> 891 <tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> 885 892 <tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr> 893 <tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> 886 894 <tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr> 887 >>> print p.as_table() 895 896 >>> print p_new.as_table() 888 897 <tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr> 889 898 <tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr> 890 899 <tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr> 891 >>> print p.as_ul() 892 <li>First name: <input type="text" name="first_name" /></li> 893 <li>Last name: <input type="text" name="last_name" /></li> 894 <li>Birthday: <input type="text" name="birthday" /></li> 895 >>> print p.as_table_with_errors() 900 >>> print p.as_table() 896 901 <tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> 897 902 <tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr> 898 903 <tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> 899 904 <tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr> 900 905 <tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> 901 906 <tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr> 902 >>> print p.as_ul_with_errors() 907 908 >>> print p_new.as_ul() 909 <li>First name: <input type="text" name="first_name" /></li> 910 <li>Last name: <input type="text" name="last_name" /></li> 911 <li>Birthday: <input type="text" name="birthday" /></li> 912 >>> print p.as_ul() 903 913 <li><ul><li>This field is required.</li></ul>First name: <input type="text" name="first_name" /></li> 904 914 <li><ul><li>This field is required.</li></ul>Last name: <input type="text" name="last_name" /></li> 905 915 <li><ul><li>This field is required.</li></ul>Birthday: <input type="text" name="birthday" /></li> 906 916 907 917 >>> p = Person({'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9'}) 908 >>> p.errors ()918 >>> p.errors 909 919 {} 910 920 >>> p.is_valid() 911 921 True 912 >>> p.errors ().as_ul()922 >>> p.errors.as_ul() 913 923 u'' 914 >>> p.errors ().as_text()924 >>> p.errors.as_text() 915 925 u'' 916 926 >>> p.clean() 917 927 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} … … 932 942 <tr><td>Birthday:</td><td><input type="text" name="birthday" value="1940-10-9" /></td></tr> 933 943 934 944 >>> p = Person({'last_name': u'Lennon'}) 935 >>> p.errors ()945 >>> p.errors 936 946 {'first_name': [u'This field is required.'], 'birthday': [u'This field is required.']} 937 947 >>> p.is_valid() 938 948 False 939 >>> p.errors ().as_ul()949 >>> p.errors.as_ul() 940 950 u'<ul class="errorlist"><li>first_name<ul class="errorlist"><li>This field is required.</li></ul></li><li>birthday<ul class="errorlist"><li>This field is required.</li></ul></li></ul>' 941 >>> print p.errors ().as_text()951 >>> print p.errors.as_text() 942 952 * first_name 943 953 * This field is required. 944 954 * birthday … … 1109 1119 ... if self.clean_data.get('password1') and self.clean_data.get('password2') and self.clean_data['password1'] != self.clean_data['password2']: 1110 1120 ... raise ValidationError(u'Please make sure your passwords match.') 1111 1121 ... return self.clean_data['password2'] 1122 1123 Forms which are not passed any data will not contain error messages. 1112 1124 >>> f = UserRegistration() 1113 >>> f.errors() 1125 >>> f.errors 1126 {} 1127 1128 If you wish to receive error messages, pass the form an empty dictionary. 1129 >>> f = UserRegistration({}) 1130 >>> f.errors 1114 1131 {'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']} 1132 1115 1133 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}) 1116 >>> f.errors ()1134 >>> f.errors 1117 1135 {'password2': [u'Please make sure your passwords match.']} 1118 1136 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}) 1119 >>> f.errors ()1137 >>> f.errors 1120 1138 {} 1121 1139 >>> f.clean() 1122 1140 {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} … … 1134 1152 ... if self.clean_data.get('password1') and self.clean_data.get('password2') and self.clean_data['password1'] != self.clean_data['password2']: 1135 1153 ... raise ValidationError(u'Please make sure your passwords match.') 1136 1154 ... return self.clean_data 1137 >>> f = UserRegistration() 1138 >>> print f.as_table() 1139 <tr><td>Username:</td><td><input type="text" name="username" /></td></tr> 1140 <tr><td>Password1:</td><td><input type="password" name="password1" /></td></tr> 1141 <tr><td>Password2:</td><td><input type="password" name="password2" /></td></tr> 1142 >>> f.errors() 1155 >>> f = UserRegistration({}) 1156 >>> f.errors 1143 1157 {'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']} 1144 1158 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}) 1145 >>> f.errors ()1159 >>> f.errors 1146 1160 {'__all__': [u'Please make sure your passwords match.']} 1147 1161 >>> print f.as_table() 1148 <tr><td>Username:</td><td><input type="text" name="username" value="adrian" /></td></tr>1149 <tr><td>Password1:</td><td><input type="password" name="password1" value="foo" /></td></tr>1150 <tr><td>Password2:</td><td><input type="password" name="password2" value="bar" /></td></tr>1151 >>> print f.as_table_with_errors()1152 1162 <tr><td colspan="2"><ul><li>Please make sure your passwords match.</li></ul></td></tr> 1153 1163 <tr><td>Username:</td><td><input type="text" name="username" value="adrian" /></td></tr> 1154 1164 <tr><td>Password1:</td><td><input type="password" name="password1" value="foo" /></td></tr> 1155 1165 <tr><td>Password2:</td><td><input type="password" name="password2" value="bar" /></td></tr> 1156 >>> print f.as_ul _with_errors()1166 >>> print f.as_ul() 1157 1167 <li><ul><li>Please make sure your passwords match.</li></ul></li> 1158 1168 <li>Username: <input type="text" name="username" value="adrian" /></li> 1159 1169 <li>Password1: <input type="password" name="password1" value="foo" /></li> 1160 1170 <li>Password2: <input type="password" name="password2" value="bar" /></li> 1161 1171 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}) 1162 >>> f.errors ()1172 >>> f.errors 1163 1173 {} 1164 1174 >>> f.clean() 1165 1175 {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'}