Ticket #6790: error_dict_list_fixes_and_overrides.diff

File error_dict_list_fixes_and_overrides.diff, 7.2 KB (added by oyvind, 17 years ago)
  • django/newforms/forms.py

    diff --git a/django/newforms/forms.py b/django/newforms/forms.py
    index 2c481e4..fb9bdb9 100644
    a b class BaseForm(StrAndUnicode):  
    6464    # information. Any improvements to the form API should be made to *this*
    6565    # class, not to the Form class.
    6666    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
    67                  initial=None, error_class=ErrorList, label_suffix=':'):
     67                 initial=None, error_list=ErrorList, error_dict=ErrorDict, label_suffix=':'):
    6868        self.is_bound = data is not None or files is not None
    6969        self.data = data or {}
    7070        self.files = files or {}
    7171        self.auto_id = auto_id
    7272        self.prefix = prefix
    7373        self.initial = initial or {}
    74         self.error_class = error_class
     74        self.error_list = error_list
     75        self.error_dict = error_dict
    7576        self.label_suffix = label_suffix
    7677        self._errors = None # Stores the errors after clean() has been called.
    7778
    class BaseForm(StrAndUnicode):  
    126127        output, hidden_fields = [], []
    127128        for name, field in self.fields.items():
    128129            bf = BoundField(self, field, name)
    129             bf_errors = self.error_class([escape(error) for error in bf.errors]) # Escape and cache in local variable.
     130            bf_errors = bf.errors # Escape and cache in local variable.
    130131            if bf.is_hidden:
    131132                if bf_errors:
    132133                    top_errors.extend([u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors])
    class BaseForm(StrAndUnicode):  
    182183        field -- i.e., from Form.clean(). Returns an empty ErrorList if there
    183184        are none.
    184185        """
    185         return self.errors.get(NON_FIELD_ERRORS, self.error_class())
     186        return self.errors.get(NON_FIELD_ERRORS, self.error_list())
    186187
    187188    def full_clean(self):
    188189        """
    189190        Cleans all of self.data and populates self._errors and
    190191        self.cleaned_data.
    191192        """
    192         self._errors = ErrorDict()
     193        self._errors = self.error_dict(error_list=self.error_list)
    193194        if not self.is_bound: # Stop further processing.
    194195            return
    195196        self.cleaned_data = {}
    class BaseForm(StrAndUnicode):  
    209210                    value = getattr(self, 'clean_%s' % name)()
    210211                    self.cleaned_data[name] = value
    211212            except ValidationError, e:
    212                 self._errors[name] = e.messages
     213                self._errors[name] = (field.label, e.messages)
    213214                if name in self.cleaned_data:
    214215                    del self.cleaned_data[name]
    215216        try:
    216217            self.cleaned_data = self.clean()
    217218        except ValidationError, e:
    218             self._errors[NON_FIELD_ERRORS] = e.messages
     219            self._errors[NON_FIELD_ERRORS] = (u'', e.messages)
    219220        if self._errors:
    220221            delattr(self, 'cleaned_data')
    221222
    class BoundField(StrAndUnicode):  
    269270        Returns an ErrorList for this field. Returns an empty ErrorList
    270271        if there are none.
    271272        """
    272         return self.form.errors.get(self.name, self.form.error_class())
     273        return self.form.errors.get(self.name, self.form.error_list())
    273274    errors = property(_errors)
    274275
    275276    def as_widget(self, widget=None, attrs=None):
  • django/newforms/util.py

    diff --git a/django/newforms/util.py b/django/newforms/util.py
    index b3edf41..55ce614 100644
    a b def flatatt(attrs):  
    1212    """
    1313    return u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
    1414
    15 class ErrorDict(dict, StrAndUnicode):
     15class ErrorList(list, StrAndUnicode):
    1616    """
    1717    A collection of errors that knows how to display itself in various formats.
    18 
    19     The dictionary keys are the field names, and the values are the errors.
    2018    """
    2119    def __unicode__(self):
    2220        return self.as_ul()
    class ErrorDict(dict, StrAndUnicode):  
    2422    def as_ul(self):
    2523        if not self: return u''
    2624        return mark_safe(u'<ul class="errorlist">%s</ul>'
    27                 % ''.join([u'<li>%s%s</li>' % (k, force_unicode(v))
    28                     for k, v in self.items()]))
     25                % ''.join([u'<li>%s</li>' % escape(smart_unicode(e)) for e in self]))
    2926
    3027    def as_text(self):
    31         return u'\n'.join([u'* %s\n%s' % (k, u'\n'.join([u'  * %s' % force_unicode(i) for i in v])) for k, v in self.items()])
     28        if not self: return u''
     29        return u'\n'.join([u'* %s' % force_unicode(e) for e in self])
    3230
    33 class ErrorList(list, StrAndUnicode):
     31    def __repr__(self):
     32        return repr([force_unicode(e) for e in self])
     33
     34class ErrorDict(dict, StrAndUnicode):
    3435    """
    3536    A collection of errors that knows how to display itself in various formats.
     37
     38    The dictionary keys are the field names, and the values are the errors.
    3639    """
     40    def __init__(self, error_list=ErrorList):
     41        self.error_list = error_list
     42
    3743    def __unicode__(self):
    3844        return self.as_ul()
    3945
     46    def __getitem__(self, key):
     47        value = super(ErrorDict, self).__getitem__(key)
     48        if value:
     49            return self.error_list(value[1])
     50        return self.error_list()
     51
     52    def get(self, key, default=None):
     53        value = super(ErrorDict, self).get(key, default)
     54        if value:
     55            return self.error_list(value[1])
     56        return self.error_list()
     57
    4058    def as_ul(self):
    4159        if not self: return u''
    4260        return mark_safe(u'<ul class="errorlist">%s</ul>'
    43                 % ''.join([u'<li>%s</li>' % force_unicode(e) for e in self]))
     61                % ''.join([u'<li>%s%s</li>' % (v[0] and escape(force_unicode(v[0])) or k, force_unicode(self.error_list(v[1])))
     62                    for k, v in self.items()]))
    4463
    4564    def as_text(self):
    46         if not self: return u''
    47         return u'\n'.join([u'* %s' % force_unicode(e) for e in self])
     65        return u'\n'.join([u'* %s\n%s' % (v[0] and force_unicode(v[0]) or k, u'\n'.join([u'  * %s' % force_unicode(i) for i in v[1]])) for k, v in self.items()])
    4866
    4967    def __repr__(self):
    50         return repr([force_unicode(e) for e in self])
     68        return repr(dict([(k, v[1]) for k, v in self.items()]))
    5169
    5270class ValidationError(Exception):
    5371    def __init__(self, message):
  • django/test/_doctest.py

    diff --git a/django/test/_doctest.py b/django/test/_doctest.py
    index a56483c..6d66a8d 100644
    a b class DocTest:  
    503503        self.globs = globs.copy()
    504504        self.name = name
    505505        self.filename = filename
    506         self.lineno = lineno
     506        self.lineno = lineno or 0
    507507
    508508    def __repr__(self):
    509509        if len(self.examples) == 0:
  • tests/regressiontests/forms/extra.py

    diff --git a/tests/regressiontests/forms/extra.py b/tests/regressiontests/forms/extra.py
    index 9dff407..6b29324 100644
    a b u'sirrobin'  
    369369...     email = EmailField()
    370370...     comment = CharField()
    371371>>> data = dict(email='invalid')
    372 >>> f = CommentForm(data, auto_id=False, error_class=DivErrorList)
     372>>> f = CommentForm(data, auto_id=False, error_list=DivErrorList)
    373373>>> print f.as_p()
    374374<p>Name: <input type="text" name="name" maxlength="50" /></p>
    375375<div class="errorlist"><div class="error">Enter a valid e-mail address.</div></div>
Back to Top