Feature proposal: ToggleField and Toggle widget for Django Forms

Hello all!
I want to propose a new feature for handling so-called toggle buttons on the front end.

Those elements are quite common and are widely used in many frameworks, such as Bootstrap and Material UI.

Those could be narrowed down to the BooleanField but with several key differences:

  • They never be required, even if defied like so;
  • By default, they should be not required;
  • They should always return cleaned data to be False if not passed in the form data;
  • They should always have an initial value of False unless explicitly defined;
  • On invalid form data, those should not raise validation errors, but cast the value to False;
  • They have a special widget, based on the CheckboxInput, but adding role="switch"/part of the WAI-ARIA/ and aria-checked="false/true"/based on the value/. Also, maybe more controls?

The following is not that hard to achieve but is going to save a lot of time to defining such field for every project.

It is common practice for proposals such as this to first be created and distributed as a third-party package. This allows for people to take a look at it and to try it, to see if it does satisfy a particular need. This also allows for some time for the code and APIs to be stabilized at a rate more appropriate for a new feature.