Commits

Anonymous committed 83aaf13

additional args for validate()

Comments (0)

Files changed (2)

pyramid_simpleform/__init__.py

             errors = [errors]
         return errors
 
-    def validate(self):
+    def validate(self, force_validate=False, params=None):
         """
         Runs validation and returns True/False whether form is 
         valid.
 
         The errors and data values will be updated accordingly.
 
+        `force_validate`  : will run validation regardless of request method.
+
+        `params`          : dict or MultiDict of params. By default 
+        will use **request.POST** (if HTTP POST) or **request.params**.
         """
 
         assert self.schema or self.validators, \
         if self.is_validated:
             return not(self.errors)
 
-        if self.method and self.method != self.request.method:
-            return False
+        if not force_validate:
+            if self.method and self.method != self.request.method:
+                return False
 
-        if self.method == "POST":
-            params = self.request.POST
-        else:
-            params = self.request.params
-        
+        if params is None:
+            if self.method == "POST":
+                params = self.request.POST
+            else:
+                params = self.request.params
+            
         if self.variable_decode:
             decoded = variabledecode.variable_decode(
                         params, self.dict_char, self.list_char)

pyramid_simpleform/tests.py

 
         self.assert_(not(form.validate()))
         self.assert_(form.is_validated)
+
+    def test_is_validated_with_specified_params(self):
+        from pyramid_simpleform import Form
+
+        request = testing.DummyRequest()
+        request.method = "POST"
+
+        form = Form(request, SimpleSchema)
+        form.validate(params={'name' : 'foo'})
+        obj = form.bind(SimpleObj())
+        self.assert_(obj.name == 'foo')
  
     def test_bind(self):
         from pyramid_simpleform import Form
 
         self.assert_(form.validate())
         self.assert_(form.is_validated)
+
+    def test_force_validate(self):
+        from pyramid_simpleform import Form
+
+        request = testing.DummyRequest()
+        request.GET['name'] = 'test'
+
+        form = Form(request, SimpleSchema)
+
+        self.assert_(form.validate(force_validate=True))
+        self.assert_(form.is_validated)
  
     def test_render_without_htmlfill(self):
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.