Search code examples
phpdrupal-7drupal-modules

Ajax based Multi select dropdown not working with file field - drupal 7


I have added a file upload field to my node and I am editing the node form in a custom module as shown in below code .

There is an ajax based dependent drop-down field in the form.Upon selecting a single value the dependent drop-down loads fine. But I get the following error when I select multiple values from the drop-down:

An illegal choice has been detected. Please contact the site administrator

I get this particular error because of the file upload field.When I remove it , the form works absolutely fine.

I couldn't found any clues to solve this problem so far.

My code is Below:-

<?php
function my_module_form_alter(&$form, &$form_state, $form_id) {
    global $user;
    switch ($form_id) {
        case 'my_node_node_form':
            form_load_include($form_state, 'inc', 'node', 'node.pages');
            $country_list = load_countries();
            $selected_country = isset($form_state['values']['course_country']) ? $form_state['values']['course_country'] : key($country_list);
            $form['course_country'] = array(
                '#type' => 'select',
                '#title' => t('Select Your Country'),
                '#weight' => 11,
                '#options' => $country_list,
                '#default_value' => $selected_country,
                '#ajax' => array(
                    'callback' => 'city_dropdown_callback',
                    'wrapper' => 'city_wrapper_list',
                ),
                '#multiple' => TRUE,
                '#required' => TRUE,
            );

            $form['course_country_region'] = array(
                '#type' => 'select',
                '#title' => t('Select Your City'),
                '#weight' => 12,
                '#prefix' => '<div id="city_wrapper_list">',
                '#suffix' => '</div>',
                '#options' => load_cities($selected_country),
                '#multiple' => TRUE,
                '#required' => TRUE,
            );

            unset($form['field_upload_resouces']);
            break;
    }
}


function city_dropdown_callback($form, $form_state) {
    return $form['course_country_region'];
}


function load_countries(){
    $sel_query = db_select('country', 'cd');
    $sel_query->fields('cd');
    $result = $sel_query->execute();
    while ($data = $result->fetchAssoc()) {
        $country_list[$data['id']] = $data['name'];
    }
    return $country_list;
}


function load_cities($country_id) {
    $region_list = array('any' => 'Any');

    $sel_query = db_select('city', 'cd');
    $sel_query->fields('cd');
    if(is_array($country_id)){
       $sel_query->condition('cd.country_id', $country_id, 'IN');
    }else{
        $sel_query->condition('cd.country_id', $country_id);    
    }

    $result = $sel_query->execute();
    while ($data = $result->fetchAssoc()) {
        $city_list[$data['id']] = $data['name'];
    }

    return $city_list;
} 

Solution

  • I had similar issue and I solved by updating jquery.form.js to the latest version.

    Please refer the link https://www.drupal.org/node/153774#comment-9202403 for more details.