Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep wrapper in hidden field #164

Open
nerdoc opened this issue Nov 29, 2023 · 1 comment
Open

keep wrapper in hidden field #164

nerdoc opened this issue Nov 29, 2023 · 1 comment

Comments

@nerdoc
Copy link

nerdoc commented Nov 29, 2023

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 - 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_<field-name>.

But, and this is my problem, if crispy renders a "hidden field", it just renders the field without wrapper.

{% if field.is_hidden %}
{{ field }}
{% else %}

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:

{% 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 }}
    </{% if tag %}{{ tag }}{% else %}div{% endif %}>
{% else %}

I expanded it a bit for better readability.
What do you think about that?

@nerdoc
Copy link
Author

nerdoc commented Nov 29, 2023

Another possibility would be a DynamicField wrapper for Crispy fields, that holds that possibility, like the equivalent does for form fields in django-forms-dynamic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant