diff -r d56aac597a04 django/newforms/fields.py
a
|
b
|
|
19 | 19 | from django.utils.translation import ugettext_lazy as _ |
20 | 20 | from django.utils.encoding import StrAndUnicode, smart_unicode, smart_str |
21 | 21 | |
22 | | from util import ErrorList, ValidationError |
| 22 | from util import ErrorList, ValidationError, build_class_error_messages |
23 | 23 | from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput |
24 | 24 | |
25 | 25 | |
… |
… |
|
83 | 83 | self.creation_counter = Field.creation_counter |
84 | 84 | Field.creation_counter += 1 |
85 | 85 | |
86 | | def set_class_error_messages(messages, klass): |
87 | | for base_class in klass.__bases__: |
88 | | set_class_error_messages(messages, base_class) |
89 | | messages.update(getattr(klass, 'default_error_messages', {})) |
90 | | |
91 | | messages = {} |
92 | | set_class_error_messages(messages, self.__class__) |
93 | | messages.update(error_messages or {}) |
94 | | self.error_messages = messages |
| 86 | self.error_messages = build_class_error_messages(self.__class__, |
| 87 | error_messages) |
95 | 88 | |
96 | 89 | def clean(self, value): |
97 | 90 | """ |
diff -r d56aac597a04 django/newforms/forms.py
a
|
b
|
|
12 | 12 | from fields import Field, FileField |
13 | 13 | from widgets import TextInput, Textarea |
14 | 14 | from util import flatatt, ErrorDict, ErrorList, ValidationError |
| 15 | from util import build_class_error_messages |
15 | 16 | |
16 | 17 | __all__ = ('BaseForm', 'Form') |
17 | 18 | |
… |
… |
|
64 | 65 | # information. Any improvements to the form API should be made to *this* |
65 | 66 | # class, not to the Form class. |
66 | 67 | def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, |
67 | | initial=None, error_class=ErrorList, label_suffix=':'): |
| 68 | initial=None, error_class=ErrorList, label_suffix=':', |
| 69 | error_messages=None): |
68 | 70 | self.is_bound = data is not None or files is not None |
69 | 71 | self.data = data or {} |
70 | 72 | self.files = files or {} |
… |
… |
|
81 | 83 | # Instances should always modify self.fields; they should not modify |
82 | 84 | # self.base_fields. |
83 | 85 | self.fields = deepcopy(self.base_fields) |
| 86 | |
| 87 | self.error_messages = build_class_error_messages(self.__class__, |
| 88 | error_messages) |
84 | 89 | |
85 | 90 | def __unicode__(self): |
86 | 91 | return self.as_table() |
diff -r d56aac597a04 django/newforms/util.py
a
|
b
|
|
67 | 67 | # AttributeError: ValidationError instance has no attribute 'args' |
68 | 68 | # See http://www.python.org/doc/current/tut/node10.html#handling |
69 | 69 | return repr(self.messages) |
| 70 | |
| 71 | def build_class_error_messages(klass, error_messages): |
| 72 | """ |
| 73 | Builds a dict of error messages from the ``default_error_messages`` |
| 74 | attributes of the given class, and all base classes. |
| 75 | """ |
| 76 | def set_class_error_messages(messages, klass): |
| 77 | for base_class in klass.__bases__: |
| 78 | set_class_error_messages(messages, base_class) |
| 79 | messages.update(getattr(klass, 'default_error_messages', {})) |
| 80 | |
| 81 | messages = {} |
| 82 | set_class_error_messages(messages, klass) |
| 83 | messages.update(error_messages or {}) |
| 84 | return messages |