Opened 7 years ago

Closed 7 years ago

#29036 closed Bug (fixed)

HTML5 required validation for SelectDateWidget doesn't work if the attribute is added by JavaScript

Reported by: Vlastimil Zíma Owned by: Vlastimil Zíma
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: Carlton Gibson Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

SelectDateWidget uses 0 as a empty value. When the field uses HTML5 required attribute to make browser check the input, it does not work. Browsers (tested with Firefox 52.5 and Chromium 62) consider selected 0 as filled, thus required check incorrectly passes.

I suggest to use empty string as a value for SelectDateWidget.none_value.

Attachments (1)

29036.diff (10.4 KB ) - added by Tim Graham 7 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 by Israel Saeta Pérez, 7 years ago

Hi Vlastimil!

I've tried to reproduce the issue you mention but it's not possible, since when the form field is required the empty label option is not rendered. From the documentation https://docs.djangoproject.com/en/2.0/ref/forms/widgets/#django.forms.SelectDateWidget:

If the DateField is not required, SelectDateWidget will have an empty choice at the top of the list

You can see this in the code as well:
https://github.com/django/django/blob/47d238b69602711c06c369a5555bb554a4b3f7fb/django/forms/widgets.py#L955-L995

Therefore what you mention can never happen I believe. Can you provide a code sample where you can reproduce the issue? Otherwise I'm in favor of marking this as invalid.

comment:3 by Tim Graham, 7 years ago

Easy pickings: unset
Resolution: needsinfo
Status: newclosed

comment:4 by Vlastimil Zíma, 7 years ago

Hi Israel,

we have a slightly more complicated use case, in which required attribute is added by JavaScript depending on other values in the form. Having 0 as a default value, makes things more complicated for us.

More cases might emerge based on browsers' behavior, which considers 0 as selected.

Also looking around for HTML5 specification, it looks like hiding the empty label for required select is not valid. According to https://www.w3.org/TR/html5/sec-forms.html#placeholder-label-option the empty labels must be present for selects with required attributes.

Based on Tims' closure, should I open a new ticket for that?

by Tim Graham, 7 years ago

Attachment: 29036.diff added

comment:5 by Tim Graham, 7 years ago

Resolution: needsinfo
Status: closednew
Summary: HTML5 required validation does not work for SelectDateWidgetHTML5 required validation for SelectDateWidget doesn't work if the attribute is added by JavaScript
Triage Stage: UnreviewedAccepted

I guess fixing the use case you mentioned might be feasible. I've attached an initial patch. More changes are required, for example SelectDateWidget.date_re must be adapted. Someone else can continue the work.

As for "hiding the empty label for required select is not valid", I'm not sure what exactly the spec is saying. For example, what's a "placeholder label option"? But yes, that seems like it would be a separate ticket, although it couldn't be addressed until this one is fixed, I think.

comment:6 by Vlastimil Zíma, 7 years ago

Owner: changed from nobody to Vlastimil Zíma
Status: newassigned

Thanks for reopening. I've split the invalid HTML into a separate #29056.

comment:7 by Vlastimil Zíma, 7 years ago

Has patch: set

PR submitted https://github.com/django/django/pull/9632

Instead of changing the date_re, I kept the pseudo-ISO format with zeros for the invalid dates. I consider it easier to debug in case an error should occur.

comment:8 by Carlton Gibson, 7 years ago

Cc: Carlton Gibson added
Triage Stage: AcceptedReady for checkin

This looks good to me.

I think leaving the date_re using 0 for invalid values makes sense.

comment:9 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In fbc3c29:

Fixed #29036 -- Fixed HTML5 required validation on SelectDateWidget if the attribute is added by JavaScript.

Thanks Tim Graham for the initial patch.

Note: See TracTickets for help on using tickets.
Back to Top