Add field override for bottom margin when field wrapper_class string contains variations on 'mb-' or 'my-' or 'm-' - #219
Open
nordmichael wants to merge 1 commit into
Conversation
… bottom margin, leading to all form fields having a bottom margin of mb-3 even if a wrapper_class is specified that should replace mb-3
Author
smithdc1
reviewed
Aug 10, 2026
smithdc1
left a comment
Member
There was a problem hiding this comment.
Thanks for the patch.
I have spotted one regression that we should fix and then have a couple of suggestions.
We should also document this as it's likely to be a breaking change for some users. I'd add a release note.
| <div class="{% for offset in bootstrap_checkbox_offsets %}{{ offset|slice:"7:14" }}{{ offset|slice:"4:7" }}{{ offset|slice:"14:16" }} {% endfor %}{{ field_class }}"> | ||
| {% endif %} | ||
| {% endif %} | ||
| <{% if tag %}{{ tag }}{% else %}div{% endif %} id="div_{{ field.auto_id }}" class="{% if field|is_checkbox and form_show_labels %}form-check{% else %}mb-3{% if 'form-horizontal' in form_class %} row{% endif %}{% endif %}{% if wrapper_class %} {{ wrapper_class }}{% endif %}{% if field.css_classes %} {{ field.css_classes }}{% endif %}"> |
Member
There was a problem hiding this comment.
This space that got removed is important.
If a layout were:
test_form.helper.layout = Layout(
Field('fruit', wrapper_class="custom-class")
)We now get:
<div id="div_id_fruit" class="mb-3custom-class">It seems we don't have a test for this, we should add one.
| {% endif %} | ||
| {% endif %} | ||
| <{% if tag %}{{ tag }}{% else %}div{% endif %} id="div_{{ field.auto_id }}" class="{% if field|is_checkbox and form_show_labels %}form-check{% else %}mb-3{% if 'form-horizontal' in form_class %} row{% endif %}{% endif %}{% if wrapper_class %} {{ wrapper_class }}{% endif %}{% if field.css_classes %} {{ field.css_classes }}{% endif %}"> | ||
| <{% if tag %}{{ tag }}{% else %}div{% endif %} id="div_{{ field.auto_id }}" class="{% if field|is_checkbox and form_show_labels %}form-check{% else %}{% if wrapper_class and ' mb-' in ' '|add:wrapper_class or wrapper_class and ' my-' in ' '|add:wrapper_class or wrapper_class and ' m-' in ' '|add:wrapper_class %}{% else %}mb-3{% endif %}{% if 'form-horizontal' in form_class %} row{% endif %}{% endif %}{% if wrapper_class %}{{ wrapper_class }}{% endif %}{% if field.css_classes %} {{ field.css_classes }}{% endif %}"> |
Member
There was a problem hiding this comment.
We could use with to avoid many string concatenations.
Suggested change
| <{% if tag %}{{ tag }}{% else %}div{% endif %} id="div_{{ field.auto_id }}" class="{% if field|is_checkbox and form_show_labels %}form-check{% else %}{% if wrapper_class and ' mb-' in ' '|add:wrapper_class or wrapper_class and ' my-' in ' '|add:wrapper_class or wrapper_class and ' m-' in ' '|add:wrapper_class %}{% else %}mb-3{% endif %}{% if 'form-horizontal' in form_class %} row{% endif %}{% endif %}{% if wrapper_class %}{{ wrapper_class }}{% endif %}{% if field.css_classes %} {{ field.css_classes }}{% endif %}"> | |
| <{% if tag %}{{ tag }}{% else %}div{% endif %} id="div_{{ field.auto_id }}" class="{% if field|is_checkbox and form_show_labels %}form-check{% else %}{% if wrapper_class %}{% with wc=' '|add:wrapper_class %}{% if ' mb-' in wc or ' my-' in wc or ' m-' in wc %}{% else %}mb-3{% endif %}{% endwith %}{% else %}mb-3{% endif %}{% if 'form-horizontal' in form_class %} row{% endif %}{% endif %}{% if wrapper_class %} {{ wrapper_class }}{% endif %}{% if field.css_classes %} {{ field.css_classes }}{% endif %}"> |
| test_form.helper = FormHelper() | ||
| test_form.helper.layout = Layout( | ||
| Field('fruit', wrapper_class="mb-0") | ||
| ) |
Member
There was a problem hiding this comment.
Add tests for the other options too?
Suggested change
| ) | |
| Field('fruit', wrapper_class="custom-class"), | |
| Field('fruit', wrapper_class="m-4"), | |
| Field('fruit', wrapper_class="my-4"), | |
| Field('fruit', wrapper_class="my-4 custom-class"), | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Existing field template does not provide any mechanism for overriding bottom margin, leading to all form fields having a bottom margin of mb-3 even if a wrapper_class is specified that should replace mb-3