Add attributes in captcha response field

October 10, 2017
1K
12K


Category:
Tags:

We sometimes need to add placeholder or html5 form fields attributes to captcha response text field. We can easily add that as an array of attributes. We first need to alter the form to add our custom function in after_build of that form. So to do that do as following:

<?php
function MY_MODULE_form_alter(&$form, &$form_state$form_id) {
  if (
$form_id == 'MY_FORM_ID') {
    
$form['#after_build'][] = 'MY_captcha_alter';
  }
}

function 
MY_captcha_alter($form, &$form_state) {
  
$form['captcha']['captcha_widgets']['captcha_response']['#attributes'] = 
    array(
'required' => 'required',
      
'placeholder' => 'Your Answer');
  return 
$form;
}
?>