Code

Ticket #4752: div-form.2.diff

File div-form.2.diff, 3.4 KB (added by michal@…, 7 years ago)

Patch that adds a parameter to BaseForm.init constructor to allow using arbitrary ErrorList class implementation

Line 
1Index: django/newforms/forms.py
2===================================================================
3--- django/newforms/forms.py    (revision 5591)
4+++ django/newforms/forms.py    (working copy)
5@@ -57,12 +57,13 @@
6     # class is different than Form. See the comments by the Form class for more
7     # information. Any improvements to the form API should be made to *this*
8     # class, not to the Form class.
9-    def __init__(self, data=None, auto_id='id_%s', prefix=None, initial=None):
10+    def __init__(self, data=None, auto_id='id_%s', prefix=None, initial=None, error_list_class = ErrorList):
11         self.is_bound = data is not None
12         self.data = data or {}
13         self.auto_id = auto_id
14         self.prefix = prefix
15         self.initial = initial or {}
16+        self.error_list_class = error_list_class
17         self._errors = None # Stores the errors after clean() has been called.
18 
19         # The base_fields class attribute is the *class-wide* definition of
20@@ -77,7 +78,7 @@
21 
22     def __iter__(self):
23         for name, field in self.fields.items():
24-            yield BoundField(self, field, name)
25+            yield BoundField(self, field, name, self.error_list_class)
26 
27     def __getitem__(self, name):
28         "Returns a BoundField with the given name."
29@@ -85,7 +86,7 @@
30             field = self.fields[name]
31         except KeyError:
32             raise KeyError('Key %r not found in Form' % name)
33-        return BoundField(self, field, name)
34+        return BoundField(self, field, name, self.error_list_class)
35 
36     def _get_errors(self):
37         "Returns an ErrorDict for self.data"
38@@ -115,8 +116,8 @@
39         top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
40         output, hidden_fields = [], []
41         for name, field in self.fields.items():
42-            bf = BoundField(self, field, name)
43-            bf_errors = ErrorList([escape(error) for error in bf.errors]) # Escape and cache in local variable.
44+            bf = BoundField(self, field, name, self.error_list_class)
45+            bf_errors = self.error_list_class([escape(error) for error in bf.errors]) # Escape and cache in local variable.
46             if bf.is_hidden:
47                 if bf_errors:
48                     top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors])
49@@ -167,7 +168,7 @@
50         field -- i.e., from Form.clean(). Returns an empty ErrorList if there
51         are none.
52         """
53-        return self.errors.get(NON_FIELD_ERRORS, ErrorList())
54+        return self.errors.get(NON_FIELD_ERRORS, self.error_list_class())
55 
56     def full_clean(self):
57         """
58@@ -220,11 +221,12 @@
59 
60 class BoundField(StrAndUnicode):
61     "A Field plus data"
62-    def __init__(self, form, field, name):
63+    def __init__(self, form, field, name, error_list_class = ErrorList):
64         self.form = form
65         self.field = field
66         self.name = name
67         self.html_name = form.add_prefix(name)
68+        self.error_list_class = error_list_class
69         if self.field.label is None:
70             self.label = pretty_name(name)
71         else:
72@@ -248,7 +250,7 @@
73         Returns an ErrorList for this field. Returns an empty ErrorList
74         if there are none.
75         """
76-        return self.form.errors.get(self.name, ErrorList())
77+        return self.form.errors.get(self.name, self.error_list_class())
78     errors = property(_errors)
79 
80     def as_widget(self, widget, attrs=None):