Ticket #4752: div-form.2.diff
File div-form.2.diff, 3.4 KB (added by , 17 years ago) |
---|
-
django/newforms/forms.py
57 57 # class is different than Form. See the comments by the Form class for more 58 58 # information. Any improvements to the form API should be made to *this* 59 59 # class, not to the Form class. 60 def __init__(self, data=None, auto_id='id_%s', prefix=None, initial=None ):60 def __init__(self, data=None, auto_id='id_%s', prefix=None, initial=None, error_list_class = ErrorList): 61 61 self.is_bound = data is not None 62 62 self.data = data or {} 63 63 self.auto_id = auto_id 64 64 self.prefix = prefix 65 65 self.initial = initial or {} 66 self.error_list_class = error_list_class 66 67 self._errors = None # Stores the errors after clean() has been called. 67 68 68 69 # The base_fields class attribute is the *class-wide* definition of … … 77 78 78 79 def __iter__(self): 79 80 for name, field in self.fields.items(): 80 yield BoundField(self, field, name )81 yield BoundField(self, field, name, self.error_list_class) 81 82 82 83 def __getitem__(self, name): 83 84 "Returns a BoundField with the given name." … … 85 86 field = self.fields[name] 86 87 except KeyError: 87 88 raise KeyError('Key %r not found in Form' % name) 88 return BoundField(self, field, name )89 return BoundField(self, field, name, self.error_list_class) 89 90 90 91 def _get_errors(self): 91 92 "Returns an ErrorDict for self.data" … … 115 116 top_errors = self.non_field_errors() # Errors that should be displayed above all fields. 116 117 output, hidden_fields = [], [] 117 118 for name, field in self.fields.items(): 118 bf = BoundField(self, field, name )119 bf_errors = ErrorList([escape(error) for error in bf.errors]) # Escape and cache in local variable.119 bf = BoundField(self, field, name, self.error_list_class) 120 bf_errors = self.error_list_class([escape(error) for error in bf.errors]) # Escape and cache in local variable. 120 121 if bf.is_hidden: 121 122 if bf_errors: 122 123 top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors]) … … 167 168 field -- i.e., from Form.clean(). Returns an empty ErrorList if there 168 169 are none. 169 170 """ 170 return self.errors.get(NON_FIELD_ERRORS, ErrorList())171 return self.errors.get(NON_FIELD_ERRORS, self.error_list_class()) 171 172 172 173 def full_clean(self): 173 174 """ … … 220 221 221 222 class BoundField(StrAndUnicode): 222 223 "A Field plus data" 223 def __init__(self, form, field, name ):224 def __init__(self, form, field, name, error_list_class = ErrorList): 224 225 self.form = form 225 226 self.field = field 226 227 self.name = name 227 228 self.html_name = form.add_prefix(name) 229 self.error_list_class = error_list_class 228 230 if self.field.label is None: 229 231 self.label = pretty_name(name) 230 232 else: … … 248 250 Returns an ErrorList for this field. Returns an empty ErrorList 249 251 if there are none. 250 252 """ 251 return self.form.errors.get(self.name, ErrorList())253 return self.form.errors.get(self.name, self.error_list_class()) 252 254 errors = property(_errors) 253 255 254 256 def as_widget(self, widget, attrs=None):