Commits

prencher  committed ded629b Merge
  • Participants
  • Parent commits 144952f, d002b15

Comments (0)

Files changed (3)

 -----------
 Not yet released.
 
+- Validation overhaul: Fields are now responsible for their own validation,
+  instead of mostly relying on Form. There are also new pre_validate and
+  post_validate hooks on subfields, adding a great deal of flexibility when
+  dealing with field-level validation. Note that this is an API breaking change
+  if you have any subfields that override `Field.validate`. These will need to
+  be updated to use the new hooks.
+
 - wtforms.ext.django has been overhauled, both to mirror features and changes
   of the Django 1.0 release, and to add some useful fields for working with
   django ORM data in forms.

File tests/form.py

+#!/usr/bin/env python
+"""
+    form
+    ~~~~
+    
+    Unittests for Form.
+    
+    :copyright: 2009 by James Crasta, Thomas Johansson.
+    :license: MIT, see LICENSE.txt for details.
+"""
+
+from unittest import TestCase
+from wtforms import Form, TextField
+from wtforms.validators import ValidationError
+
+class FormTest(TestCase):
+    class F(Form):
+        test = TextField()
+        def validate_test(form, field):
+            if field.data != 'foobar':
+                raise ValidationError('error')
+
+    def test_validate(self):
+        form = self.F(test='foobar')
+        self.assertEqual(form.validate(), True)
+        
+        form = self.F()
+        self.assertEqual(form.validate(), False)
+
+    def test_data_proxy(self):
+        form = self.F(test='foo')
+        self.assertEqual(form.data, {'test': 'foo'})
+
+    def test_errors_proxy(self):
+        form = self.F(test='foobar')
+        form.validate()
+        self.assertEqual(form.errors, {})
+        
+        form = self.F()
+        form.validate()
+        self.assertEqual(form.errors, {'test': ['error']})
+
+if __name__ == '__main__':
+    from unittest import main
+    main()

File tests/runtests.py

     :license: MIT, see LICENSE.txt for details.
 """
 
-TESTS = ['fields', 'validators']
+TESTS = ['form', 'fields', 'validators']
 
 
 from unittest import defaultTestLoader, TextTestRunner, TestSuite