Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 295783

A required boolean field behaves differently depending on the widget

$
0
0

If a boolean field is set to 'required', the behaviour is different depending on the widget:

- single checkbox widget: the checkbox is shown as required. The user must enter the On value
- radios widget: a set of On / Off radios is shown. The user must enter either the On or the Off value

Use case

I want to add a boolean base field to an entity. I allow the form widget to be configurable. This field should always have a value, so I make it required.

Problem

Let's say you choose the radios widget. You get a 'yes' radio and a 'no' radio and you're forced to choose a value. Awesome! Okay, let's try the single checkbox widget. You get a checkbox, but you can't uncheck it or you get a validation error. Huh?!

Okay, so maybe if I lift the requirement on the field things will be better. Now the checkbox widget works as expected. However, the radios widget has 3 options now: 'yes', 'no', and 'N/A'. But I want this field to have a value, so that doesn't work.

Proposed solution

Add a widget setting to the checkbox widget so that the checkbox can optionally be marked required if the respective field is marked as required. If this setting is off, you are not forced to check it. Off is a valid state (as long as the value being used is an actual 'no' value and not NULL).

The new setting defaults to FALSE so that - by default - checkboxes will not be marked as required. This is a change to the previous behavior.

For all existing required boolean fields that are using the checkbox widget we set this setting to TRUE to match the previous behavior.


Viewing all articles
Browse latest Browse all 295783


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>