Commits

Anonymous committed 5f6b6f5

first stab at handling nested values

  • Participants
  • Parent commits 4eff770

Comments (0)

Files changed (2)

File examples/simplesite/simplesite/handlers.py

+import pprint
 import logging
 import colander
 
     )
 
 
+class NestedSchema(colander.MappingSchema):
+
+    number = colander.SchemaNode(
+        colander.Int()
+    )
+
+    model = MyModelSchema()
+
+
 log = logging.getLogger(__name__)
 
 class MainHandler(object):
             return HTTPFound(location="/")
 
         return dict(form=FormRenderer(form))
+
+    @action(renderer='nested.html')
+    def nested(self):
+
+        form = Form(self.request, schema=NestedSchema())
+
+        if form.validate():
+
+            print pprint.pprint(form.data)
+
+        return dict(form=FormRenderer(form))

File pyramid_simpleform/renderers.py

 
         self.form = form
         self.data = self.form.data
+        self.context = []
         self.csrf_field = csrf_field
 
     def value(self, name, default=None):
-        return self.data.get(name, default)
+        try:
+            context = self.context[0]  
+            data = self.data.get(context, {})
+        except IndexError:
+            data = self.data
+
+        return data.get(name, default)
 
     def begin(self, url=None, **attrs):
         """
         if name:
             value = name + ":" + value
 
+        # push data stack
+        self.context.insert(0, name)
+
         return HTML.tag(
             "div", 
             self.hidden('__start__', value, id=id), 
             style="display:none;"
         )
 
+    def start_mapping_tag(self, name=None, id=''):
+        return self.start_hidden_tag('mapping', name, id)
+
+    def start_sequence_tag(self, name=None, id=''):
+        return self.start_hidden_tag('sequence', name, id)
+
     def end_hidden_tag(self, value=None, id=''):
         """
         Renders peppercorn __end__ hidden tag
 
         :versionadded: 0.7
         """
+        self.context.pop()
+
         return HTML.tag(
             "div",
             self.hidden('__end__', value, id=id),
 
         for i in xrange(num_tags):
             rv.append(self.hidden('__end__', id=''))
+            self.context.pop()
 
         return HTML.tag(
             "div",
         """
         Outputs radio input.
         """
-        checked = self.data.get(name) == value or checked
+        checked = self.value(name) == value or checked
         return tags.radio(name, value, checked, label, **attrs)
 
     def submit(self, name, value=None, id=None, **attrs):