Add support for setting default field values on new forms
i.e. those using Colander schema
This commit is contained in:
parent
c95e2dbb06
commit
7442b933fd
|
@ -177,8 +177,8 @@ class Form(object):
|
|||
"""
|
||||
|
||||
def __init__(self, fields=None, schema=None, request=None, readonly=False, readonly_fields=[],
|
||||
model_instance=None, model_class=None, nodes={}, enums={}, labels={}, renderers={}, widgets={},
|
||||
action_url=None, cancel_url=None):
|
||||
model_instance=None, model_class=None, nodes={}, enums={}, labels={}, renderers={},
|
||||
widgets={}, defaults={}, action_url=None, cancel_url=None):
|
||||
|
||||
self.fields = list(fields) if fields is not None else None
|
||||
self.schema = schema
|
||||
|
@ -196,6 +196,7 @@ class Form(object):
|
|||
self.labels = labels or {}
|
||||
self.renderers = renderers or {}
|
||||
self.widgets = widgets or {}
|
||||
self.defaults = defaults or {}
|
||||
self.action_url = action_url
|
||||
self.cancel_url = cancel_url
|
||||
|
||||
|
@ -265,6 +266,11 @@ class Form(object):
|
|||
if key in schema:
|
||||
schema[key].widget = widget
|
||||
|
||||
# apply any default values
|
||||
for key, default in self.defaults.items():
|
||||
if key in schema:
|
||||
schema[key].default = default
|
||||
|
||||
self.schema = schema
|
||||
|
||||
return self.schema
|
||||
|
@ -330,7 +336,7 @@ class Form(object):
|
|||
"""
|
||||
Set the default value for a given field.
|
||||
"""
|
||||
raise NotImplementedError # TODO
|
||||
self.defaults[key] = value
|
||||
|
||||
def render(self, template=None, **kwargs):
|
||||
if not template:
|
||||
|
|
Loading…
Reference in a new issue