Commit 11f0899b authored by Nick Presta's avatar Nick Presta Committed by Tim Graham
Browse files

Fixed #11776 -- Added CSS class for non-field/top of form errors.

Thanks Daniel Pope for the suggestion.
parent a00efa30
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -510,6 +510,7 @@ answer newbie questions, and generally made Django that much better:
    polpak@yahoo.com
    Ross Poulton <ross@rossp.org>
    Mihai Preda <mihai_preda@yahoo.com>
    Nick Presta <nick@nickpresta.ca>
    Matthias Pronk <django@masida.nl>
    Jyrki Pulliainen <jyrki.pulliainen@gmail.com>
    Thejaswi Puthraya <thejaswi.puthraya@gmail.com>
+5 −2
Original line number Diff line number Diff line
@@ -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,6 +331,9 @@ 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))
                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:
+11 −1
Original line number Diff line number Diff line
@@ -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(
            '<ul class="errorlist">{0}</ul>',
            '<ul class="{0}">{1}</ul>',
            self.error_class,
            format_html_join('', '<li>{0}</li>', ((force_text(e),) for e in self))
        )

+4 −0
Original line number Diff line number Diff line
@@ -129,6 +129,10 @@ Forms
  the ``<label>`` tags for required fields will have this class present in its
  attributes.

* The rendering of non-field errors in unordered lists (``<ul>``) now includes
  ``nonfield`` in its list of classes to distinguish them from field-specific
  errors.

* :class:`~django.forms.Field` now accepts a
  :attr:`~django.forms.Field.label_suffix` argument, which will override the
  form's :attr:`~django.forms.Form.label_suffix`. This enables customizing the
+11 −0
Original line number Diff line number Diff line
@@ -292,6 +292,17 @@ over them::
        </ol>
    {% endif %}

.. versionchanged:: 1.8

Non-field errors (and/or hidden field errors that are rendered at the top of
the form when using helpers like ``form.as_p()``) will be rendered with an
additional class of ``nonfield`` to help distinguish them from field-specific
errors. For example, ``{{ form.non_field_errors }}`` would look like::

    <ul class="errorlist nonfield">
        <li>Generic validation error</li>
    </ul>

Looping over the form's fields
------------------------------

Loading