Drupal 7 #ajax element giving error An illegal choice has been detected. Please contact the site administrator.

Events happening in the community are now at Drupal community events on www.drupal.org.
Mandakini_Kumari's picture

I am doing select of country,state and city using ajax element and getting message "An illegal choice has been detected. Please contact the site administrator." if passing $selected_state

If I use ajax_example_get_third_dropdown_options(2) it works but give error for ajax_example_get_third_dropdown_options($selected_state)

'#ajax' => array( '#options' => ajax_example_get_third_dropdown_options($selected_state),
'#default_value' => $profile_country_residence_city_id,
),

Comments

Reason for this error message

brajendrasingh's picture

Reason for this error message is may be exact replacement of wrappers. Please check your wrapper id's which you used for list drop down. I think this is not due to function parameter.

Drupal 7 Example module

Mandakini_Kumari's picture

I am adding one more step of select on url http://localhost/mysite/examples/ajax_example/dependent_dropdown
below is my script. Please help me to correct it
<?
function ajax_example_dependent_dropdown($form, &$form_state) {
// Get the list of options to populate the first dropdown.
$options_first = _ajax_example_get_first_dropdown_options();
$selected = isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first);
$selected3 = isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : 'Violin';

$form['dropdown_first'] = array(
'#type' => 'select',
'#title' => 'Instrument Type',
'#options' => $options_first,
'#default_value' => $selected,
'#ajax' => array(
'callback' => 'ajax_example_dependent_dropdown_callback',
'wrapper' => 'dropdown-second-replace',
),
);

$form['dropdown_second'] = array(
'#type' => 'select',
'#title' => $options_first[$selected] . ' ' . t('Instruments'),
'#prefix' => '',
'#suffix' => '',
'#options' => _ajax_example_get_second_dropdown_options($selected),
'#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
'#ajax' => array(
'callback' => 'ajax_dependent_dropdown_second_callback',
'wrapper' => 'dropdown-third-replace',
)
);

$form['dropdown_third'] = array(
'#type' => 'select',
'#title' => 'Select City',
'#prefix' => '',
'#suffix' => '',
'#options' => _ajax_third_dropdown($selected3),
'#default_value' => isset ($form_state['value']['dropdown_third']) ? $from_state['value']['dropdown_third'] : '',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}

function ajax_example_dependent_dropdown_callback($form, $form_state) {
return $form['dropdown_second'];
}
function ajax_dependent_dropdown_second_callback($form, $form_state) {
return $form['dropdown_third'];
}
function _ajax_third_dropdown($key = '') {
$options =array();
$options = array(
t('Violin') => drupal_map_assoc(array(t('Violin'), t('Viola'), t('Cello'), t('Double Bass'))),
t('Viola') => drupal_map_assoc(array(t('Flute'), t('Clarinet'), t('Oboe'), t('Bassoon'))),
t('Cello') => drupal_map_assoc(array(t('Trumpet'), t('Trombone'), t('French Horn'), t('Euphonium'))),
t('Double Bass') => drupal_map_assoc(array(t('Bass Drum'), t('Timpani'), t('Snare Drum'), t('Tambourine'))),
);
if (isset($options[$key])) {
return $options[$key];
}
else {
return array();
}
}

?>

Mandakini Kumari

*_*

beautifulmind's picture

You get the error because Drupal thinks the form has been altered illigally.
Please make sure that you populate the boxes in proper way and format.

Regards.

correction in code

Mandakini_Kumari's picture

Yes you are right. It happen in normal form too. Can you tell me what mistake I am doing in above coding ?

Mandakini Kumari

*_*

beautifulmind's picture

Where is following wrappers in the form?

'wrapper' => 'dropdown-second-replace',

*_*

Drupal 7 Example module

Mandakini_Kumari's picture

conditional_fields is not available in D7

Mandakini Kumari

*_*

beautifulmind's picture

Yes it is. But its dev release, you can give it a try.

Regards.

same problem here!

hablutzel1's picture

I have the same problem but it only happen when you enable cache for anonymous user, try to disable the page cache for anonymous users, clean the cache and test again.

This happens when the options

vaibhavjain's picture

This happens when the options passed in the select box does not meet with the ones provide while submitting the form.
Like if options are 1,2,3,4 and we get the value of that field as 5, it will not pass the form validation.

As stated you are using AJAX, i believe you should try to rebuild the form when returning the rebuild element via AJAX call.

Vaibhav Jain

if you have one combo box

hablutzel1's picture

if you have one combo box that depends on another (using ajax) and you have anononymous users cache enabled, the first anonymous user that exercise the form will configure the dependant combobox, so if another user gets to the page and use the parent combobox there in cache will be something from the previous user and the validation will fail, look at this way to reproduce the problem in Drupal 7.14:

  1. Enable cache for anonymous users.

  2. Create a module with contents like the following:

<?php
function module_test1_menu()
{
   
// This is the minimum information you can provide for a menu item.
   
$items['module_test1'] = array(
       
'title' => 'menu item',
       
'page callback' => 'drupal_get_form',
       
'page arguments' => array('module_test1_form1'),
       
'access arguments' => array('access content'),
    );

    return
$items;
}


function
module_test1_form1_foo_callback($form, $form_sate)
{
    return
$form['bar'];
}




function
module_test1_form1($form, &$form_state)
{
   
$form['foo'] = array(
       
'#type' => 'select',
       
'#title' => 'foo',
       
'#options' => array("1" => "1", "2" => "2"),
       
'#ajax' => array(
           
'callback' => 'module_test1_form1_foo_callback',
           
'wrapper' => 'module_test1_form1_bar-wrapper',
        )
    );
   
$randValue = rand(0, 10);
   
$form['bar'] = array(
       
'#type' => 'select',
       
'#title' => 'bar',
       
'#options' => array($randValue => $randValue),
       
'#prefix' => '
<div id="module_test1_form1_bar-wrapper">'
,
       
'#suffix' => '</div>
'
,
    );
    return
$form;
}
?>
  1. Visit the internal link module_test1 as an anonymous user

  2. Exercise foo combobox

  3. Open the same link in another web browser, then exercise foo combobox and you will receive the error:

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

Because the cached version of the webpage had another 'options' for the dependant combobox, I think this is a bug or at least a limitation when there are anonymous users, but as I'm pretty new to drupal I could be wrong.

NOTE: I just tested this against 7.x trunk and the error keeps coming, should I fill a ticket?

Ajax Blocks

hablutzel1's picture

Ajax Blocks (http://drupal.org/project/ajaxblocks) module overcomes the above problem, but anyway drupal should manage this kind of oddities

I would suggest also to check

RKumar-dupe's picture

I would suggest also to check with Drupal core less than 7.11, as the later cores have many problems.

Synchronize

Reno Greenleaf's picture

Both forms ($form array in backend and frontend HTML form) should be equal. But in AJAX callback you can alter only frontend form (backend $form is inaccessible).

The question is how to access the backend array to alter it the same way as frontend form.

Maybe $form and $form_state in AJAX callback should be references to the actual data.

Solution

Reno Greenleaf's picture

You can use form_set_cache. See the example.

<?php
//The callback.
function my_ajax_callback($form, $form_state) {
 
$form['new_element'] = array('#type' => 'textfield');//Alter the form.
 
form_set_cache($form['#build_id'], $form, $form_state);//Update it in backend.
 
return $form['new_element'];//Update it in frontend.
}
?>

i added ‘#validated’ =>

blogook's picture

i added

‘#validated’ => TRUE

to my form and problem is solved.

where exactly ‘#validated’ =>

mbahojlo's picture

where exactly ‘#validated’ => TRUE was placed? I have same issue and cant find out how to solve it

#validate

balintcsaba's picture

The #validate solve my problem to. The place to use is the new element you change with the ajax.

#validated

berekel's picture

#validated parameter didn't worked for me until i placed
$form_state['rebuild'] = TRUE;
after that, so my code looked like this

function MYMODULE_form_alter(&$form,&$form_state,$form_id){
  $form['my_field']['#validated'] = TRUE;
  $form_state['rebuild'] = TRUE;
}

This solved my problem, thx

lagvjjt's picture

This solved my problem, thx

will this work for Drupal 8?

isalmanhaider's picture

This seems for Drupal7
What would be its alternate solution for Drupal8?

#validated

prajaankit's picture

i am making the taxonomy dependent drop down for that we make the contenttype alter and get the values but when we select the value of parent term desire value come in my child field. and error massage
An illegal choice has been detected. Please contact the site administrator.
we use all the things

validate

default value

rebuild the form

but error is not resolved any one please help me,
i am using shs module currently but my work is stop i need the value from above type

Other AJAX elements on the form

neelam.chaudhary's picture

This happens when there are other AJAX elements on the form like file upload or some AJAX elements other than the dependent and dependee dropdowns.

e.g. Initially dropdown A has values '1' => 'abc',
'2'=> 'def'
'3' => 'ghi'

Initially dropdown B has values 'a' => '123',
'b'=> '456'
'c' => '789'

When the form is being loaded the form gets some fixed values in the dependent dropdown.

Then after altering its values depending upon the value of the dependent dropdown the form now has these new values in the dropdown.

e.g. If dropdown A has value 'def' as selected value and dropdown B has been changed to
'd' => '789',
'e'=> '012'

After this when submitting, the form gets different set of values it had after alteration of the values.
Something like 'f' => '684',
'a'=> '098'
This happens because the values has been altered by some other AJAX element.
To prevent the values of the depenedent dropdown being changed by other AJAX element.

Alter the options only when the form element alter AJAX is being fired not by any other AJAX on the form.

if (!isset($form_state['input']['_triggering_element_value']) || ((isset($form_state['input']['_triggering_element_value']) && $form_state['input']['_triggering_element_value'] == 'your_ajax_triggering_element_name'))) {

// Alter your dependent dropdown options here

}

this worked for me

nixar's picture

In my case I have a form using multistep that an ajax dependent dropdown and 2 field collections with unlimited cardinality which together caused the same error message when I add 2 field collection items.

Note that I had to set up a condition with $form_state['input']['_triggering_element_name'] and not *_value like in the example above.

My solution in same

rostost's picture

My solution in same situation:

  • prepare all possible options, set them to selectlist and make it disabled
  • during ajax call change disabled to enabled and show needed new selectlist (with limit of options)

BTW:
inside ajax call function, to change disabled attribute we need:

$element = $form['OUR_ELEMENT']

$element['#disabled'] = FALSE;
unset($element['#attributes']['disabled']);

return $element;

India

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: