django-crispy-forms / django-crispy-forms/crispy-bootstrap5

keep wrapper in hidden field

Open
#164 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
HTML
Stars
525
Forks
77
Avg merge
1d 1h
Merged PRs (30d)
2

Description

With the upcoming of HTML-over-the-wire like HTMX, Unpoly etc., we are facing new challenges.

I am constructing a generic form that has the possibility of updating itself dynamically, based on [django-forms-dynamic](https://pypi.org/project/django-forms-dynamic/) - but with HTMX functionality included.

This basically works perfectly with Crispy, with one except: When a conditional field triggers the absence of another field, I must "remove" that field from view. When the field is removed from the form itself, it is not rendered at all, and I don't have a tag with an id as HTMX target any more. I'd like to modifie django-forms-dynamic so that it just "hides" the field.
This only works only with django crispy, as crispy adds a functional wrapper to each field which automatically has a `id=div_id_`.

But, and this is my problem, if crispy renders a "hidden field", it just renders the field without wrapper.
https://github.com/django-crispy-forms/crispy-bootstrap5/blob/2737939d424da36ccca95fbf6e48c49f9bd4b7df/crispy_bootstrap5/templates/bootstrap5/field.html#L3-L5

This makes it impossible to use it as target and hide/show it dynamically.

So I have a suggestion, that may be considered impossible as it *may* break things, but would help crispy to be more consistent with hidden fields:

Would it be possible to also enclose the hidden field in a div that has the same id as the visible field?
this would be like:

```django
{% if field.is_hidden %}
<{% if tag %}{{ tag }}{% else %}div{% endif %}
id="div_{{ field.auto_id }}"
class="{% if wrapper_class %} {{ wrapper_class }}{% endif %}{% if field.css_classes %} {{ field.css_classes }}{% endif %}"
>
{{ field }}

{% else %}
```
I expanded it a bit for better readability.
What do you think about that?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.