7

I'm having trouble with something incredibly simple: When I select an element in a multiselect-form element, I want a textfield to become enabled. The problem I'm having is that Drupal never manages to get in the callback-function I specified.

Here I create my multiselect-element:

$form['foocat'] = array(
  '#type' => 'fieldset', 
  '#title' => t('Manage subcategories'), 
  '#prefix' => '<div class="fieldset">',
  '#suffix' => '</div>',
);
$form['foocat']['subcategories'] = array(
  '#type' => 'select',
  '#default_value' => '',
  '#title' => $foocategory,
  '#options' => $options,
  '#ajax' => array(
    'callback' => 'edit_displayname_callback', 
    'wrapper' => 'displayname',
  ),
  '#attributes' => array('id' => 'foocats',),
  '#prefix' => '<div class="formselect" id="selectmapcat">',
  '#suffix' => '</div>',
  '#size' => 9,
);

A little bit further in the code I create my textfield:

$form['foocat']['subcat_displayname'] = array(
  '#type' => 'textfield',
  '#title' => t('Name'),
  '#default_value' => $entity->foocategory,
  '#size' => 25,
  '#maxlength' => 31,
  '#prefix' => '<div class="floater" id="displayname">',
  '#suffix' => '</div>',
  '#disabled' => TRUE,
);

if(!empty($form_state['subcategories'])) {
  $form['foocat']['subcat_displayname']['#disabled'] = FALSE; 
  //Code for setting the textfield, dependent on what's been selected in the multiselect
}

And this is my callback function:

function edit_displayname_callback($form, &$form_state) {
  watchdog("Test", "Testing callback..", array(), WATCHDOG_ALERT);
  return $form['foocat']['subcat_displayname'];
}

The callback is never even called, and I have no idea why. I use AJAX in another part of the same form, and there it does work, so I'm guessing it's not a form problem.

Can anyone spot the problem?

1 Answer 1

2

I guess you are changing the id with '#attributes' => array('id' => 'foocats',), And now the callback is not executed because the id is changed.

if you change your code it will work ( tested this in my own install )

$form['foocat']['subcategories'] = array(
  '#type' => 'select',
  '#default_value' => '',
  '#title' => $foocategory,
  '#options' => $options,
  '#ajax' => array(
    'callback' => 'edit_displayname_callback', 
    'wrapper' => 'displayname',
  ),
  //'#attributes' => array('id' => 'foocats',),
  '#prefix' => '<div class="formselect MY_SPECIAL_JQUERY_SELECTOR" id="selectmapcat">',
  '#suffix' => '</div>',
  '#size' => 9,
);
3
  • The problem is that I need the ID on $form['foocat']['subcategories']. I use it to select it in jQuery
    – RienK
    Commented Apr 26, 2012 at 15:08
  • updated my answer, you can add a second class so you can use that for your jQuery: $('div.MY_SPECIAL_JQUERY_SELECTOR select')
    – FLY
    Commented Apr 26, 2012 at 15:13
  • Nice, I'll check it out as soon as I'm back at work again!
    – RienK
    Commented Apr 29, 2012 at 11:05

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.