[1.10.x] Fixed #27299 -- Documented the Widget.use_required_attribute() method.

Thanks Tim Graham for the review and edits.

Backport of c74378b from master
This commit is contained in:
Jon Dufresne 2016-10-22 08:15:33 -07:00
parent 72d174bc4e
commit dda9a5928a
2 changed files with 27 additions and 1 deletions

View file

@ -287,6 +287,29 @@ foundation for custom widgets.
an HTML form submission, so it's unknown whether or not the user
actually submitted a value.
.. method:: use_required_attribute(initial)
.. versionadded:: 1.10.1
Given a form field's ``initial`` value, returns whether or not the
widget can be rendered with the ``required`` HTML attribute. Forms use
this method along with :attr:`Field.required
<django.forms.Field.required>` and :attr:`Form.use_required_attribute
<django.forms.Form.use_required_attribute>` to determine whether or not
to display the ``required`` attribute for each field.
By default, returns ``False`` for hidden widgets and ``True``
otherwise. Special cases are :class:`~django.forms.ClearableFileInput`,
which returns ``False`` when ``initial`` is not set, and
:class:`~django.forms.CheckboxSelectMultiple`, which always returns
``False`` because browser validation would require all checkboxes to be
checked instead of at least one.
Override this method in custom widgets that aren't compatible with
browser validation. For example, a WSYSIWG text editor widget backed by
a hidden ``textarea`` element may want to always return ``False`` to
avoid browser validation on the hidden field.
``MultiWidget``
---------------