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.

Comments

joseph.olstad created an issue. See original summary.

joseph.olstad’s picture

idebr’s picture

Please 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

andrewmacpherson’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Accessibility, +Regression, +Needs accessibility review

I 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.

joey91133’s picture

StatusFileSize
new1.14 KB

update for Drupal 7.99

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.