1

I have a little problem:

/**
 * Form callback
 */
function ajax_disable_test_form($form, &$form_state) {
  $form['select'] = array(
    '#type' => 'select',
    '#required' => FALSE,
    '#title' => t('Change me!'),
    '#empty_option' => t('- Select -'),
    '#options' => drupal_map_assoc(range(1, 3)),
    '#prefix' => '<div id="select_box_wrapper">',
    '#suffix' => '</div>',
  );
  $form['change'] = array(
    '#type' => 'submit',
    '#value' => t('Change!'),
    '#ajax' => array(
      'callback' => 'ajax_disable_test_form_submit_ajax',
      'wrapper' => 'select_box_wrapper',
    ),
  );
  return $form;
}

/**
 * Ajax callback
 */
function ajax_disable_test_form_submit_ajax($form, $form_state) {
  $form['select']['#title'] = t('Changed!'); // It's work!
  $form['select']['#disabled'] = TRUE; // It's not work!
  return $form['select'];
}

Link: https://gist.github.com/nevergone/23f0a50bee01acc34ed2

If clicked the button, select box title changed, but select box not disabled. Why? Version: Drupal 7.41

1

try:

$form['select']['#attributes']['disabled'] = 'disabled';

Ok, a detailed explanation can be found on this article https://www.drupal.org/node/357328

But pretty much resumes to:

we cannot simple set the $element['#disabled'] attribute of the element. The reason is this attribute is transformed into $element['#attributes']['disabled'] by _form_builder_handle_input_element executes()...

  • Please try explaining why the code works. A code-only answer doesn't explain why it works or what the asker did wrong in the first place. – Shawn Conn Oct 25 '15 at 1:39
  • ok, some background was added to my answer. – Blanca Esqueda Oct 25 '15 at 2:09
0

You will need to rebuild your form. Have a look at the Ajax Framework docs for further information.

<?php

/**
 * Form callback
 *
 * Implements hook_form().
 */
function ajax_disable_test_form($form, &$form_state) {
  $form['select'] = array(
    '#type' => 'select',
    '#required' => FALSE,
    '#title' => t('Change me!'),
    '#empty_option' => t('- Select -'),
    '#options' => drupal_map_assoc(range(1, 3)),
    '#prefix' => '<div id="select_box_wrapper">',
    '#suffix' => '</div>',
  );
  if (!empty($form_state['values']['change'])) {
    $form['select']['#disabled'] = TRUE;
    $form['select']['#title'] = t('Changed');
  }

  $form['change'] = array(
    '#type' => 'submit',
    '#value' => t('Change!'),
    '#submit' => array('ajax_disable_test_change_submit'),
    '#ajax' => array(
      'callback' => 'ajax_disable_test_ajax_callback',
      'wrapper' => 'select_box_wrapper',
    ),
  );
  return $form;
}

/**
 * Submit handler to disable the select input, and then rebuild the form.
 * If the button was click with JS enabled, then ajax_form_callback() calls
 * ajax_disable_test_submit_ajax() to return just the changed part of the form.
 */
function ajax_disable_test_change_submit($form, &$form_state) {
  if ($form_state['values']['change']) {
    $form_state['select_is_disabled'] = true;
  }
  $form_state['rebuild'] = TRUE;
}

/**
 * Ajax callback
 *
 * @see ajax_disable_test_change_submit().
 */
function ajax_disable_test_ajax_callback($form, $form_state) {
  return $form['select'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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