#36317: ModelChoiceField cannot be hidden by default
-----------------------------+--------------------------------------
     Reporter:  FloppyDisco  |                     Type:  Bug
       Status:  new          |                Component:  Forms
      Version:  5.2          |                 Severity:  Normal
     Keywords:               |             Triage Stage:  Unreviewed
    Has patch:  0            |      Needs documentation:  0
  Needs tests:  0            |  Patch needs improvement:  0
Easy pickings:  0            |                    UI/UX:  0
-----------------------------+--------------------------------------
 hello all, I have run into something that feels like a bug to me, but i
 could be wrong there may be a reason for this behavior.

 I have added a custom action form to my Admin page.
 my goal is to default these fields to hidden and only show them when the
 associated action is selected.
 I have accomplished this with JS, however there is a delay before it is
 loaded and there is a small layout shift.
 the form contains two fields, the relevant code is below.

 {{{
   choice = ModelChoiceField(
         ...
         widget=Select(
             attrs={
                 "style": "display: none;"
             }
         ),
     )

   date = DateField(
         ...
         widget=DateInput(
             attrs={
                 "style": "display: none;",
             }
         ),
     )
 }}}


 the DateInput is appropriately hidden when the template is rendered and
 requires no JS to hide the input initially.
 however, because the way dropdowns are rendered in html. the
 ModelChoiceField is not correctly hidden.

 the rendered html basically looks like this:
 {{{

 <label>
   <select style="display: none;" />
   <span />
 </label>
 }}}

 the problem is that the `<select>` element is hidden by default and the
 `<span>` is what the user actually interacts with.

 so the expected behavior is that when `display: none;` is set on the
 widget, the widget is hidden.

 it would be nice if Django was smart enough to set `display: none;` on all
 the associated elements to the widget for this particular case.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36317>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019620d2af87-ac98ac36-32bb-43b2-a309-703ebe3710b4-000000%40eu-central-1.amazonses.com.

Reply via email to