Commits

Carl Meyer committed 8c4733b Merge

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

  • Participants
  • Parent commits 89db8f5, fc604c3

Comments (0)

Files changed (3)

 none"``), and will be available as the ``row_attrs`` attribute of the
 ``BoundField``.
 
-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.
 
 Rendering
 '''''''''

form_utils/forms.py

         req_class = 'required'
     else:
         req_class = 'optional'
+    if bf.errors:
+        req_class += ' error'
     if 'class' in row_attrs:
         row_attrs['class'] = row_attrs['class'] + ' ' + req_class
     else:
     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):
     """
         self.assertEquals(honeypot.row_attrs,
                           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