Code

Opened 4 years ago

Closed 3 years ago

Last modified 6 weeks ago

#14372 closed Uncategorized (duplicate)

Admin shouldn't render label tags on hidden fields

Reported by: Mnewman Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords: admin hiddenfields forms
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

If a field in the admin is hidden, the label tag is still rendered. It would be pretty easy to not have that happen anymore. See patch attached.

Attachments (1)

14372-admin-hiddenfield.diff (797 bytes) - added by Mnewman 4 years ago.
Simple check to is_hidden to determine if we should render the label.

Download all attachments as: .zip

Change History (5)

Changed 4 years ago by Mnewman

Simple check to is_hidden to determine if we should render the label.

comment:1 Changed 4 years ago by ramiro

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

See also the patch attached to #7859.

comment:2 Changed 3 years ago by julien

  • Needs tests set
  • Patch needs improvement set

The patch is not ideal. With that patch, if the row contains just one field and that field is hidden, then you're left with an empty <div class="form-row title">. The patch should be improved to not display the row at all in this case.

comment:3 Changed 3 years ago by julien

  • Resolution set to duplicate
  • Status changed from new to closed

This is effectively a dupe of #11277.

comment:4 Changed 6 weeks ago by django@…

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

Aloha,

I use the following snippet as fieldset.html.

It not only hides the input field instead if the fieldset contains only a single element which input-type is set to hidden it also hides the surrounding div-elements. So no space is wasted.

The snippet inserts the style-attribute with display: none. Others may prefer to insert a css class. But this way no one has to edit the admins css.

Maybe someone could test if this solution has side effects in other cases.

Regards, lacki

<fieldset class="module aligned {{ fieldset.classes }}">
    {% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %}
    {% if fieldset.description %}
        <div class="description">{{ fieldset.description|safe }}</div>
    {% endif %}
    {% for line in fieldset %}
        <div class="form-row{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}"{% if line.fields|length_is:'1' %}{% for field in line %}{% if field.field.is_hidden %} style="display: none"{% endif %}{% endfor %}{% endif %}>
            {% if line.fields|length_is:'1' %}{{ line.errors }}{% endif %}
            {% for field in line %}
                <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}"{% endif %}{% if field.field.is_hidden %} style="display: none"{% endif %}>
                    {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
                    {% if field.is_checkbox %}
                        {{ field.field }}{{ field.label_tag }}
                    {% else %}
                        {# only show the label for visible fields #}
                        {% if not field.field.is_hidden %}
                        {{ field.label_tag }}
                        {% endif %}

                        {% if field.is_readonly %}
                            <p>{{ field.contents }}</p>
                        {% else %}
                            {{ field.field }}
                        {% endif %}
                    {% endif %}
                    {% if field.field.help_text %}
                        <p class="help">{{ field.field.help_text|safe }}</p>
                    {% endif %}
                </div>
            {% endfor %}
        </div>
    {% endfor %}
</fieldset>

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.