Anonymous avatar Anonymous committed 7083ded

better obj handling

Comments (0)

Files changed (3)

 
 Note that running **bind()** on a form that hasn't been validated yet, or where the form contains errors, will raise a **RuntimeError**.
 
+Also note that attributes starting with an underscore will not be explicitly bound. In order to bind such properties you must do so manually from the ``data`` property of your form instance.
+
 Form rendering
 --------------
 

pyramid_simpleform/__init__.py

 
     `list_char`       : variabledecode list char
 
+    Also note that values of ``obj`` supercede those of ``defaults``. Only
+    fields specified in your schema or validators will be taken from the 
+    object.
     """
 
     default_state = State
 
     def __init__(self, request, schema=None, validators=None, defaults=None, 
-                 obj=None, state=None, method="POST", variable_decode=False, 
-                 dict_char=".", list_char="-", multipart=False):
+                 obj=None, extra=None, include=None, exclude=None, state=None, 
+                 method="POST", variable_decode=False,  dict_char=".", 
+                 list_char="-", multipart=False):
 
         self.request = request
         self.schema = schema
-        self.validators = validators
+        self.validators = validators or {}
         self.method = method
         self.variable_decode = variable_decode
         self.dict_char = dict_char
             self.data.update(defaults)
 
         if obj:
-            self.data.update(obj.__dict__)
+            fields = self.schema.fields.keys() + self.validators.keys() 
+            for f in fields:
+                if hasattr(obj, f):
+                    self.data[f] = getattr(obj, f) 
 
     def is_error(self, field):
         """
 
         Returns the `obj` passed in.
 
+        Note that any properties starting with underscore "_" are ignored
+        regardless of ``include`` and ``exclude``. If you need to set these
+        do so manually from the ``data`` property of the form instance.
+
         Calling bind() before running validate() will result in a RuntimeError
         """
 
         if self.errors:
             raise RuntimeError, "Cannot bind to object if form has errors"
 
-        for k, v in self.data.items():
+        items = [(k, v) for k, v in self.data.items() if not k.startswith("_")]
+        for k, v in items:
 
             if include and k not in include:
                 continue

pyramid_simpleform/tests.py

         form.validate()
         obj = form.bind(SimpleObj())
         self.assert_(obj.name == 'test')
+
+    def test_bind_ignore_underscores(self):
+        from pyramid_simpleform import Form
+
+        request = testing.DummyRequest()
+        request.method = "POST"
+        request.POST['name'] = 'test'
+        request.POST['_ignoreme'] = 'test'
+
+        class SimpleObjWithPrivate(SimpleObj):
+            _ignoreme = None
+
+        class SimpleSchemaWithPrivate(SimpleSchema):
+            _ignoreme = validators.String()
+
+        form = Form(request, SimpleSchemaWithPrivate)
+        form.validate()
+        obj = form.bind(SimpleObjWithPrivate())
+        self.assert_(obj.name == 'test')
+        self.assert_(obj._ignoreme is None)
         
     def test_bind_not_validated_yet(self):
         from pyramid_simpleform import Form
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.