diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt index 99514e46a2..c6148a5460 100644 --- a/docs/ref/forms/widgets.txt +++ b/docs/ref/forms/widgets.txt @@ -286,6 +286,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 + ` and :attr:`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`` --------------- diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index 1df1f0dcc4..3c21a703ee 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -858,7 +858,10 @@ Miscellaneous * Required form fields now have the ``required`` HTML attribute. Set the :attr:`Form.use_required_attribute ` attribute to ``False`` to disable it. You could also add the ``novalidate`` - attribute to ``
`` if you don't want browser validation. + attribute to ```` if you don't want browser validation. To disable + the ``required`` attribute on custom widgets, override the + :meth:`Widget.use_required_attribute() ` + method. * The WSGI handler no longer removes content of responses from ``HEAD`` requests or responses with a ``status_code`` of 100-199, 204, or 304. Most