Add support for setting default field values on new forms

i.e. those using Colander schema
This commit is contained in:
Lance Edgar 2017-10-14 17:55:29 -07:00
parent c95e2dbb06
commit 7442b933fd

View file

@ -177,8 +177,8 @@ class Form(object):
""" """
def __init__(self, fields=None, schema=None, request=None, readonly=False, readonly_fields=[], def __init__(self, fields=None, schema=None, request=None, readonly=False, readonly_fields=[],
model_instance=None, model_class=None, nodes={}, enums={}, labels={}, renderers={}, widgets={}, model_instance=None, model_class=None, nodes={}, enums={}, labels={}, renderers={},
action_url=None, cancel_url=None): widgets={}, defaults={}, action_url=None, cancel_url=None):
self.fields = list(fields) if fields is not None else None self.fields = list(fields) if fields is not None else None
self.schema = schema self.schema = schema
@ -196,6 +196,7 @@ class Form(object):
self.labels = labels or {} self.labels = labels or {}
self.renderers = renderers or {} self.renderers = renderers or {}
self.widgets = widgets or {} self.widgets = widgets or {}
self.defaults = defaults or {}
self.action_url = action_url self.action_url = action_url
self.cancel_url = cancel_url self.cancel_url = cancel_url
@ -265,6 +266,11 @@ class Form(object):
if key in schema: if key in schema:
schema[key].widget = widget 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 self.schema = schema
return self.schema return self.schema
@ -330,7 +336,7 @@ class Form(object):
""" """
Set the default value for a given field. Set the default value for a given field.
""" """
raise NotImplementedError # TODO self.defaults[key] = value
def render(self, template=None, **kwargs): def render(self, template=None, **kwargs):
if not template: if not template: