1

I have created a node type of Advertisement which holds all the information for adding new advertations to the website, including categories.

there are 3 levels of categories and i need to make a seperate box for them. The content type which i created through the GUI of drupal already has a term reference select box for category, sub category, and level 3 category.. So i made a module and implemented hook_form_alter and i added the #ajax attributefunction

<?php
adverts_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'advertisement_node_form') {
      $topLevelTermArray = array('_none' => ' - Select a value -');
      $topLevelTerms = array(5275, 5245, 5299, 5341, 5702, 5681, 5826, 5555, 6039, 5592, 5776, 5994, 6027, 5451, 5852, 6016, 5496, 5475, 6049);
      foreach($topLevelTerms AS $value) {
        $term = taxonomy_term_load($value);
        $topLevelTermArray[$term->tid] = $term->name;
      }
      $form['field_specific_category']['und']['#prefix'] = '<div id="field_specific_category_replace">';
      $form['field_specific_category']['und']['#suffix'] = '</div>';
      $form['field_sub_category']['und']['#ajax'] = array(
        'callback' => 'ajax_sub_category_callback',
        'wrapper' => 'field_specific_category_replace',
      );
      $form['field_sub_category']['und']['#prefix'] = '<div id="field_sub_category_replace">';
      $form['field_sub_category']['und']['#suffix'] = '</div>';
      $form['field_sub_category']['und']['#options'] = array('_none' => t('- Please select a category -'));
      $form['field_specific_category']['und']['#options'] = array('_none' => t('- Please select a sub category -'));
      $form['field_category']['und']['#options'] = $topLevelTermArray;
      $form['field_category']['und']['#ajax'] = array(
        'callback' => 'ajax_category_callback',
        'wrapper' => 'field_sub_category_replace',
      );
  }
}
?>

And 2 callback functions

function ajax_category_callback($form, $form_state) {
  $subCats = array();
  $tree = taxonomy_get_tree(5, $form_state['values']['field_category']['und'][0]['tid']);
  foreach($tree AS $term) {
    $subCats[$term->tid] = $term->name;
  }
  $element = $form['field_sub_category']['und'];
  $mainCatTerm = taxonomy_term_load($form_state['values']['field_category']['und'][0]['tid']);
  $element['#title'] = t('Sub categories for @cat.', array('@cat' => $mainCatTerm->name));
  $element['#options'] = $subCats;

  return $element;
}

function ajax_sub_category_callback($form, $form_state) {
  $subCats = array();
  $tree = taxonomy_get_tree(5, $form_state['values']['field_sub_category']['und'][0]['tid']);
  //var_dump($form_state['values']['field_sub_category']['und'][0]['tid']);
  foreach($tree AS $term) {
    $subCats[$term->tid] = $term->name;
  }
  $element = $form['field_specific_category']['und'];
  if(empty($tree)) {
    $element['#options'] = array('_none' => '- No specific category -');
  } else {
    $mainCatTerm = taxonomy_term_load($form_state['values']['field_sub_category']['und'][0]['tid']);
    $element['#title'] = t('Sub categories for @cat.', array('@cat' => $mainCatTerm->name));
    $element['#options'] = $subCats;
  }
  return $element;
}

But when i go to node/advertisement/edit and i select a category and sub category is show's:

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

How can i get that error too dissapear?

3
  • remove ['und'] index.
    – Gaydabura
    Commented Jan 11, 2013 at 15:43
  • Could you be more specific Commented Jan 11, 2013 at 19:14
  • see. in D. every form element and level must be described. so you have $form['field_specific_category']['und'] where field_specific_category - is parent leven and it is not specified. you've specified 'und' which is a part of another element. just remove one level
    – Gaydabura
    Commented Jan 12, 2013 at 13:08

2 Answers 2

3

The value you are submitting was not present during the page or form loading time.

If you populate some values using ajax which was not present in initial page loads and try to submit the form, then Drupal will not allow you to submit the form. It will always shows "An illegal choice has been detected" message.

1
  • Is there no way around this at all? I have the same issue. I'm surprised that this hasn't been patched considering it is a pretty standard request with ajax these days.
    – Delmontee
    Commented Sep 18, 2014 at 15:38
-1

I'm afraid there is only one solution and that's to comment out line 1388 & 1389 of form.inc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.