Skip to content
Snippets Groups Projects
Commit 59ffb84d authored by Elke Kreim's avatar Elke Kreim
Browse files

Add helptext-tags for fields change checkbox input behaviour

Differences between multi select checkboxes and single
checkbox are considered. Field label for single checkbox
becomes checkbox label while multi select checkboxes have
a field label and additional checkbox or option labels.
parent 4c365d3f
No related branches found
No related tags found
No related merge requests found
...@@ -27,6 +27,12 @@ def render_form_field(field, label=''): ...@@ -27,6 +27,12 @@ def render_form_field(field, label=''):
out = label_or_not(BulmaFieldMarkup.with_icons(field, field.as_widget())) out = label_or_not(BulmaFieldMarkup.with_icons(field, field.as_widget()))
elif input_type and getattr(BulmaFieldMarkup, input_type, None): elif input_type and getattr(BulmaFieldMarkup, input_type, None):
# something else explicitly defined # something else explicitly defined
allow_multiple_selected = getattr(field.field.widget, 'allow_multiple_selected', False)
if input_type == 'checkbox' and allow_multiple_selected:
# Checkboxes for multi select have common label above and checkboxes
# with label of choice option.
out = label_or_not(BulmaFieldMarkup.div_control(field.as_widget()))
else:
out = getattr(BulmaFieldMarkup, input_type)(field, field.as_widget()) out = getattr(BulmaFieldMarkup, input_type)(field, field.as_widget())
else: else:
# fallback default # fallback default
...@@ -48,7 +54,11 @@ class BulmaFieldMarkup(object): ...@@ -48,7 +54,11 @@ class BulmaFieldMarkup(object):
"""Bulma requires to wrap every input field with this <div class="field">""" """Bulma requires to wrap every input field with this <div class="field">"""
args_generator = ([str(e)] for e in field.errors) args_generator = ([str(e)] for e in field.errors)
error_tags = format_html_join(str(), '<p class="help is-danger">{0}</p>', args_generator) error_tags = format_html_join(str(), '<p class="help is-danger">{0}</p>', args_generator)
return format_html('<div class="field">{}{}</div>', content, error_tags) if field.help_text:
help_tags = format_html('<p class=help>{}</p>', mark_safe(field.help_text))
else:
help_tags = ''
return format_html('<div class="field">{}{}{}</div>', content, error_tags, help_tags)
@classmethod @classmethod
def div_control(cls, content, control_class='control'): def div_control(cls, content, control_class='control'):
...@@ -105,6 +115,7 @@ class BulmaFieldMarkup(object): ...@@ -105,6 +115,7 @@ class BulmaFieldMarkup(object):
"""Checkboxes are super special, they wrap the input field with the label""" """Checkboxes are super special, they wrap the input field with the label"""
return cls.div_control(cls.label(mark_safe(content + ' ' + str(field.label)), css_class='checkbox')) return cls.div_control(cls.label(mark_safe(content + ' ' + str(field.label)), css_class='checkbox'))
@classmethod @classmethod
def radio(cls, field, content): def radio(cls, field, content):
choice_markup = '' choice_markup = ''
...@@ -128,14 +139,14 @@ def render_form_generics( ...@@ -128,14 +139,14 @@ def render_form_generics(
rendered_errors, rendered_errors,
rendered_submit_button, rendered_submit_button,
submit_only_once=True, submit_only_once=True,
form_id='' form_id='',
): ):
csrf_field = defaulttags.CsrfTokenNode().render(context) csrf_field = defaulttags.CsrfTokenNode().render(context)
submit_only_once = "true" if submit_only_once else "false" submit_only_once = "true" if submit_only_once else "false"
form_icon = None form_icon = None
form_id = 'id={}'.format(form_id) if form_id else '' form_id = 'id={}'.format(form_id) if form_id else ''
return format_html( return format_html(
""" f"""
<form method="post" {form_id} data-submit-only-once="{submit_only_once}"> <form method="post" {form_id} data-submit-only-once="{submit_only_once}">
{csrf_field} {csrf_field}
{rendered_errors} {rendered_errors}
......
...@@ -63,13 +63,20 @@ def bulma_form( ...@@ -63,13 +63,20 @@ def bulma_form(
submit_class="button is-outlined", submit_class="button is-outlined",
submit_icon="fas fa-check", submit_icon="fas fa-check",
submit_only_once=True, submit_only_once=True,
form_id='' form_id='',
): ):
"""Renders whole form, including errors, csrf and a submit button.""" """Renders whole form, including errors, csrf and a submit button."""
fields = bulma_form_fields(form) fields = bulma_form_fields(form)
errors = bulma_form_errors(form) errors = bulma_form_errors(form)
submit = bulma_submit_button(text=submit_text, css_class=submit_class, icon=submit_icon) submit = bulma_submit_button(text=submit_text, css_class=submit_class, icon=submit_icon)
return render_form_generics(context, fields, errors, submit, submit_only_once, form_id) return render_form_generics(
context,
fields,
errors,
submit,
submit_only_once,
form_id,
)
def render_layout(elements, form): def render_layout(elements, form):
......
...@@ -7,7 +7,7 @@ os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) ...@@ -7,7 +7,7 @@ os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
setup( setup(
name='django-hshassets', name='django-hshassets',
version='2.2.18', version='2.2.21',
packages=find_packages(), packages=find_packages(),
include_package_data=True, include_package_data=True,
license='MIT License', license='MIT License',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment