Permalink
Browse files

warn about Form.validate() mutating the form itself

  • Loading branch information...
1 parent 041dc86 commit ce606744299850f48e7377380bb34ac5c185b65e @domenkozar domenkozar committed Sep 23, 2012
Showing with 19 additions and 12 deletions.
  1. +1 −0 CONTRIBUTORS.txt
  2. +18 −12 deform/field.py
View
@@ -112,3 +112,4 @@ Contributors
- Josh Finnie, 2012/02/16
- Daniel Nouri, 2012/03/30
- Sam Brauer, 2012/09/15
+- Domen Kožar, 2012/09/23
View
@@ -493,18 +493,24 @@ class MySchema(schema.MappingSchema):
color = schema.SchemaNode(schema.String())
schema = MySchema()
- form = Form(schema)
-
- if 'submit' in request.POST: # the form submission needs validation
- controls = request.POST.items()
- try:
- deserialized = form.validate(controls)
- do_something(deserialized)
- return HTTPFound(location='http://example.com/success')
- except ValidationFailure, e:
- return {'form':e.render()}
- else:
- return {'form':form.render()} # the form just needs rendering
+
+ def view(request):
+ form = Form(schema, buttons=('submit',))
+ if 'submit' in request.POST: # the form submission needs validation
+ controls = request.POST.items()
+ try:
+ deserialized = form.validate(controls)
+ do_something(deserialized)
+ return HTTPFound(location='http://example.com/success')
+ except ValidationFailure, e:
+ return {'form':e.render()}
+ else:
+ return {'form':form.render()} # the form just needs rendering
+
+ .. warning::
+
+ ``form.validate(controls)`` mutates ``Form`` instance, so ``Form``
+ instance should be constructed (and live) inside one request.
If ``subcontrol`` is supplied, it represents a named subitem in the
data returned by ``peppercorn.parse(controls)``. Use this subitem as

0 comments on commit ce60674

Please sign in to comment.