#27370: Django's Select widget adds a required="required" attribute, even if
created with empty_label=True
-------------------------+-------------------------------------------------
Reporter: | Owner: nobody
alexpirine |
Type: Bug | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Keywords: HTML,Select,wdiget,ModelChoiceField
Triage Stage: | Has patch: 0
Unreviewed |
Easy pickings: 0 | UI/UX: 0
-------------------------+-------------------------------------------------
I didn't think it was wrong in HTML, but the W3C validator says so, and
after reading this discussions, 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>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27370>
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/053.268892837ff52ce7fbf34cb3eb8c742e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.