Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

As given in the code below, I tried enabling select list $form['answer']['answer1'] through AJAX callback, textbox is getting populated with value, where as the dropdownlist is not getting enabled. Any help?

function mymodule_search_block_form($form, &$form_state) {
  $options = array();
  $options['key1'] = t('value1');
  $options['key2'] = t('value2');
  $options['key3'] = t('value3');
  $options['key4'] = t('value4');

  $form['profiles'] = array(
    '#type' => 'select',
    '#title' => 'Profiles',
    '#options' => $options,
    '#ajax' => array(
        'callback' => 'ajax_select_callback',
        'wrapper' => 'profile-wrapper',
        'event' => 'change',
    ),
  );

  $form['answer']['profile_name'] = array(
    '#prefix' => '<div id="profile-wrapper">',
    '#type' => 'textfield',
    '#title' => 'Profile Name1',
  );
  $form['answer']['profile_name2'] = array(
    '#type' => 'textfield',
    '#title' => 'Profile Name2',
  );
  $form['answer']['answer1'] = array(
    '#type' => 'select',
    '#disabled' => 1,
    '#title' => 'Profiles',
    '#options' => $options,
    '#suffix' => '</div></br>',
  );
  return $form;
}

function ajax_select_callback($form, $form_state) {
  $form['answer']['profile_name']['#value'] = 'Profile 1';
  $form['answer']['profile_name2']['#value'] = 'Profile 2';
  $form['answer']['answer1']['#disabled'] = 0;
  return $form['answer'];
}
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Interesting question. The way I see it there are actually two parts to this. This first is, of course, your actual question and that can be easily solved:

// Your code in ajax_select_callback().
unset($form['answer']['answer1']['#attributes']['disabled']);
$form['answer']['answer1']['#disabled'] = 0;
// More of your code in ajax_select_callback().

When you set the #disabled property of a form element to TRUE, what Drupal does is simply add the disabled attribute to the element and allows the renderer (i.e. drupal_render()) to do its job. In your AJAX callback, the $form array passed is that of the rendered form. This means that the your dropdown element already has the disabled attribute on it. Therefore, when it is rendered again, it still shows up disabled. So, to prevent this, we simply remove the attribute.

However, you'd notice that when you submit your form after this, the value of the disabled dropdown (in $form_state['values']) may not be the value you chose after "enabling" it. This is because your form is not being re-built on the server which means that Drupal still thinks your dropdown is disabled and will refuse to process your input, simply ignoring it and using the default. If you are able to reproduce this, comment on this answer and I'll update this post with a solution for that problem.

share|improve this answer
    
Thanks it worked. –  Kiran Kumar Dec 26 '12 at 6:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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