Add basic "helptext" support for forms2

This commit is contained in:
Lance Edgar 2017-12-05 15:07:25 -06:00
parent c20fdf4450
commit 8041c085f6
2 changed files with 35 additions and 5 deletions

View file

@ -286,7 +286,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={}, model_instance=None, model_class=None, nodes={}, enums={}, labels={}, renderers={},
widgets={}, defaults={}, validators={}, required={}, action_url=None, cancel_url=None): widgets={}, defaults={}, validators={}, required={}, helptext={},
action_url=None, cancel_url=None):
self.fields = FieldList(fields) if fields is not None else None self.fields = FieldList(fields) if fields is not None else None
self.schema = schema self.schema = schema
@ -307,6 +308,7 @@ class Form(object):
self.defaults = defaults or {} self.defaults = defaults or {}
self.validators = validators or {} self.validators = validators or {}
self.required = required or {} self.required = required or {}
self.helptext = helptext or {}
self.action_url = action_url self.action_url = action_url
self.cancel_url = cancel_url self.cancel_url = cancel_url
@ -340,6 +342,10 @@ class Form(object):
def insert_after(self, field, newfield): def insert_after(self, field, newfield):
self.fields.insert_after(field, newfield) self.fields.insert_after(field, newfield)
def replace(self, field, newfield):
self.insert_after(field, newfield)
self.remove(field)
def remove(self, *args): def remove(self, *args):
for arg in args: for arg in args:
if arg in self.fields: if arg in self.fields:
@ -442,7 +448,12 @@ class Form(object):
if key in self.readonly_fields: if key in self.readonly_fields:
self.readonly_fields.remove(key) self.readonly_fields.remove(key)
def set_node(self, key, node): def set_node(self, key, nodeinfo, **kwargs):
if isinstance(nodeinfo, colander.SchemaNode):
node = nodeinfo
else:
kwargs.setdefault('name', key)
node = colander.SchemaNode(nodeinfo, **kwargs)
self.nodes[key] = node self.nodes[key] = node
def set_type(self, key, type_): def set_type(self, key, type_):
@ -500,6 +511,25 @@ class Form(object):
""" """
self.defaults[key] = value self.defaults[key] = value
def set_helptext(self, key, value):
"""
Set the help text for a given field.
"""
self.helptext[key] = value
def has_helptext(self, key):
"""
Returns boolean indicating whether the given field has accompanying
help text.
"""
return key in self.helptext
def render_helptext(self, key):
"""
Render the help text for the given field.
"""
return self.helptext[key]
def render(self, template=None, **kwargs): def render(self, template=None, **kwargs):
if not template: if not template:
if self.readonly: if self.readonly:

View file

@ -32,9 +32,9 @@ ${h.csrf_token(request)}
<div class="field"> <div class="field">
${field.serialize()|n} ${field.serialize()|n}
</div> </div>
## % if 'instructions' in field.metadata: % if form.has_helptext(field.name):
## <span class="instructions">${field.metadata['instructions']}</span> <span class="instructions">${form.render_helptext(field.name)}</span>
## % endif % endif
</div> </div>
## % if not _focus_rendered and (fieldset.focus is True or fieldset.focus is field): ## % if not _focus_rendered and (fieldset.focus is True or fieldset.focus is field):