Problem/Motivation
HTML5 offers a "required" form element attribute for browser-native client-side validation.
Proposed resolution
Using the #required Form API attribute, add either of the following to an element:
TRUE: required="required" aria-required="true"
FALSE: [nothing]
For buttons that shouldn't require validation (like a 'Previous' button on a multi-paged form), a 'formnovalidate' attribute can be added to its #attributes property, which would output like this:
TRUE: formnovalidate="formnovalidate"
FALSE: [nothing]
Remaining tasks
Commit the patch from #1174938-93: Natively support the HTML5 required and aria-required FAPI properties (attached)
User interface changes
HTML5 browsers will validate form elements that are marked as required, giving the user feedback on empty fields as they submit the form.
API changes
This patch adds the following options to the Form API:
$form['myelement']['#attributes']['required'] = TRUE;
$form['mysubmitbutton']['#attributes']['formnovalidate'] = TRUE;
Original report by Dave Reid
Currently adding a #required => TRUE to a form element gives it a class, but now with HTML5 we can put the 'required' as a direct attribute in the form element itself.
http://diveintohtml5.org/forms.html#required http://diveintohtml5.info/forms.html#required
--
We're going to need a follow up issues for a required/toolbar bug.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 1174938-required-backported_to_D7-99.patch | 1.14 KB | joey91133 |
| 1174938-required-backported_to_D7-93.patch | 1.26 KB | joseph.olstad |
Comments
Comment #2
joseph.olstadComment #3
idebr commentedPlease note this introduced a major regression in D8 that has not been fixed yet: #1797438: HTML5 validation is preventing form submit and not fully accessible
Comment #4
andrewmacpherson commentedI realize this is about backporting an established D8 behaviour to D7, but I'm very wary of this commiting because we don't yet have a consensus on how to address the accessibility regression in #1797438: HTML5 validation is preventing form submit and not fully accessible, and it may well have to wait until D9.
Strictly speaking, I'm only the accessibility maintainer for D8, but I would say no to commiting this. It would introduce an accessibilty problem to the majority of existing D7 sites, which are currently doing fine with server-side validation.
Comment #5
joey91133 commentedupdate for Drupal 7.99