Ticket #6737: forms-error-messages.patch

File forms-error-messages.patch, 3.4 KB (added by nicklane, 17 years ago)
  • django/newforms/fields.py

    diff -r d56aac597a04 django/newforms/fields.py
    a b  
    1919from django.utils.translation import ugettext_lazy as _
    2020from django.utils.encoding import StrAndUnicode, smart_unicode, smart_str
    2121
    22 from util import ErrorList, ValidationError
     22from util import ErrorList, ValidationError, build_class_error_messages
    2323from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput
    2424
    2525
     
    8383        self.creation_counter = Field.creation_counter
    8484        Field.creation_counter += 1
    8585
    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)
    9588
    9689    def clean(self, value):
    9790        """
  • django/newforms/forms.py

    diff -r d56aac597a04 django/newforms/forms.py
    a b  
    1212from fields import Field, FileField
    1313from widgets import TextInput, Textarea
    1414from util import flatatt, ErrorDict, ErrorList, ValidationError
     15from util import build_class_error_messages
    1516
    1617__all__ = ('BaseForm', 'Form')
    1718
     
    6465    # information. Any improvements to the form API should be made to *this*
    6566    # class, not to the Form class.
    6667    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):
    6870        self.is_bound = data is not None or files is not None
    6971        self.data = data or {}
    7072        self.files = files or {}
     
    8183        # Instances should always modify self.fields; they should not modify
    8284        # self.base_fields.
    8385        self.fields = deepcopy(self.base_fields)
     86
     87        self.error_messages = build_class_error_messages(self.__class__,
     88            error_messages)
    8489
    8590    def __unicode__(self):
    8691        return self.as_table()
  • django/newforms/util.py

    diff -r d56aac597a04 django/newforms/util.py
    a b  
    6767        # AttributeError: ValidationError instance has no attribute 'args'
    6868        # See http://www.python.org/doc/current/tut/node10.html#handling
    6969        return repr(self.messages)
     70
     71def 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
Back to Top