managed_file upload clearing $form_state I am having an annoying problem whereby $form_state['values']['region'] and $form_state['values']['country'] are being cleared when I upload an image via the managed_file field. This is causing a "...tid cannot be null" error upon DB insertion (of course because the values are now null after managed_file upload). If I disable the managed_file field everything works as it should but of course I need the upload!

I am relatively new to Drupal and thus not quite understanding why this is acting like it is. Can anyone help me out as this is driving me nuts!

My form

function myforms_company_add_form($form, &amp;$form_state, $node) {

  $form['#prefix'] = '&lt;div id="wrapper"&gt;';
  $form['#suffix'] = '&lt;/div&gt;';

  if(!isset($form_state['values']['region']) &amp;&amp; !isset($form_state['values']['country'])) {
      $selectedRegion = array_shift(taxonomy_get_parents($node-&gt;field_company_region['und'][0]['tid']))-&gt;tid;    
      $selectedCountry = $node-&gt;field_company_region['und'][0]['tid'];
    }
    else {
        $selectedRegion = $form_state['values']['region'];    
        $selectedCountry = $form_state['values']['country'];
    }

$form['region'] = array(
    '#title' =&gt; t('Region'),
    '#type' =&gt; 'select',
    '#required' =&gt; TRUE,
    '#options' =&gt; _get_region_list('region'),
    '#prefix' =&gt; '&lt;div id="contact-selector"&gt;&lt;div id="region-wrapper"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
    '#default_value' =&gt; $selectedRegion,
    '#ajax' =&gt; array(
      'callback' =&gt; 'region_ajax_callback',
      'wrapper' =&gt; 'wrapper',
     ),
  ); 

  $form['country']['wrapper'] = array(
    '#type' =&gt; 'markup',
    '#prefix' =&gt; '&lt;div id="country-wrapper"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;&lt;/div&gt;',
  );

  if (isset($selectedRegion)) {
    $form['country'] = array(
        '#title' =&gt; t('Country'),
        '#type' =&gt; 'select',
        '#required' =&gt; TRUE,
        '#options' =&gt; _get_country_list($selectedRegion),
        '#default_value' =&gt; $selectedCountry,
        '#ajax' =&gt; array(
            'callback' =&gt; 'region_ajax_callback',
            'wrapper' =&gt; 'wrapper',
        ),
      );
   }

  $form['company_logo'] = array(
    '#title' =&gt; t('Logo'),
    '#type' =&gt; 'managed_file',
    '#upload_location' =&gt; 'public://my-files/',
    '#required' =&gt; FALSE,
    '#default_value' =&gt; $node-&gt;field_company_logo['und'][0]['fid'],
  );

  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; 'Submit',
    '#prefix' =&gt; '&lt;hr&gt;',
  );

  return $form;

}


Then my callback

function region_ajax_callback($form, &amp;$form_state) { 
  return $form;
}
I recently encountered the same problem. I found that data is erased in function - drupal_validate_form(). I have not fully understood why this is happening, therefore, just went bypassed the problem. I have rewritten my code so that the code section that erased the data in drupal_validate_form() is not executed.

My code:

  $managed_file_processing_fn = array_merge(
    element_info_property('managed_file', '#process', array()),
    array('ccw_managed_file_process')
  );

  $form['customization']['main']['request_design']['upload_design'] = array(
    '#type' =&gt; 'managed_file',
    '#progress_indicator' =&gt; 'bar',
    '#upload_location' =&gt; 'public://',
    '#upload_validators' =&gt; array(
      'file_validate_extensions' =&gt; array('jpg jpeg gif png zip rar tar'),
    ),
    '#states' =&gt; array(
      'visible' =&gt; array(
        ':input[name="type_of_upload"]' =&gt; array(
          array('value' =&gt; strval(CCW_GET_HELP)),
        ),
      ),
    ),
    '#process' =&gt; $managed_file_processing_fn,
    '#default_value' =&gt; $upload_design,
  );

/**
 * Process managed_file.
 */
function ccw_managed_file_process($element, &amp;$form_state, $form) {
  // When AJAX is execute we ignore form validation.
  $element['upload_button']['#limit_validation_errors'] = FALSE;
  $element['remove_button']['#limit_validation_errors'] = FALSE;

  return $element;
}


Topic with the same problem on drupal.org:
https://www.drupal.org/node/2311199

Also, I noticed that in Drupal 8 the behavior is the same.