How to debug missing default value of managed file? I have a form with images. It also contains title and alt for 3 languages, so when I select them in one query, I get the same managed file fid 3 times. But that's not the problem.

Relevant part of code is: 

kpr($image_has_language);
$first_language = reset($image_has_language);
$form['image'][$i] = array (
   '#type' =&gt; 'fieldset',
   '#collapsible' =&gt; FALSE,
   'ImageFile' =&gt; array(
     '#type' =&gt; 'managed_file',
     '#default_value' =&gt; (integer)$first_language['ImageFile'],
     '#title' =&gt; t("Image"),
     '#title_display' =&gt; 'invisible',
     // '#description' =&gt; t('Allowed extensions: gif png jpg jpeg.'),
     '#upload_validators' =&gt; array(
       'file_validate_extensions' =&gt; array('gif png jpg jpeg'),
       'file_validate_size' =&gt; array(variable_get('something_image_size', 5 * 1024 * 1024)),
     ),
     '#upload_location' =&gt; 'public://something/image',
     '#theme' =&gt; 'preview_upload',
   ),
   'id_image' =&gt; array(
     '#type' =&gt; 'value',
     '#value' =&gt; $clone?'new':$first_language['idImage'],
   )
 );
 kpr($form['image'][$i]);


Below results of that kprs:



As you can see, all is perfectly well. File is on disk, file preview work, in database it is marked as permanent and as used.

When I call var_dump on $form_state['values']['image'][0] in validation function, without changing file on the form, I get:


  array(3) {
    ["ImageFile"]=>
    int(0)
    ["id_image"]=>
    string(3) "599"
  }


Values in $_POST are OK:


  ["image"]=>
  array(4) {
    [0]=>
    array(2) {
      ["ImageFile"]=>
      array(1) {
        ["fid"]=>
        string(4) "3696"
      }


If I change file, it works as expected:


  array(3) {
    ["ImageFile"]=>
    string(4) "3698"
    ["id_image"]=>
    string(3) "599"
  }


and in $_POST:


  ["image"]=>
  array(4) {
    [0]=>
    array(2) {
      ["ImageFile"]=>
      array(1) {
        ["fid"]=>
        string(4) "3698"
      }


How can I identify where my value gets removed? I also have another image on that form, single one (so without foreach), and it works OK.
First of all can you try to remove the fieldset to a different $form[] array key? Is it really necessary to make it nested like this?

I think if you want to know if the file is added or removed then you can check in the following way.

$total_image_count = $form_state['storage']['image_elements']; // The number of image uploaded elements; just as example.
for($i = 0; $i &lt; $total_image_count; $i++) {
  if ($form_state['values']['image'][$i] != 0) {
    // File is not deleted.
  } elseif ($form_state['values']['image'][$i] == 0) {
    //File is deleted.
  }
}


However, check with this and let me know.