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)
Change History (9)
comment:1 by , 7 years ago
comment:3 by , 7 years ago
Easy pickings: | unset |
---|---|
Resolution: | → needsinfo |
Status: | new → closed |
comment:4 by , 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 select
s with required
attributes.
Based on Tims' closure, should I open a new ticket for that?
by , 7 years ago
Attachment: | 29036.diff added |
---|
comment:5 by , 7 years ago
Resolution: | needsinfo |
---|---|
Status: | closed → new |
Summary: | HTML5 required validation does not work for SelectDateWidget → HTML5 required validation for SelectDateWidget doesn't work if the attribute is added by JavaScript |
Triage Stage: | Unreviewed → Accepted |
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 , 7 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Thanks for reopening. I've split the invalid HTML into a separate #29056.
comment:7 by , 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 , 7 years ago
Cc: | added |
---|---|
Triage Stage: | Accepted → Ready for checkin |
This looks good to me.
I think leaving the date_re
using 0
for invalid values makes sense.
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:
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.