Last active
September 19, 2017 08:28
-
-
Save chasingmaxwell/7324854 to your computer and use it in GitHub Desktop.
Drupal 7: Add HTML5 required attribute to all form elements with '#required' => TRUE.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This goes in YOUR_MODULE.module file. | |
*/ | |
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) { | |
// Always add the html5 required attribute for required elements. | |
_YOUR_MODULE_html5_required($form); | |
} | |
function _YOUR_MODULE_html5_required($element) { | |
if (is_array($element)) { | |
if ((isset($element['#type']) && $element['#type'] !== 'checkboxes') && isset($element['#required']) && $element['#required']) { | |
$element['#attributes']['required'] = ''; | |
} | |
else { | |
foreach (element_get_visible_children($element) as $key) { | |
_YOUR_MODULE_html5_required($element[$key]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function _YOUR_MODULE_html5_required($element)
should befunction _YOUR_MODULE_html5_required(&$element)
.