From 11f0899bbe7c04dc6109f57c3b36ed8621b8f08e Mon Sep 17 00:00:00 2001 From: Nick Presta Date: Mon, 14 Apr 2014 23:58:51 -0400 Subject: [PATCH] Fixed #11776 -- Added CSS class for non-field/top of form errors. Thanks Daniel Pope for the suggestion. --- AUTHORS | 1 + django/forms/forms.py | 7 +- django/forms/utils.py | 12 ++- docs/releases/1.8.txt | 4 + docs/topics/forms/index.txt | 11 +++ tests/admin_inlines/tests.py | 2 +- tests/admin_views/tests.py | 4 +- .../forms_tests/tests/test_error_messages.py | 2 +- tests/forms_tests/tests/test_forms.py | 90 +++++++++++++++++-- tests/forms_tests/tests/test_regressions.py | 4 +- 10 files changed, 120 insertions(+), 17 deletions(-) diff --git a/AUTHORS b/AUTHORS index 534e3e0f529..472d53c999b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -510,6 +510,7 @@ answer newbie questions, and generally made Django that much better: polpak@yahoo.com Ross Poulton Mihai Preda + Nick Presta Matthias Pronk Jyrki Pulliainen Thejaswi Puthraya diff --git a/django/forms/forms.py b/django/forms/forms.py index b5cf17f4eb1..347eccab4bd 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -280,7 +280,7 @@ class BaseForm(object): 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_class(error_class='nonfield')) def _raw_value(self, fieldname): """ @@ -331,7 +331,10 @@ class BaseForm(object): if field != NON_FIELD_ERRORS and field not in self.fields: raise ValueError( "'%s' has no field named '%s'." % (self.__class__.__name__, field)) - self._errors[field] = self.error_class() + if field == NON_FIELD_ERRORS: + self._errors[field] = self.error_class(error_class='nonfield') + else: + self._errors[field] = self.error_class() self._errors[field].extend(error_list) if field in self.cleaned_data: del self.cleaned_data[field] diff --git a/django/forms/utils.py b/django/forms/utils.py index f7f53e9a0a7..14253f36fec 100644 --- a/django/forms/utils.py +++ b/django/forms/utils.py @@ -80,6 +80,14 @@ class ErrorList(UserList, list): """ A collection of errors that knows how to display itself in various formats. """ + def __init__(self, initlist=None, error_class=None): + super(ErrorList, self).__init__(initlist) + + if error_class is None: + self.error_class = 'errorlist' + else: + self.error_class = 'errorlist {}'.format(error_class) + def as_data(self): return ValidationError(self.data).error_list @@ -99,8 +107,10 @@ class ErrorList(UserList, list): def as_ul(self): if not self.data: return '' + return format_html( - '
    {0}
', + '
    {1}
', + self.error_class, format_html_join('', '
  • {0}
  • ', ((force_text(e),) for e in self)) ) diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 056ea86bafe..f716c995caf 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -129,6 +129,10 @@ Forms the ``