0

using hook_form_alter and custom form elements below

$form['get-quotes-shipping']['country'] = array(
      '#type' => 'select',
      '#options' => array('_none' => '- None -') + country_get_list(),
      '#multiple' => FALSE,
      '#weight' => 3,
      '#title'=>t('Country'),
      '#default_value' => (isset($_SESSION['selected_country'])) ? $_SESSION['selected_country'] : '_none',
      '#ajax' => array(
          'event' => 'change',
            'wrapper' => 'squadron-wrapper',
            'callback' => '_get_state_field',
            'method' => 'replace',
       ),
    );






    $form['get-quotes-shipping2']['actions']['get_quotes'] = array(
      '#type' =>'submit',
      '#value' => 'Get Quotes',
       '#submit' => array('custom_changes_get_quotes_submit'),
      '#validate' => array('custom_changes_get_quotes_validate'),
      '#weight' => 1,
    );


  $squadron_options = array();
   if (isset($_SESSION['selected_country'])) {
       module_load_include('inc', 'location');
       $states = location_get_provinces($_SESSION['selected_country']);
       $squadron_options =  $states;
   }
   $form['get-quotes-shipping0']['states']['#type'] = 'select'; 
     $form['get-quotes-shipping0']['states']['#title'] = t('State/Province');


   $form['get-quotes-shipping0']['states']['#prefix'] = '<div id="squadron-wrapper">';
   $form['get-quotes-shipping0']['states']['#suffix'] = '</div>';
    $form['get-quotes-shipping0']['states']['#options'] = array('_none' => '- None -') + $squadron_options;
    if (isset($squadron_options[0])) {
      $form['get-quotes-shipping0']['states']['#default_value'] =  $squadron_options[0];
    }

Callback function as given below.

function _get_state_field(&$form,&$form_state) {
  module_load_include('inc', 'location');
  if ($form_state['values']['country'] == '_none') {
    unset($form['get-quotes-shipping0']['states']['#options']);
    $form['get-quotes-shipping0']['states']['#default_value'] = '_none';
     return $form['get-quotes-shipping0']['states'];

  }
  else {
      $states = location_get_provinces($form_state['values']['country']);
 $form['get-quotes-shipping0']['states']['#options'] = $states;
     return $form['get-quotes-shipping0']['states'];

  }

}

When I select Country and then state at first it gives proper values but when I select country again it gives An illegal choice has been detected

What is the issue with the above code? can anyone point out?

2
  • 1
    Since you are unsetting $form['get-quotes-shipping0']['states']['#options'], that form element doesn't have anymore options; therefore, even the option you select after is correct, because it is not listed in $form['get-quotes-shipping0']['states']['#options'].
    – apaderno
    Feb 22, 2016 at 9:37
  • Thanks for the comments @kiamlalumno but even if I comment $form['get-quotes-shipping0']['states']['#options'] this it gives same error.I would appreciate your help again
    – harshal
    Feb 22, 2016 at 14:14

1 Answer 1

1

i recently made a 3 level dependent dropdown and had the same issue with some of the problems i found on the way. First you need to re-organize your code a little bit more. have in mind that the callback function should normaly look something like this:

//callback
    function _get_state_field(&$form,&$form_state) {

     return $form['get-quotes-shipping0']['states'];
}

Any code inside the callback has the potential to intervene with the proccess, with some exceptions.

Without taking a longer look to your code i think it should look somethink like this:

<?php

 module_load_include('inc', 'location');
  if ($form_state['values']['country'] == '_none') {
    unset($form['get-quotes-shipping0']['states']['#options']);
    $form['get-quotes-shipping0']['states']['#default_value'] = '_none';
     return $form['get-quotes-shipping0']['states'];

  }
  else {
      $states = location_get_provinces($form_state['values']['country']);
 $form['get-quotes-shipping0']['states']['#options'] = $states;
  }

$form['get-quotes-shipping']['country'] = array(
      '#type' => 'select',
      '#options' => array('_none' => '- None -') + country_get_list(),
      '#multiple' => FALSE,
      '#weight' => 3,
      '#title'=>t('Country'),
      '#default_value' => (isset($_SESSION['selected_country'])) ? $_SESSION['selected_country'] : '_none',
      '#ajax' => array(
          'event' => 'change',
            'wrapper' => 'squadron-wrapper',
            'callback' => '_get_state_field',
            'method' => 'replace',
       ),
    );


    $form['get-quotes-shipping2']['actions']['get_quotes'] = array(
      '#type' =>'submit',
      '#value' => 'Get Quotes',
       '#submit' => array('custom_changes_get_quotes_submit'),
      '#validate' => array('custom_changes_get_quotes_validate'),
      '#weight' => 1,
    );



   $form['get-quotes-shipping0']['states']['#type'] = 'select'; 
     $form['get-quotes-shipping0']['states']['#title'] = t('State/Province');


   $form['get-quotes-shipping0']['states']['#prefix'] = '<div id="squadron-wrapper">';
   $form['get-quotes-shipping0']['states']['#suffix'] = '</div>';
    $form['get-quotes-shipping0']['states']['#options'] = array('_none' => '- None -') + $squadron_options;
    if (isset($squadron_options[0])) {
      $form['get-quotes-shipping0']['states']['#default_value'] =  $squadron_options[0];
    }


    //callback
    function _get_state_field(&$form,&$form_state) {

     return $form['get-quotes-shipping0']['states'];
}
?>

This is the code i wrote, try to follow the same structure it's working for me perfectly.

Tell us how it went, have a good coding!

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.