#27370: Django's Select widget adds a required="required" attribute, even if
created with empty_label=True
-------------------------------------+-------------------------------------
Reporter: alexpirine | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage:
HTML,Select,wdiget,ModelChoiceField| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by alexpirine:
Old description:
> I didn't think it was wrong in HTML, but the W3C validator says so, and
> after reading this discussion, it seems to be right:
>
> https://github.com/validator/validator/issues/47
>
> This is how I could explain it:
>
> If a form field is required and has the `empty_label=True` argument, the
> generated `<select>` will have a `required="required"` attribute. This
> doesn't make sense since the first item will be selected anyways, and
> there is no way for the user to ''unselect'' a value.
>
> So, the `required="required"` attribute is useless.
>
> Example:
>
> {{{#!python
> class TestForm(forms.Form):
> some_field = forms.ModelChoiceField(queryset=SomeModel.objects.all(),
> empty_label=None)
> }}}
>
> results in:
>
> {{{#!text/html
> <select id="id_some_field" name="some_field" required>
> <option value="1">Value 1</option>
> <option value="2">Value 2</option>
> <option value="3">Value 3</option>
> <!-- ... -->
> </select>
> }}}
>
> You can check the following code using the
> [https://validator.w3.org/#validate_by_input W3C validation service]:
>
> {{{#!text/html
> <!DOCTYPE html>
> <html lang="en">
> <head>
> <meta charset="utf-8">
> <title>Validation test</title>
> </head>
> <body>
> <select id="id_some_field" name="some_field" required>
> <option value="1">Value 1</option>
> <option value="2">Value 2</option>
> <option value="3">Value 3</option>
> <!-- ... -->
> </select>
> </body>
> </html>
> }}}
New description:
I didn't think it was wrong in HTML, but the W3C validator says so, and
after reading this discussion, it seems to be right:
https://github.com/validator/validator/issues/47
This is how I could explain it:
If a form field is required and has the `empty_label=True` argument, the
generated `<select>` will have a `required="required"` attribute. This
doesn't make sense since the first item will be selected anyways, and
there is no way for the user to ''unselect'' a value.
So, the `required="required"` attribute is useless.
Example:
{{{#!python
class TestForm(forms.Form):
some_field = forms.ModelChoiceField(queryset=SomeModel.objects.all(),
empty_label=None)
}}}
results in:
{{{#!text/html
<select id="id_some_field" name="some_field" required>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
<!-- ... -->
</select>
}}}
You can check the following code using the
[https://validator.w3.org/#validate_by_input W3C validation service]:
{{{#!text/html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Validation test</title>
</head>
<body>
<select id="id_some_field" name="some_field" required>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
<!-- ... -->
</select>
</body>
</html>
}}}
In order to fix this, the generated `<select>` widget should not have a
`required` attribute.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/27370#comment:2>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/068.d508c1811254949aafcb438cc326c373%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.