title: FilePrg with collections on ZF2
body: <p>I need to submit a form which contains some fields along with a collection of max 3 images using fileprg plugin for saving uploaded files in case there has been some validation errors.</p>
  
  <p>I'd like not to switch to a different solution ( like splitting the form in subforms, or removing collection in favour of three different file fields ). The main problem I experience is that when the user submits an invalid file ( too big, or with invalid mimetype ) I'd like to report the error and allow to upload a different file.  What happens instead is that the collection disappears from the form.</p>
  
  <p>The same happens if the users submit no file at all, even if files are required.
  After testing with my own code, I decided to test on cgmartin code, and this happens anyway.</p>
  
  <p>I know this could be banned like a duplicate of <a href="https://stackoverflow.com/questions/16521888/zf2-fileprg-with-files-in-collection">this question</a> but I'm stuck in this problem by a couple of days. </p>
  
  <p>This is my "fork" of cgmartin code ( I actually added the fileprg in the controller, view and form have been left untouched).
  Thanks.
  A.</p>
  
  <pre><code>public function collectionAction()
  {
      $form = new Form\CollectionUpload('file-form');
      $prg = $this-&gt;fileprg($form);
  
  
      if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
          $this-&gt;getServiceLocator()-&gt;get('Zend/Log')-&gt;debug("Redirect to Get!");
          return $prg; // Return PRG redirect response
      } elseif (is_array($prg)) {
  
  
              if ($form-&gt;isValid()) {
                  //
                  // ...Save the form...
                  //
                  return $this-&gt;redirectToSuccessPage($form-&gt;getData());
              }
  
      }
  
      return array('form' =&gt; $form);
  }
  </code></pre>
  
  <p>The view</p>
  
  <pre><code>&lt;div&gt;
   &lt;a href="&lt;?php echo $this-&gt;url('fileupload')?&gt;"&gt;&amp;laquo; Back to Examples Listing&lt;/a&gt;
  &lt;/div&gt;
  
  &lt;h2&gt;&lt;?php echo ($this-&gt;title) ?: 'File Upload Examples' ?&gt;&lt;/h2&gt;
  
  &lt;?php
  // Init Form
  $form = $this-&gt;form;
  $form-&gt;setAttribute('class', 'form-horizontal');
  $form-&gt;prepare();
  
  // Configure Errors Helper
  $errorsHelper  = $this-&gt;plugin('formelementerrors');
  $errorsHelper
      -&gt;setMessageOpenFormat('&lt;div class="help-block"&gt;')
      -&gt;setMessageSeparatorString('&lt;/div&gt;&lt;div class="help-block"&gt;')
      -&gt;setMessageCloseString('&lt;/div&gt;');
  ?&gt;
  &lt;?php echo $this-&gt;form()-&gt;openTag($form); ?&gt;
  &lt;fieldset&gt;
      &lt;legend&gt;&lt;?php echo ($this-&gt;legend) ?: 'Multi-File Upload with Collections' ?&gt;&lt;/legend&gt;
  
      &lt;?php
      $elem = $form-&gt;get('text');
      $elem-&gt;setLabelAttributes(array('class' =&gt; 'control-label'));
      $errors = $elem-&gt;getMessages();
      $errorClass = (!empty($errors)) ? ' error' : '';
      ?&gt;
      &lt;div class="control-group&lt;?php echo $errorClass ?&gt;"&gt;
          &lt;?php echo $this-&gt;formLabel($elem); ?&gt;
          &lt;div class="controls"&gt;
              &lt;?php echo $this-&gt;formText($elem); ?&gt;
              &lt;?php echo $errorsHelper($elem); ?&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;?php
      $collection = $form-&gt;get('file-collection');
      foreach ($collection as $elem) {
          $elem-&gt;setLabelAttributes(array('class' =&gt; 'control-label'));
          $errors = $elem-&gt;getMessages();
          $errorClass = (!empty($errors)) ? ' error' : '';
          ?&gt;
          &lt;div class="control-group&lt;?php echo $errorClass ?&gt;"&gt;
              &lt;?php echo $this-&gt;formLabel($elem); ?&gt;
              &lt;div class="controls"&gt;
                  &lt;?php echo $this-&gt;formFile($elem); ?&gt;
                  &lt;?php echo $errorsHelper($elem); ?&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;?php
      }
      ?&gt;
      &lt;?php if (!empty($this-&gt;tempFiles)) { ?&gt;
      &lt;div class="control-group"&gt;&lt;div class="controls"&gt;
          &lt;div class="help-block"&gt;
              Uploaded: &lt;ul&gt;
              &lt;?php foreach ($this-&gt;tempFiles as $tempFile) { ?&gt;
              &lt;li&gt;&lt;?php echo $this-&gt;escapeHtml($tempFile['name']) ?&gt;&lt;/li&gt;
              &lt;?php } ?&gt;
              &lt;/ul&gt;
          &lt;/div&gt;
      &lt;/div&gt;&lt;/div&gt;
      &lt;?php } ?&gt;
  
      &lt;div class="control-group"&gt;
          &lt;div class="controls"&gt;
              &lt;button class="btn btn-primary"&gt;Submit&lt;/button&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  &lt;/fieldset&gt;
  &lt;?php echo $this-&gt;form()-&gt;closeTag($form); ?&gt;
  </code></pre>
  
  <p>the form: </p>
  
  <pre><code>&lt;?php
  
  namespace ZF2FileUploadExamples\Form;
  
  use Zend\InputFilter;
  use Zend\Form\Form;
  use Zend\Form\Element;
  
  class CollectionUpload extends Form
  {
      public $numFileElements = 2;
  
      public function __construct($name = null, $options = array())
      {
          parent::__construct($name, $options);
          $this-&gt;addElements();
          $this-&gt;setInputFilter($this-&gt;createInputFilter());
      }
  
      public function addElements()
      {
          // File Input
          $file = new Element\File('file');
          $file-&gt;setLabel('Multi File');
  
          $fileCollection = new Element\Collection('file-collection');
          $fileCollection-&gt;setOptions(array(
               'count'          =&gt; $this-&gt;numFileElements,
               'allow_add'      =&gt; false,
               'allow_remove'   =&gt; false,
               'target_element' =&gt; $file,
          ));
          $this-&gt;add($fileCollection);
  
          // Text Input
          $text = new Element\Text('text');
          $text-&gt;setLabel('Text Entry');
          $this-&gt;add($text);
      }
  
      public function createInputFilter()
      {
          $inputFilter = new InputFilter\InputFilter();
  
          // File Collection
          $fileCollection = new InputFilter\InputFilter();
          for ($i = 0; $i &lt; $this-&gt;numFileElements; $i++) {
              $file = new InputFilter\FileInput($i);
              $file-&gt;setRequired(true);
              $file-&gt;getFilterChain()-&gt;attachByName(
                  'filerenameupload',
                  array(
                      'target'          =&gt; './data/tmpuploads/',
                      'overwrite'       =&gt; true,
                      'use_upload_name' =&gt; true,
                  )
              );
              $fileCollection-&gt;add($file);
          }
          $inputFilter-&gt;add($fileCollection, 'file-collection');
  
          // Text Input
          $text = new InputFilter\Input('text');
          $text-&gt;setRequired(true);
          $inputFilter-&gt;add($text);
  
          return $inputFilter;
      }
  }
  </code></pre>