I'm using webform 7.x-3.18 module. My form contains some mandatory fields,drop down list with ajax calls.
I got the form id of my webform.The issue here is whenever I selects any drop down list which has an ajax call, and submit without entering any values in the mandatory fields, the mandatory functionality works, but the template of my form gets changed.When I debugged the code I found out the form id of my webform getting changed each and every time when I selects a drop down list and enters submit (webform-client-form-87 to webform-client-form-87--4) .The changed id is also not constant as the last digit(4) keeps on changing every time I selects different drop down. Below is my code:

<?php
function modulename_form_alter(&$form, &$form_state, $form_id) {
 if ($form_id == 'webform_client_form_87') {

 $uri = request_uri();

#first drop down
#machine name - sal_type

  $form['submitted']['sal_type']['#prefix'] = '<div class="field_container_sal_type">';
  $form['submitted']['sal_type']['#suffix'] = '</div>';
  $form['submitted']['sal_type']['#options'] = get_default_uri($uri, 'sal_type');
  $typeselected = isset($form_state['values']['submitted']['sal_type']) ? $form_state['values']['submitted']['sal_type'] : '';
  $form['submitted']['sal_type']['#default_value'] = $typeselected;

#onchange with ajax

  $form['submitted']['sal_type']['#ajax'] = array('event' => 'change', 'callback' => 'change_saltype', 'wrapper' => 'dropdown-subsector-replace', 'method' => 'replace');
#validate element on submit
  $form['submitted']['sal_type']['#element_validate'] = array('modulename_custom_validation');

#second drop down
#machine name - sub_sector_type

  $form['submitted']['sub_sector_type']['#prefix'] = '<div id="dropdown-subsector-replace" class="field_container_sub_sector_type">';
  $form['submitted']['sub_sector_type']['#suffix'] = '</div>';
  $form['submitted']['sub_sector_type']['#options'] =get_sub_sector_type_dropdown_options($typeselected, $uri);
  $form['submitted']['sub_sector_type']['#default_value'] = isset($form_state['values']['submitted']['sal_type']) ? $form_state['values']['submitted']['sal_type'] : '';
#validate element on submit
  $form['submitted']['sub_sector_type']['#element_validate'] = array('modulename_custom_validation');

}

function get_default_uri($uri, $a = ''){
#returns list and set values
  if($a == 'sal_type'){
  $options['option1'] = 'a';
  $options['option2'] = 'b';
  $options['option3'] = 'c';
 }
return $options;
}

function change_saltype($form,$form_state){

  return $form['submitted']['sub_sector_type'];
}

function get_sub_sector_type_dropdown_options($key='',$uri){
#populates the values from db based on the selected value on first drop down
  
$options=get_default_uri($uri);
  $subsector=db_select('sub_sector_type','c') -> fields('c') -> condition('type', $key, '=') -> orderBy('c.sub_sector', 'ASC') -> execute();

  foreach ($subsector as $subsectors) {
  $options[$subsectors -> sub_sector] = t($subsectors -> sub_sector);
  }

 return $options;
}


function modulename_custom_validation($element){
  $errors = array(
    'sal_type' => t('Select salary type'),
    'sub_sector_type' => t('Select sector type')
  );
  $value =$element['#value'];
  
 //print_r($element);die;
  if(isset($errors[$element['#webform_component']['form_key']])){
  	form_error($element, $errors[$element['#webform_component']['form_key']]);
  }
}

?>

Any help on this would be great as I am kinda new to drupal.
Thanks in advance!

Comments

DanChadwick’s picture

Status: Active » Fixed

As explained in the issue submission guidelines, we don't supply custom programming support in this issue queue. Try the forum, IRC, or Drupal Answers.

Also, I recommend that you update to 7.x-4.x, if possible, if you are building custom code.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.