7: Form API: managed_file: AJAX upload error "Undefined index ... in file_ajax_upload()" I have a problem with "AJAX: add more file fields". The Problem: i create new managed_file field and add AJAX function "add more file fields". When i try to upload a file with first static file field, it works fine. But when use function "add more fields" and try to upload a file with AJAX created field, get error "Notice: Undefined index: ... in file_ajax_upload() (Line 262 in .../modules/file/file.module)."

All Answers for Similar Questions here don't solve this Problem.

The code:

&lt;?php
/**
* implimentation of hook_form_alter()
*/
function mymodule_form_alter(&amp;$form, &amp;$form_state, $form_id) {

}

/**
* implimentation of hook_form_FORM_ID_alter()
*/

function mymodule_form_block_admin_configure_alter(&amp;$form, &amp;$form_state, $form_id) {
 // 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' =&gt; t('How many checkboxes do you want?'),
    '#type' =&gt; 'select',
    '#options' =&gt; array(
      1 =&gt; 1,
      2 =&gt; 2,
      3 =&gt; 3,
      4 =&gt; 4,
    ),
    '#default_value' =&gt; $num_checkboxes,
    '#ajax' =&gt; array(
      'callback' =&gt; 'ajax_example_autocheckboxes_callback',
      'wrapper' =&gt; 'checkboxes-div',
      // See ajax_example_autotextfields for an example of 'fade'.
      'effect' =&gt; 'fade',
      // Don't show any throbber...
      'progress' =&gt; array('type' =&gt; 'none'),
    ),
  );

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

  for ($i = 1; $i &lt;= $num_checkboxes; $i++) {
   $form['checkboxes_fieldset']["slideshow_item_$i"] = array(
    '#title'    =&gt; t('Image')." $i:",
    '#type'     =&gt; 'managed_file',
    '#required' =&gt; FALSE,
    '#upload_location' =&gt; file_default_scheme() . '://mymodule_images/',
    '#upload_validators' =&gt; array(
      'file_validate_extensions' =&gt; array('gif png jpg jpeg'),
    ),
    '#tree' =&gt; TRUE,
    '#description' =&gt; t("Upload slideshow image. Allowed extensions: gif, png, jpg, jpeg."),
   );
  }
  return $form;
}



function ajax_example_autocheckboxes_callback($form, $form_state) {
  $form_state['rebuild'] = TRUE;
  return $form['checkboxes_fieldset'];
}

?&gt;


I think the Problem is in $form_state. How i can refresh / rebuild $form_state? In ajax_example_autocheckboxes_callback i set $form_state['rebuild'] = TRUE; but without effect :(

Code Test Video:

http://www.youtube.com/watch?v=PtRiQorCTA4

I looking 3 days for a solution :-/ 

Helpful articles:

How to add more Fields using Form Api

https://drupal.org/node/752056

https://drupal.org/node/331941

How to wrap managed_file Form API element with custom HTML using #theme_wrappers?
You need to pass the parameters by reference if you want changes to persist outside the function scope:

function ajax_example_autocheckboxes_callback(&amp;$form, &amp;$form_state) {