this error has been driving me completely mad for a few days. I at first thought it was specific to the media button and so switched to file managed and had the same issue.

Anyway, I have a form that generates, using AJAX, a set of form elements depending on the value of a radio button. The first option generates a file upload element and others basic text fields

The issue is that if I click upload, or with the media element 'attach', the ajax runs on that button and then the form element completely disappears, it's not in the DOM at all. I get no error messages. If I attempt to submit the form, the elements (even the ones that are still on the page) don't have any values in form_state.

I'm at a total loss with this.

Here is my form:

$node = node_load($nid);
// Getting the field_image widget - seemingly the only way to do this is to get every form element from node edit and then remove them. There is a ctools way to get individual elements but I couldn't get it to work
field_attach_form('node', $node, $form, $form_state, LANGUAGE_NONE);
foreach ($form as $key => $values) {
  if (!in_array($key, array('artist', 'track', 'label', 'links', 'field_image')) && substr($key, 0, 1) != '#') {
    $form[$key]['#access'] =  FALSE;
  }
}
// Save the field_image array
$field_image = $form['field_image'];
// Remove the original.
unset($form['field_image']);
The ajax radio button

$form['approve'] = array(
  '#type' => 'radios',
  '#title' => 'Approve, Reject or Reprocess?',
  '#options' => array(
    1 => 'Approve',
    2 => 'Reject',
    3 => 'Reprocess',
    4 => 'Postpone',
  ),
  '#required' => TRUE,
  '#ajax' => array(
    'callback' => 'post_approval_ajax_callback',
    'wrapper' => 'approval-div',
    'method' => 'replace',
    'effect' => 'fade',
  ),
);

$form['approval_fieldset'] = array(
  '#prefix' => '<div id="approval-div">',
  '#suffix' => '</div>',
  '#type' => 'fieldset',
);

And the first part of the switch that retrieves the image form

switch($form_state['values']['approve']) {
  case 1:
    $form['approval_fieldset']['information'] = array(
      '#markup' => '<p>Approve this track? Please complete the following information about it, as much as possible.</p>',
    );
    $form['approval_fieldset']['artist'] = array(
      '#type' => 'textfield',
      '#title' => t('Artist'),
      '#required' => TRUE,
      '#default_value' => trim($details[0]),
    );

    $form['approval_fieldset']['track'] = array(
      '#type' => 'textfield',
      '#title' => t('Track'),
      '#required' => TRUE,
      '#default_value' => trim($details[1]),
    );

    $form['approval_fieldset']['label'] = array(
      '#type' => 'textfield',
      '#title' => t('Label'),
    );

    $form['approval_fieldset']['links'] = array(
      '#type' => 'text_format',
      '#title' => t('Information'),
      '#format' => 'full_html'
    );

    $form['approval_fieldset']['field_image'] = $field_image;

    break;

My callback


function post_approval_ajax_callback($form, $form_state) {
   return $form['approval_fieldset'];
}

Now it should be noted that this works absolutely perfectly without the AJAX generating the initial form. So there is something in either my ajax callback, or the file field button ajax callback that is incorrect.

Please help!

Comments

jaypan’s picture

It's hard to give proper information without seeing the functions that this code resides in. Drupal is all about context, the function names determine how things work. It's also hard to tell how the first and second blocks of code are related within your code. Please copy your actual code, and just remove anything that doesn't have relevance.


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
michaelmallett’s picture

Sure

function post_approval_get_posts($form, &$form_state) {
    $form['song'] = array(
      '#markup' =>  '<ul class="playlist"><li><a href="' . $node->field_mp3_link['und'][0]['value'] . '">' . $node->title . '</a></li></ul>',
    );
    $form['search'] = array(
      '#markup' => '<p><a href="http://www.discogs.com/search/?q=' . $node->title . '" target="_blank">Discogs Search for: ' . $node->title . '</a></p>',
    );

    $form['node'] = array(
      '#type' => 'value',
      '#value' => $node,
    );

    $form['approve'] = array(
      '#type' => 'radios',
      '#title' => 'Approve, Reject or Reprocess?',
      '#options' => array(
        1 => 'Approve',
        2 => 'Reject',
        3 => 'Reprocess',
        4 => 'Postpone',
      ),
      '#required' => TRUE,
      '#ajax' => array(
        'callback' => 'post_approval_ajax_callback',
        'wrapper' => 'approval-div',
        'method' => 'replace',
        'effect' => 'fade',
      ),
    );

    $form['approval_fieldset'] = array(
      '#prefix' => '<div id="approval-div">',
      '#suffix' => '</div>',
      '#type' => 'fieldset',
    );

    switch($form_state['values']['approve']) {
      case 1:
        $form['approval_fieldset']['information'] = array(
          '#markup' => '<p>Approve this track? Please complete the following information about it, as much as possible.</p>',
        );

        $form['approval_fieldset']['artist'] = array(
          '#type' => 'textfield',
          '#title' => t('Artist'),
          '#required' => TRUE,
          '#default_value' => trim($details[0]),
        );

        $form['approval_fieldset']['track'] = array(
          '#type' => 'textfield',
          '#title' => t('Track'),
          '#required' => TRUE,
          '#default_value' => trim($details[1]),
        );

        $form['approval_fieldset']['label'] = array(
          '#type' => 'textfield',
          '#title' => t('Label'),
        );

        $form['approval_fieldset']['links'] = array(
          '#type' => 'text_format',
          '#title' => t('Information'),
          '#format' => 'full_html'
        );

        $form['approval_fieldset']['image'] = array(
          '#title' => t('Artwork'),
          '#type' => 'managed_file',
          '#required' => TRUE,
        );
        break;
      case 2:
        $form['approval_fieldset']['information'] = array(
          '#markup' => '<p>Reject this track? Use this if the clip is not music, or any other reason why we wouldn\'t want it (it\'s rubbish).</p>',
        );

        $form['approval_fieldset']['reason'] = array(
          '#type' => 'textfield',
          '#title' => t('Reason (optional)'),
        );
        break;
      case 3:
        $form['approval_fieldset']['information'] = array(
          '#markup' => '<p>Reprocess this track? Please provide a better link if the current one is not full length, or poor quality.</p>',
        );

        $form['approval_fieldset']['link'] = array(
          '#type' => 'textfield',
          '#title' => t('Link'),
          '#description' => 'Replace the current link with another, better, Soundcloud or Youtube link. Full url only',
          '#required' => TRUE,
        );
        break;
      case 4:
        $form['approval_fieldset']['information'] = array(
          '#markup' => '<p>Postpone this track? Use this option if the link is poor and you cannot find a better quality one (but believe one can be found in future).</p>',
        );
        break;
    }
    if ($form_state['values']['approve']) {
      $form['approval_fieldset']['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Save'
      );
    }

    $form['ajaxwrapper_end'] = array(
      '#markup' => '</div>',
    );

  }
  else {
    $form['markup'] = array(
      '#markup' =>  '<p>There are no posts to process</p>',
    );
  }

  return $form;
}


function post_approval_ajax_callback($form, $form_state) {
  return $form['approval_fieldset'];
}

I've replaced the image field with just a normal managed file field to avoid confusion. When clicking the upload button the throbber appears and then the whole form field disappears. Clicking submit doesn't get me anywhere, dpm'ing the form_state in the validate hook shows that there are no values for any of the ajax generated fields, or even just array keys.

michaelmallett’s picture

I tried adding the same field to the examples ajax form to make sure it wasn't my form, and got

Notice: Undefined index: image in file_ajax_upload() (line 271 of /Users/Mike/Sites/Soul/modules/file/file.module).
Notice: Undefined index: #suffix in file_ajax_upload() (line 280 of /Users/Mike/Sites/Soul/modules/file/file.module).

I updated my core, and all my modules, then applied the patch in here:
https://drupal.org/node/1336212

And tried to follow what was said here:
https://drupal.org/node/1526300#comment-5899988

But I don't think it applies, as I am not retrieving a node form, or trying to call user_profile_form(), although I might be misunderstanding?

So in the generate checkboxes form in the examples module I added the managed_file field

function ajax_example_autocheckboxes($form, &$form_state) {
  // Since the form builder is called after every AJAX request, we rebuild
  // the form based on $form_state.
  $num_checkboxes = !empty($form_state['values']['howmany_select']) ? $form_state['values']['howmany_select'] : 1;

  $form['howmany_select'] = array(
    '#title' => t('How many checkboxes do you want?'),
    '#type' => 'select',
    '#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4),
    '#default_value' => $num_checkboxes,
    '#ajax' => array(
      'callback' => 'ajax_example_autocheckboxes_callback',
      'wrapper' => 'checkboxes-div',
      // 'method' defaults to replaceWith, but valid values also include
      // append, prepend, before and after.
      // 'method' => 'replaceWith',
      // 'effect' defaults to none. Other valid values are 'fade' and 'slide'.
      // See ajax_example_autotextfields for an example of 'fade'.
      'effect' => 'slide',
      // 'speed' defaults to 'slow'. You can also use 'fast'
      // or a number of milliseconds for the animation to last.
      // 'speed' => 'slow',
      // Don't show any throbber...
      'progress' => array('type' => 'none'),
    ),
  );

  $form['checkboxes_fieldset'] = array(
    '#title' => t("Generated Checkboxes"),
    // The prefix/suffix provide the div that we're replacing, named by
    // #ajax['wrapper'] above.
    '#prefix' => '<div id="checkboxes-div">',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
    '#description' => t('This is where we get automatically generated checkboxes'),
  );

  for ($i = 1; $i <= $num_checkboxes; $i++) {
    $form['checkboxes_fieldset']["checkbox$i"] = array(
      '#type' => 'checkbox',
      '#title' => "Checkbox $i",
    );
  }
  // I added this here
  if ($num_checkboxes > 1) {
    $form['checkboxes_fieldset']['image'] = array(
      '#title' => t('Artwork'),
      '#type' => 'managed_file',
      '#required' => TRUE,
    );
  }

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

And upon clicking upload, the form element disappears and gives me those 2 errors. Bizarrely I didn't get those errors in my own form.

michaelmallett’s picture

Looks like the theme was stopping my error messages, I just tried it again and got these messages:

Notice: Undefined index: approve in post_approval_get_posts()
Notice: Undefined index: approve in post_approval_get_posts()
Notice: Undefined index: image in file_ajax_upload()
Notice: Undefined index: #suffix in file_ajax_upload()

I can't find anything that helps me with those errors messages. It seems like ajax form elements don't work in a ajax form...but that's surely a huge bug and would have been reported by now.

I've created a big report in the meantime, this is definitely not expected behaviour.

https://drupal.org/node/2210295

debaryadas’s picture

Hi michaelmallett,

A response of the issue has been posted at below link.

https://drupal.org/node/2210295.

check it and reply whether it works for you or not.

With regards
Debarya Das

With Regards
Debarya Das

saayanta’s picture

I have a custom form made in drupal 7.I hv 1 text box which asks user to give an input.Then there is a submit button and another text box.Now I want that I will click on that submit button and the desired output will appear in that text box.The calculation for that output is done but it is not showing in that text box.Can anyone please help me solve the issue?

debaryadas’s picture

Hi Saatyanta,

Can you share your code so that I can help you.

With Regards
Debarya Das

With Regards
Debarya Das