Wednesday, January 29, 2014

Rename '- None -' option from select list in a webform

When we creating a webform with select options, it gives the list with our options and '-None-' as the default empty value. If i choose it as a mandatory field it changes to '-Select-'. Recently a client of mine wants me to rename the '-None-' as '-Please Select-'. There is no way you can do that with the webform module. There comes an idea of implementing hook_form_alter.

  1. function yourmodule_form_alter(&$form, &$form_state, $form_id) {
  2.   if ($form_id == 'webform_client_form_your_webform_id') {
  3.       $form['submitted']['component_name']['#default_value'] = '';
  4.       $form['submitted']['component_name']['#empty_value'] = '';
  5.       $form['submitted']['component_name']['#empty_option'] = '- Please Select -';
  6. }

Thats it!!