web/form.py
branchstable
changeset 4668 9f82f81bf13d
parent 4252 6c4f109c2b03
child 4683 c375d50eaad3
--- a/web/form.py	Thu Feb 18 14:30:23 2010 +0100
+++ b/web/form.py	Tue Feb 23 17:32:31 2010 +0100
@@ -15,7 +15,7 @@
 from cubicweb.appobject import AppObject
 from cubicweb.view import NOINDEX, NOFOLLOW
 from cubicweb import tags
-from cubicweb.web import stdmsgs, httpcache, formfields
+from cubicweb.web import stdmsgs, httpcache, formfields, controller
 
 
 class FormViewMixIn(object):
@@ -69,12 +69,42 @@
     __metaclass__ = metafieldsform
     __registry__ = 'forms'
 
+    internal_fields = ('__errorurl',) + controller.NAV_FORM_PARAMETERS
+
     parent_form = None
     force_session_key = None
+    domid = 'form'
+    copy_nav_params = False
 
-    def __init__(self, req, rset, **kwargs):
-        super(Form, self).__init__(req, rset=rset, **kwargs)
-        self.restore_previous_post(self.session_key())
+    def __init__(self, req, rset=None, row=None, col=None,
+                 submitmsg=None, mainform=True, **kwargs):
+        super(Form, self).__init__(req, rset=rset, row=row, col=col)
+        self.fields = list(self.__class__._fields_)
+        self.add_hidden(u'__form_id', kwargs.pop('formvid', self.__regid__))
+        for key, val in kwargs.iteritems():
+            if key in controller.NAV_FORM_PARAMETERS:
+                self.add_hidden(key, val)
+            elif key == 'redirect_path':
+                self.add_hidden(u'__redirectpath', val)
+            elif hasattr(self.__class__, key) and not key[0] == '_':
+                setattr(self, key, val)
+            else:
+                self.cw_extra_kwargs[key] = val
+            # skip other parameters, usually given for selection
+            # (else write a custom class to handle them)
+        if mainform:
+            self.add_hidden(u'__errorurl', self.session_key())
+            self.add_hidden(u'__domid', self.domid)
+            self.restore_previous_post(self.session_key())
+        # XXX why do we need two different variables (mainform and copy_nav_params ?)
+        if self.copy_nav_params:
+            for param in controller.NAV_FORM_PARAMETERS:
+                if not param in kwargs:
+                    value = req.form.get(param)
+                    if value:
+                        self.add_hidden(param, value)
+        if submitmsg is not None:
+            self.add_hidden(u'__message', submitmsg)
 
     @property
     def root_form(self):