initial form values can now also be a callable of the field or the form itself tls-sprint
authorFlorent <florent@secondweb.fr>
Fri, 17 Apr 2009 08:51:16 +0200
branchtls-sprint
changeset 1386 8ba1f0d36c7e
parent 1380 aa1586de4563
child 1387 4af48b1aaf29
initial form values can now also be a callable of the field or the form itself
web/form.py
--- a/web/form.py	Thu Apr 16 15:38:58 2009 +0200
+++ b/web/form.py	Fri Apr 17 08:51:16 2009 +0200
@@ -343,14 +343,22 @@
         1. kw args given to render_form (including previously submitted form
            values if any)
         2. req.form
-        3. field's initial value
+        3. default_<fieldname> attribute / method on the form
+        4. field's initial value
         """
+        defaultattr = 'default_%s' % field.name
         if field.name in values:
             value = values[field.name]
         elif field.name in self.req.form:
             value = self.req.form[field.name]
+        elif hasattr(self, defaultattr):
+            value = getattr(self, defaultattr)
+            if callable(value):
+                value = value()
         else:
             value = field.initial
+            if callable(value):
+                value = value(self)
         return value
     
     def form_field_error(self, field):
@@ -479,7 +487,7 @@
                     # use field's initial value
                     value = field.initial
             if callable(value):
-                value = value()
+                value = value(self)
         return value
     
     def form_field_format(self, field):