0

I want to add a couple of fields to a content type form using AJAX and, upon validation, run a custom _submit callback to do something with those fields.

My approach:

1. Alter the form adding a select that triggers the ajax, evaluates the value of $form_state['values'][and produces two textfield fields:

function hook_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
  $form['select'] = array(
    '#type' => 'select',
    '#title' => t('Options'),
    '#options' => array(0 => 'Do nothing', 1 => 'AJAX'),
    '#ajax' => array(
      'callback' => 'my_ajax_callback',
      'wrapper' => 'ajax-info',
      'effect' => 'fade',
    ),
  );

  $form['info'] = array(
    '#type' => 'fieldset',
    '#title' => t('Information'),
    '#prefix' => '<div id="ajax-info">',
    '#suffix' => '</div>',
  );

  if ($form_state['values']['some_value'] == 'some_other_value') {
    $form['info']['input_1'] = array(
      '#type' => 'textfield',
      '#title' => t('Input 1'),
    );

    $form['info']['input_2'] = array(
      '#type' => 'textfield',
      '#title' => t('Input 2'),
    );
  }

  // some validation
  $form['#validate'][] = 'my_module_FORM_ID_form_validate';

  // append it to the bottom because it's important that it is executed after the default submit callback
  $form['actions']['submit']['#submit'][] ='my_module_FORM_ID_form_submit';
}

2. Return the relevant part of code in the ajax callback:

function my_ajax_callback($form, $form_state){
  return $form['info'];
}

My problem comes when implementing my_module_FORM_ID_form_submit, as it seems that $form_state['input_1'] and $form_state['input_2'] are not set.

Sometimes, after clearing cash, input_1 and input_2 may be read in the callback and everything may work. But eventually, errors occur after a few attempts.

I also tried setting $form_state['rebuild'] = TRUE in the _validation without success.

Can it be a problem with the way I'm adding the submit callback? I cannot add it to $form['#submit][] because the default form callback is not defined there and it's important that my custom callback gets executed last.

EDIT I think that what is causing the problem is that one of the fields that I am adding is actually an addressfield widget, that I attach to my form by calling addressfield_generate().

  • I think that what is causing the problem is that one of the fields that I am adding is actually an addressfield widget, that I attach to my form by calling addressfield_generate(). – Cándido Otero Mar 27 at 11:47
  • Could be because you're missing & in your callback. Try function my_ajax_callback(&$form, &$form_state){ – No Sssweat Mar 27 at 15:23
  • Unfortunately that makes no difference. Actually, it's my understanding that no changes on $form of $form_state should be done in the ajax_callback. I narrowed a bit more the sources of my problem, and the issue is that all fields that have an ajax attached (managed_file or addressfield) cause the same kind of issues: drupal.org/forum/support/module-development-and-code-questions/…. I'm taking a look at this module (shoutout to @debaryadas) drupal.org/project/drupal/issues/2210295#comment-8665713 where this seems to work. – Cándido Otero Mar 27 at 15:46

Your Answer

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

Browse other questions tagged or ask your own question.