Add basic "helptext" support for forms2
This commit is contained in:
parent
c20fdf4450
commit
8041c085f6
|
@ -286,7 +286,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={}, 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.schema = schema
|
||||
|
@ -307,6 +308,7 @@ class Form(object):
|
|||
self.defaults = defaults or {}
|
||||
self.validators = validators or {}
|
||||
self.required = required or {}
|
||||
self.helptext = helptext or {}
|
||||
self.action_url = action_url
|
||||
self.cancel_url = cancel_url
|
||||
|
||||
|
@ -340,6 +342,10 @@ class Form(object):
|
|||
def insert_after(self, 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):
|
||||
for arg in args:
|
||||
if arg in self.fields:
|
||||
|
@ -442,7 +448,12 @@ class Form(object):
|
|||
if key in self.readonly_fields:
|
||||
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
|
||||
|
||||
def set_type(self, key, type_):
|
||||
|
@ -500,6 +511,25 @@ class Form(object):
|
|||
"""
|
||||
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):
|
||||
if not template:
|
||||
if self.readonly:
|
||||
|
|
|
@ -32,9 +32,9 @@ ${h.csrf_token(request)}
|
|||
<div class="field">
|
||||
${field.serialize()|n}
|
||||
</div>
|
||||
## % if 'instructions' in field.metadata:
|
||||
## <span class="instructions">${field.metadata['instructions']}</span>
|
||||
## % endif
|
||||
% if form.has_helptext(field.name):
|
||||
<span class="instructions">${form.render_helptext(field.name)}</span>
|
||||
% endif
|
||||
</div>
|
||||
|
||||
## % if not _focus_rendered and (fieldset.focus is True or fieldset.focus is field):
|
||||
|
|
Loading…
Reference in a new issue