diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 2c481e4..fb9bdb9 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -64,14 +64,15 @@ class BaseForm(StrAndUnicode): # information. Any improvements to the form API should be made to *this* # class, not to the Form class. def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, - initial=None, error_class=ErrorList, label_suffix=':'): + initial=None, error_list=ErrorList, error_dict=ErrorDict, label_suffix=':'): self.is_bound = data is not None or files is not None self.data = data or {} self.files = files or {} self.auto_id = auto_id self.prefix = prefix self.initial = initial or {} - self.error_class = error_class + self.error_list = error_list + self.error_dict = error_dict self.label_suffix = label_suffix self._errors = None # Stores the errors after clean() has been called. @@ -126,7 +127,7 @@ class BaseForm(StrAndUnicode): output, hidden_fields = [], [] for name, field in self.fields.items(): bf = BoundField(self, field, name) - bf_errors = self.error_class([escape(error) for error in bf.errors]) # Escape and cache in local variable. + bf_errors = bf.errors # Escape and cache in local variable. if bf.is_hidden: if bf_errors: top_errors.extend([u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors]) @@ -182,14 +183,14 @@ class BaseForm(StrAndUnicode): field -- i.e., from Form.clean(). Returns an empty ErrorList if there are none. """ - return self.errors.get(NON_FIELD_ERRORS, self.error_class()) + return self.errors.get(NON_FIELD_ERRORS, self.error_list()) def full_clean(self): """ Cleans all of self.data and populates self._errors and self.cleaned_data. """ - self._errors = ErrorDict() + self._errors = self.error_dict(error_list=self.error_list) if not self.is_bound: # Stop further processing. return self.cleaned_data = {} @@ -209,13 +210,13 @@ class BaseForm(StrAndUnicode): value = getattr(self, 'clean_%s' % name)() self.cleaned_data[name] = value except ValidationError, e: - self._errors[name] = e.messages + self._errors[name] = (field.label, e.messages) if name in self.cleaned_data: del self.cleaned_data[name] try: self.cleaned_data = self.clean() except ValidationError, e: - self._errors[NON_FIELD_ERRORS] = e.messages + self._errors[NON_FIELD_ERRORS] = (u'', e.messages) if self._errors: delattr(self, 'cleaned_data') @@ -269,7 +270,7 @@ class BoundField(StrAndUnicode): Returns an ErrorList for this field. Returns an empty ErrorList if there are none. """ - return self.form.errors.get(self.name, self.form.error_class()) + return self.form.errors.get(self.name, self.form.error_list()) errors = property(_errors) def as_widget(self, widget=None, attrs=None): diff --git a/django/newforms/util.py b/django/newforms/util.py index b3edf41..55ce614 100644 --- a/django/newforms/util.py +++ b/django/newforms/util.py @@ -12,11 +12,9 @@ def flatatt(attrs): """ return u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()]) -class ErrorDict(dict, StrAndUnicode): +class ErrorList(list, StrAndUnicode): """ A collection of errors that knows how to display itself in various formats. - - The dictionary keys are the field names, and the values are the errors. """ def __unicode__(self): return self.as_ul() @@ -24,30 +22,50 @@ class ErrorDict(dict, StrAndUnicode): def as_ul(self): if not self: return u'' return mark_safe(u'
Name: