1

Could someone advise me how to use a hook to add the HTML 5 required attribute to all inputs that have been selected as 'required' within webforms.

Webforms already adds class 'required' but the HTML 5 required attribute provides great instant validation in Opera, Firefox and Chrome browsers.

  • Do you need a javascript solution or PHP or both ? You can do this by setting form attributes in a hook_form_alter() – AyeshK Oct 3 '12 at 23:41
  • PHP, could you produce an example please? – Scott Lawrence Oct 3 '12 at 23:43
  • See the answer below. – AyeshK Oct 4 '12 at 0:24
4

You will need to create a module for this. Note that, this answer assumes you need to set required attribute on all form elements which are required in web form settings. you will need to implement a different hook and/or add conditions to make this strict.

Create a file named webform_formalters.info and put following to it.

name = webform form alters
description = a collection of custom form alters for webforms. 
core = 7.x
package = Custom
dependencies[] = webform

Create another file named 'webform_formalters.module' and put following in it.

<?php
/* 
 * implements hook_form_BASE_FORM_ID_alter. 
 */
function webform_formalters_form_webform_client_form_alter(&$form){
  //dsm($form);
  foreach ($form['submitted'] as &$field){
   if (is_array($field) && isset($field[#required]) && $field['#required'] == 1){
     //dsm($field);
     $field['#attributes']['required'] = 'required';
   }
  }
}

Put both files to a folder named webform_formalters and upload to your site's module folder and enable the module. See if it works.

Unlike other forms, web forms' fields are stored in $form['submitted'] array. So we go through each item (which should be an array of form definitions) and check if the element has marked as required. if so, we add or merge required attribute. We are altering an existing form, so we are not going to pop out existing attributes.

Good luck!

| improve this answer | |
  • Thanks for your time with this. Out of interest why can't you perform this function in template.php? – Scott Lawrence Oct 4 '12 at 9:22
  • Hey @ayesh-k thanks for your help! I added this into template.php. The functionality works! But also produces this error message Notice: Undefined index: #required in innovista_form_webform_client_form_alter() (line 188 of /home/sites/innovista.org/public_html/beta/sites/all/themes/innovista/template.php). But only once per form. – Scott Lawrence Oct 4 '12 at 9:30
  • I fixed this by wrapping your if statement within if(isset($field['#required'])){. Is this acceptable? – Scott Lawrence Oct 4 '12 at 9:36
  • Yes it will work. But better if you put solenoid like this right into the existing if statement. if (is_array($field) && isset($field[#required]) && $field[#re ... – AyeshK Oct 4 '12 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.