Commits

Chris McDonough  committed 7f6c471

use bare Invalid exception from colander civilian-exceptions branch

  • Participants
  • Parent commits 4eff770

Comments (0)

Files changed (3)

File pyramid_simpleform/form.py

 
         self.is_validated = False
 
-        self.errors = {}
+        self.errors = None
         self.data = {}
 
-        self.non_field_errors = []
-
         if defaults:
             self.data.update(defaults)
 
                 if hasattr(obj, f):
                     self.data[f] = getattr(obj, f) 
 
-    def is_error(self, field):
-        """
-        Checks if individual field has errors.
-        """
-        return field in self.errors
-
-    def all_errors(self):
-        """
-        Returns all errors in a single list.
-        """
-        errors = self.non_field_errors[:]
-        for node in self.schema:
-            errors += self.errors_for(node.name)
-        return errors
-
-    def errors_for(self, field):
-        """
-        Returns any errors for a given field as a list.
-        """
-        errors = self.errors.get(field, [])
-        if isinstance(errors, basestring):
-            errors = [errors]
-        return errors
-
     def validate(self, force_validate=False, params=None):
         """
         Runs validation and returns True/False whether form is 
         """
 
         if self.is_validated:
-            return not(self.errors)
+            return self.errors is None
 
         if not force_validate:
             if self.method and self.method != self.request.method:
             try:
                 self.data = self.schema.deserialize(params)
             except colander.Invalid, e:
-                self.errors = e.asdict()
+                self.errors = e
 
         self.is_validated = True
 
-        return not(self.errors)
+        return self.errors is None
 
     def bind(self, obj, include=None, exclude=None):
         """

File pyramid_simpleform/renderers.py

             self._get_id(id, name), 
             **attrs)
 
-    def is_error(self, name):
+    @property
+    def errors(self):
         """
-        Shortcut for **self.form.is_error(name)**
+        Shortcut for **self.form.errors**
         """
-        return self.form.is_error(name)
-
-    def errors_for(self, name):
-        """
-        Shortcut for **self.form.errors_for(name)**
-        """
-        return self.form.errors_for(name)
-
-    def all_errors(self):
-        """
-        Shortcut for **self.form.all_errors()**
-        """
-        return self.form.all_errors()
-
-    def errorlist(self, name=None, **attrs):
-        """
-        Renders errors in a <ul> element. Unless specified in attrs, class
-        will be "error".
-
-        If no errors present returns an empty string.
-
-        `name` : errors for name. If **None** all errors will be rendered.
-        """
-
-        if name is None:
-            errors = self.all_errors()
-        else:
-            errors = self.errors_for(name)
-
-        if not errors:
-            return ''
-
-        content = "\n".join(HTML.tag("li", error) for error in errors)
-        
-        if 'class_' not in attrs:
-            attrs['class_'] = "error"
-
-        return HTML.tag("ul", tags.literal(content), **attrs)
+        return self.form.errors
 
     def label(self, name, label=None, **attrs):
         """

File pyramid_simpleform/tests.py

 class TestColanderForm(unittest.TestCase):
     
 
-    def test_is_error(self):
+    def test_errors(self):
         from pyramid_simpleform.form import Form
 
         request = testing.DummyRequest()
         self.assert_(form.is_validated)
         self.assert_('name' in form.errors)
 
-    def test_all_errors_with_dict(self):
-
-        from pyramid_simpleform.form import Form
-
-        request = testing.DummyRequest()
-        request.method = "POST"
-
-        form = Form(request, SimpleColanderSchema())
-        form.errors = {"name" : u"Name is missing",}
-        form.non_field_errors.append(u"Value is missing")
-        self.assert_(form.all_errors() == [
-            u"Value is missing",
-            u"Name is missing"])
-
-    def test_errors_for(self):
-        from pyramid_simpleform.form import Form
-
-        request = testing.DummyRequest()
-        request.method = "POST"
-
-        form = Form(request, SimpleColanderSchema())
-
-        self.assert_(not(form.validate()))
-        self.assert_(form.is_validated)
-        self.assert_('name' in form.errors)
-
-        self.assert_(form.errors_for('name') == ['Required'])
-
     def test_validate_twice(self):
         
         from pyramid_simpleform.form import Form
             '<input checked="checked" id="name" name="name" type="checkbox" '
             'value="1" />')
 
-    def test_is_error(self):
+    def test_has_errorrs(self):
         from pyramid_simpleform.form import Form
         from pyramid_simpleform.renderers import FormRenderer
 
         self.assert_(not(form.validate()))
 
         renderer = FormRenderer(form)
-        self.assert_(renderer.is_error('name'))
+        self.assert_(renderer.errors)
 
-    def test_errors_for(self):
-
-        from pyramid_simpleform.form import Form
-        from pyramid_simpleform.renderers import FormRenderer
-
-        request = testing.DummyRequest()
-        request.method = "POST"
-
-        form = Form(request, SimpleColanderSchema())
-
-        self.assert_(not(form.validate()))
-        renderer = FormRenderer(form)
-
-        self.assert_(renderer.errors_for('name') == ['Required'])
-
-    def test_errorlist(self):
-
-        from pyramid_simpleform.form import Form
-        from pyramid_simpleform.renderers import FormRenderer
-
-        request = testing.DummyRequest()
-        request.method = "POST"
-
-        form = Form(request, SimpleColanderSchema())
-        form.validate()
-
-        renderer = FormRenderer(form)
-
-        self.assert_(renderer.errorlist() == \
-                     '<ul class="error"><li>Required</li></ul>')
-     
-
-    def test_errorlist_with_no_errors(self):
-
-        from pyramid_simpleform.form import Form
-        from pyramid_simpleform.renderers import FormRenderer
-
-        request = testing.DummyRequest()
-        request.method = "POST"
-        request.POST['name'] = 'test'
-
-        form = Form(request, SimpleColanderSchema())
-        form.validate()
-
-        renderer = FormRenderer(form)
-
-        self.assert_(renderer.errorlist() == '')
-
- 
-    def test_errorlist_with_field(self):
-
-        from pyramid_simpleform.form import Form
-        from pyramid_simpleform.renderers import FormRenderer
-
-        request = testing.DummyRequest()
-        request.method = "POST"
-
-        form = Form(request, SimpleColanderSchema())
-        form.validate()
-
-        renderer = FormRenderer(form)
-
-        self.assert_(renderer.errorlist('name') == \
-                     '<ul class="error"><li>Required</li></ul>')
- 
     def test_label(self):
 
         from pyramid_simpleform.form import Form
             '<input checked="checked" id="name" name="name" type="checkbox" '
             'value="1" />')
 
-    def test_is_error(self):
+    def test_errors(self):
         from pyramid_simpleform import Form
         from pyramid_simpleform.renderers import FormRenderer
 
         self.assert_(not(form.validate()))
 
         renderer = FormRenderer(form)
-        self.assert_(renderer.is_error('name'))
+        self.assert_(renderer.errors)
 
-    def test_errors_for(self):
-
-        from pyramid_simpleform import Form
-        from pyramid_simpleform.renderers import FormRenderer
-
-        request = testing.DummyRequest()
-        request.method = "POST"
-
-        form = Form(request, SimpleFESchema)
-
-        self.assert_(not(form.validate()))
-        renderer = FormRenderer(form)
-
-        self.assert_(renderer.errors_for('name') == ['Missing value'])
-
-    def test_errorlist(self):
-
-        from pyramid_simpleform import Form
-        from pyramid_simpleform.renderers import FormRenderer
-
-        request = testing.DummyRequest()
-        request.method = "POST"
-
-        form = Form(request, SimpleFESchema)
-        form.validate()
-
-        renderer = FormRenderer(form)
-
-        self.assert_(renderer.errorlist() == \
-                     '<ul class="error"><li>Missing value</li></ul>')
-     
-
-    def test_errorlist_with_no_errors(self):
-
-        from pyramid_simpleform import Form
-        from pyramid_simpleform.renderers import FormRenderer
-
-        request = testing.DummyRequest()
-        request.method = "POST"
-        request.POST['name'] = 'test'
-
-        form = Form(request, SimpleFESchema)
-        form.validate()
-
-        renderer = FormRenderer(form)
-
-        self.assert_(renderer.errorlist() == '')
-
-    def test_errorlist_with_custom_localizer(self):
-
-        from pyramid_simpleform import Form
-        from pyramid_simpleform import State
-        from pyramid_simpleform.renderers import FormRenderer
-
-        request = testing.DummyRequest()
-        request.method = "POST"
-
-        state = State(_=lambda s:s.upper())
-
-        form = Form(request, SimpleFESchema, state=state)
-        form.validate()
-
-        renderer = FormRenderer(form)
-
-        self.assert_(renderer.errorlist('name') == \
-                     '<ul class="error"><li>MISSING VALUE</li></ul>')
- 
- 
-    def test_errorlist_with_field(self):
-
-        from pyramid_simpleform import Form
-        from pyramid_simpleform.renderers import FormRenderer
-
-        request = testing.DummyRequest()
-        request.method = "POST"
-
-        form = Form(request, SimpleFESchema)
-        form.validate()
-
-        renderer = FormRenderer(form)
-
-        self.assert_(renderer.errorlist('name') == \
-                     '<ul class="error"><li>Missing value</li></ul>')
- 
     def test_label(self):
 
         from pyramid_simpleform import Form