Add "hidden" concept for form fields

i.e. include hidden fields but don't show label or other dressing
This commit is contained in:
Lance Edgar 2018-02-05 12:53:57 -06:00
parent 868b184069
commit ab16ffc823
2 changed files with 16 additions and 8 deletions

View file

@ -320,10 +320,11 @@ class Form(object):
""" """
Base class for all forms. Base class for all forms.
""" """
update_label = "Save"
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=None, model_instance=None, model_class=None, nodes={}, enums={}, labels={}, renderers=None,
widgets={}, defaults={}, validators={}, required={}, helptext={}, hidden={}, widgets={}, defaults={}, validators={}, required={}, helptext={},
action_url=None, cancel_url=None): action_url=None, cancel_url=None):
self.fields = None self.fields = None
@ -346,6 +347,7 @@ class Form(object):
self.renderers = self.make_renderers() self.renderers = self.make_renderers()
else: else:
self.renderers = renderers or {} self.renderers = renderers or {}
self.hidden = hidden or {}
self.widgets = widgets or {} self.widgets = widgets or {}
self.defaults = defaults or {} self.defaults = defaults or {}
self.validators = validators or {} self.validators = validators or {}
@ -588,6 +590,9 @@ class Form(object):
else: else:
self.renderers[key] = renderer self.renderers[key] = renderer
def set_hidden(self, key, hidden=True):
self.hidden[key] = hidden
def set_widget(self, key, widget): def set_widget(self, key, widget):
self.widgets[key] = widget self.widgets[key] = widget
@ -681,6 +686,11 @@ class Form(object):
context['render_field_readonly'] = self.render_field_readonly context['render_field_readonly'] = self.render_field_readonly
return render('/forms2/deform.mako', context) return render('/forms2/deform.mako', context)
def field_visible(self, field):
if self.hidden and self.hidden.get(field):
return False
return True
def render_field_readonly(self, field_name, **kwargs): def render_field_readonly(self, field_name, **kwargs):
label = HTML.tag('label', self.get_label(field_name), for_=field_name) label = HTML.tag('label', self.get_label(field_name), for_=field_name)
field = self.render_field_value(field_name) or '' field = self.render_field_value(field_name) or ''

View file

@ -20,11 +20,8 @@ ${h.csrf_token(request)}
% elif field in dform: % elif field in dform:
<% field = dform[field] %> <% field = dform[field] %>
## % if field.requires_label: % if form.field_visible(field.name):
<div class="field-wrapper ${field.name} ${'error' if field.error else ''}"> <div class="field-wrapper ${field.name} ${'error' if field.error else ''}">
## % for error in field.errors:
## <div class="field-error">${error}</div>
## % endfor
% if field.error: % if field.error:
<div class="field-error"> <div class="field-error">
% for msg in field.error.messages(): % for msg in field.error.messages():
@ -61,9 +58,10 @@ ${h.csrf_token(request)}
% endif % endif
% endif % endif
## % else: % else:
## ${field.render()|n} ## hidden field
## % endif ${field.serialize()|n}
% endif
% endif % endif