Re: required radio buttons and checkboxes

(Note to editors: See below for suggested spec changes, in case WF2 is 
used in the future.)

Ben Boyle wrote:
> hi all,
> 
> I know we haven't got anything official about forms in the HTML5 docs
> yet, but in WF2 there is the @required attribute to flag when controls
> are required. I'm sure we're all looking forward to specifying
> required fields in a consistent and simple manner.
> 
> I was wondering how this works with radio buttons and checkboxes,
> given there are multiple 'input' elements for the controls, is the
> 'required' attribute needed on every input? What happens if it is
> inconsistently applied to a group?

My interpretation of http://www.whatwg.org/specs/web-forms/current-work/ is:

(Quoting from the spec:)
> For checkboxes, the required attribute shall only be satisfied when one
> or more of the checkboxes with that name in that form are checked.
> 
> For radio buttons, the required attribute shall only be satisfied when
> exactly one of the radio buttons in that radio group is checked.

The form will be invalid if any of its 'required' constraints is not 
satisfied. So it doesn't matter (in terms of form validity) whether you 
specify 'required' on one of the form controls in a group, or on all of 
them, since the presence of a single 'required' attribute will cause the 
validity check to be applied to that whole group of controls, and the 
validity check looks at all the controls regardless of whether they have 
'required'.

It has an effect on exactly where validity-related events go - if you 
mix required and not-required radio buttons in a single group, 'invalid' 
events will only be sent to those with a 'required' attribute; and the 
':invalid' pseudo-class will only apply to elements with 'required'.

But the suggested default behaviour for error reporting is to tell the 
user once about the whole radio group, regardless of how many elements 
have 'required':

> When a radio group has no checked radio button and more than one of the
> radio buttons is marked as required, the UA, when alerting the user,
> should only tell the user that the radio group as a whole is missing a
> value, not complain about each radio button in turn, even though all of
> the radio buttons marked with the required attribute would have the
> valueMissing flag set.

(Suggested spec change: Say "When a radio group does not have exactly 
one checked radio button and more than ...", to be consistent with the 
definition of "required" in the (maybe impossible?) case where more than 
one is checked.)

(Suggested spec change: Say "When a radio group does not have exactly 
one checked radio button, or a group of checkboxes with the same name 
does not have one or more checked checkboxes, and more than one of the 
group's form controls is marked as required, ...", because this should 
apply to checkboxes and not just radio buttons.)

So, in common usage, 'required' doesn't need to be specified 
consistently on every form control, and everything works the same as 
long as you have at least one 'required' control in each group.

-- 
Philip Taylor
pjt47@cam.ac.uk

Received on Tuesday, 22 April 2008 11:59:59 UTC