File pyramid_simpleform-git-e22834c5.patch of Package python-pyramid_simpleform
diff --git a/docs/index.rst b/docs/index.rst
index 15b171c..81352e8 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -21,6 +21,7 @@ Here is a typical (truncated) example::
from formencode import Schema, validators
from pyramid_simpleform import Form
+ from pyramid_simpleform.renderers import FormRenderer
class MySchema(Schema):
diff --git a/pyramid_simpleform/__init__.py b/pyramid_simpleform/__init__.py
index b5aff63..0e8a0af 100644
--- a/pyramid_simpleform/__init__.py
+++ b/pyramid_simpleform/__init__.py
@@ -162,7 +162,7 @@ class Form(object):
`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**.
+ will use **request.json_body** (if JSON body), **request.POST** (if HTTP POST) or **request.params**.
"""
assert self.schema or self.validators, \
@@ -176,17 +176,21 @@ class Form(object):
return False
if params is None:
- if self.method == "POST":
+ if hasattr(self.request, 'json_body') and self.request.json_body:
+ params = self.request.json_body
+ elif self.method == "POST":
params = self.request.POST
else:
params = self.request.params
- if self.variable_decode:
+ if self.variable_decode and not (hasattr(self.request, 'json_body') and self.request.json_body):
decoded = variabledecode.variable_decode(
params, self.dict_char, self.list_char)
else:
decoded = params
+ if hasattr(decoded, "mixed"):
+ decoded = decoded.mixed()
self.data.update(params)
diff --git a/pyramid_simpleform/tests.py b/pyramid_simpleform/tests.py
index 77f85de..d0d21ba 100644
--- a/pyramid_simpleform/tests.py
+++ b/pyramid_simpleform/tests.py
@@ -102,6 +102,37 @@ class TestFormencodeForm(unittest.TestCase):
form.errors = [u"Name is missing"]
self.assert_(form.all_errors() == [u"Name is missing"])
+ def test_ok_with_jsonbody(self):
+
+ from pyramid_simpleform import Form
+
+ request = testing.DummyRequest()
+ request.method = "POST"
+
+ import json
+ request.json_body = json.loads('{"name" : "ok"}')
+
+ form = Form(request, SimpleFESchema)
+ self.assert_(form.validate())
+
+ def test_error_with_jsonbody(self):
+
+ from pyramid_simpleform import Form
+
+ request = testing.DummyRequest()
+ request.method = "POST"
+
+ import json
+ request.json_body = json.loads('{}')
+
+ form = Form(request, SimpleFESchema)
+ form.errors = {"name" : [u"Name is missing"],
+ "value" : u"Value is missing"}
+ self.assert_(sorted(form.all_errors()) == sorted([
+ u"Name is missing",
+ u"Value is missing"]))
+
+
def test_all_errors_with_dict(self):
from pyramid_simpleform import Form
@@ -180,6 +211,24 @@ class TestFormencodeForm(unittest.TestCase):
self.assert_(form.errors_for('name') == ['Please enter a value'])
+ def test_foreach_with_validators_and_multidict(self):
+ from formencode import ForEach
+ from pyramid_simpleform import Form
+ from webob.multidict import MultiDict
+
+ request = testing.DummyRequest()
+ request.method = "POST"
+ request.POST = MultiDict([
+ ("name", "1"),
+ ("name", "2"),
+ ("name", "3"),
+ ])
+
+ form = Form(request,
+ validators=dict(name=ForEach(validators.NotEmpty())))
+ self.assert_(form.validate())
+ self.assertListEqual(form.data["name"], ["1", "2", "3"])
+
def test_is_validated_on_post(self):
from pyramid_simpleform import Form