Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By bostuh on
I have 2 dropdown select lists. The first a kanban process and the second a kanban step. When i select a kanban process the second dropdown should change based on the kanban process. But when i do i get an error: "An illegal choice has been detected. Please contact the site administrator."
function kanban_form_kanban_task_node_form_alter(&$form, &$form_state, $form_id) {
$options_step = $form['field_kanban_step_ref']['und']['#options'];
if($form)
$form['field_kanban_process']['und']['#ajax'] = array(
'event' => 'change',
'callback' => 'ajax_dependent_dropdown_callback',
'wrapper' => 'dropdown-second-replace',
'method' => 'replace',
);
$form['field_kanban_step_ref']['und']['#prefix'] = '<div id ="dropdown-second-replace">';
$form['field_kanban_step_ref']['und']['#suffix'] = '</div>';
$form['field_kanban_step_ref']['und']['#options'] = $options_step;
}
function ajax_dependent_dropdown_callback($form, $form_state) {
$process = $form['field_kanban_process']['und']['#value'];
$form['field_kanban_step_ref']['und']['#options'] = get_step_reference($process);
return $form['field_kanban_step_ref'];
}
function get_step_reference($kanban) {
$result = db_query('Select n.entity_id FROM {field_data_field_kanban_process_ref} n WHERE n.field_kanban_process_ref_target_id = :field_kanban_process_ref_target_id', array(':field_kanban_process_ref_target_id' => $kanban));
foreach($result as $record) {
$entity_id[] = $record->entity_id;
}
foreach($entity_id as $id) {
$result2 = db_query('Select t.title FROM {node} t WHERE t.nid = :nid', array(':nid' => $id));
foreach($result2 as $record2) {
$title[] = $record2->title;
}
}
return $title;
}
Comments
<?phpfunction
Two problems:
1) You are changing the $form element in your ajax callback above. This is causing the error you are seeing. Th only thing you should be doing in this function is returning the part of the form that should be inserted into the existing form. So everything in the above function that isn't part of the return statement should be part of your form definition.
2) You need to render the content that you return, using render().
Could you demonstrate how i
Could you demonstrate how i could do that?
If you download the examples
If you download the examples module, there is an ajax example in there. That will show you how to do it.
It should be mentioned that
It should be mentioned that this dependent dropdown is going to be used in the function hook_form_alter()
I have looked at the example.
I have looked at the example. But i'm totally frustrated now. Could you please show me how do it?
It's fine that it's in
It's fine that it's in hook_form_alter(). That doesn't make a difference.
Here's an example:
As you can see, any alterations to the form are done in the form definition, as explained in the comments. In the ajax callback, I have only returned the section to be rendered, and I rendered it using render().
Save the selected value
Now it works fine. It populates the right things. But the selected is not saved inside the field_kanban_step_ref. How can i do this?
It looks like you have set up
It looks like you have set up your ajax properly, which will let the user select a value from the options you give. The next thing you need to do is to attach a submit function to be called after the form is submitted. You can add your submit function to the $form element's #submit array, and it will be called after any existing submit callbacks:
still problem exist for me
before submit everything works fine and my second dropdownlist failed with right values but..
after submit i still see this error massage "An illegal choice has been detected. Please contact the site administrator." and my second dropdownlist going to be empty again. could anyone help..!
Paste the code
Always paste the code for reference. Always.
Thank You,
Shashwat Purav
Try below
Use Ajax Chain Select Module to get the problem solved
Ajax Chain Select Module addresses problems specified. you can use it for free from:
https://www.drupal.org/project/ajax_chain_select
Does your module work with
Does your module work with Webform? Thanks