1. Carl Meyer
  2. django-form-utils


Carl Meyer  committed 8c4733b Merge

Merge from agriffis to add row_attrs error class; added test.

  • Participants
  • Parent commits 89db8f5, fc604c3
  • Branches default

Comments (0)

Files changed (3)

File README.rst

View file
  • Ignore whitespace
 none"``), and will be available as the ``row_attrs`` attribute of the
-Also, a CSS class of "required" or "optional" will automatically be
-added to the row_attrs of each ``BoundField``, depending on whether
-the field is required.
+A ``BetterForm`` or ``BetterModelForm`` will add a CSS class of
+"required" or "optional" automatically to the row_attrs of each
+``BoundField`` depending on whether the field is required, and will
+also add a CSS class of "error" if the field has errors.

File form_utils/forms.py

View file
  • Ignore whitespace
         req_class = 'required'
         req_class = 'optional'
+    if bf.errors:
+        req_class += ' error'
     if 'class' in row_attrs:
         row_attrs['class'] = row_attrs['class'] + ' ' + req_class

File tests/tests.py

View file
  • Ignore whitespace
     class Meta:
         row_attrs = {'honeypot': {'style': 'display: none'}}
+    def clean_honeypot(self):
+        if self.cleaned_data.get("honeypot"):
+            raise forms.ValidationError("Honeypot field must be empty.")
 class PersonForm(BetterModelForm):
                           u' style="display: none" class="required"')
+    def test_row_attrs_error_class(self):
+        """
+        row_attrs adds an error class if a field has errors.
+        """
+        form = HoneypotForm({"honeypot": "something"})
+        self.assertEqual(form["honeypot"].row_attrs,
+                         u' style="display: none" class="required error"')
     def test_friendly_typo_error(self):
         If we define a single fieldset and leave off the trailing , in