title: How do I use ViewScripts on Zend_Form File Elements?
body: <p>I am using this ViewScript for my standard form elements:</p>
  
  <pre><code>&lt;div class="field" id="field_&lt;?php echo $this-&gt;element-&gt;getId(); ?&gt;"&gt;
     &lt;?php if (0 &lt; strlen($this-&gt;element-&gt;getLabel())) : ?&gt;
        &lt;?php echo $this-&gt;formLabel($this-&gt;element-&gt;getName(), $this-&gt;element-&gt;getLabel());?&gt;
     &lt;?php endif; ?&gt;
     &lt;span class="value"&gt;&lt;?php echo $this-&gt;{$this-&gt;element-&gt;helper}(
        $this-&gt;element-&gt;getName(),
        $this-&gt;element-&gt;getValue(),
        $this-&gt;element-&gt;getAttribs()
     ) ?&gt;&lt;/span&gt;
     &lt;?php if (0 &lt; $this-&gt;element-&gt;getMessages()-&gt;length) : ?&gt;
         &lt;?php echo $this-&gt;formErrors($this-&gt;element-&gt;getMessages()); ?&gt;
     &lt;?php endif; ?&gt;
     &lt;?php if (0 &lt; strlen($this-&gt;element-&gt;getDescription())) : ?&gt;
        &lt;span class="hint"&gt;&lt;?php echo $this-&gt;element-&gt;getDescription(); ?&gt;&lt;/span&gt;
     &lt;?php endif; ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Trying to use that ViewScript alone results in an error:</p>
  
  <blockquote>
    <p>Exception caught by form: No file
    decorator found... unable to render
    file element</p>
  </blockquote>
  
  <p>Looking at <a href="http://framework.zend.com/wiki/display/ZFFAQ/Forms" rel="noreferrer">this FAQ</a> revealed part of my problem, and I updated my form element decorators like this:</p>
  
  <pre><code>'decorators' =&gt; array(
     array('File'),
     array('ViewScript', array('viewScript' =&gt; 'form/field.phtml'))
  )
  </code></pre>
  
  <p>Now it's rendering the file element twice, once within my view script, and extra elements with the file element outside my view script:</p>
  
  <pre><code>&lt;input type="hidden" name="MAX_FILE_SIZE" value="8388608" id="MAX_FILE_SIZE" /&gt;
  &lt;input type="hidden" name="UPLOAD_IDENTIFIER" value="4b5f7335a55ee" id="progress_key" /&gt;
  &lt;input type="file" name="upload_file" id="upload_file" /&gt;
  &lt;div class="field" id="field_upload_file"&gt;
      &lt;label for="upload_file"&gt;Upload File&lt;/label&gt;
      &lt;span class="value"&gt;&lt;input type="file" name="upload_file" id="upload_file" /&gt;&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Any ideas on how to handle this properly with a ViewScript?</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong> Based on Shaun's solution, here's my final code:</p>
  
  <p>Form Element:</p>
  
  <pre><code>$this-&gt;addElement('file', 'upload_file', array(
      'disableLoadDefaultDecorators' =&gt; true,
      'decorators' =&gt; array('File', array('ViewScript', array(
          'viewScript' =&gt; '_form/file.phtml',
          'placement' =&gt; false,
      ))),
      'label' =&gt; 'Upload',
      'required' =&gt; false,
      'filters' =&gt; array(),
      'validators' =&gt; array(array('Count', false, 1),),
  ));
  </code></pre>
  
  <p>View Script:</p>
  
  <pre><code>&lt;?php
  $class .= 'field ' . strtolower(end(explode('_',$this-&gt;element-&gt;getType())));
  if ($this-&gt;element-&gt;isRequired()) {
      $class .= ' required';
  }
  if ($this-&gt;element-&gt;hasErrors()) {
      $class .= ' errors';
  }
  ?&gt;
  &lt;div class="&lt;?php echo $class; ?&gt;" id="field_&lt;?php echo $this-&gt;element-&gt;getId(); ?&gt;"&gt;
      &lt;?php if (0 &lt; strlen($this-&gt;element-&gt;getLabel())): ?&gt;
          &lt;?php echo $this-&gt;formLabel($this-&gt;element-&gt;getFullyQualifiedName(), $this-&gt;element-&gt;getLabel());?&gt;
      &lt;?php endif; ?&gt;
      &lt;span class="value"&gt;&lt;?php echo $this-&gt;content; ?&gt;&lt;/span&gt;
      &lt;?php if ($this-&gt;element-&gt;hasErrors()): ?&gt;
          &lt;?php echo $this-&gt;formErrors($this-&gt;element-&gt;getMessages()); ?&gt;
      &lt;?php endif; ?&gt;
      &lt;?php if (0 &lt; strlen($this-&gt;element-&gt;getDescription())): ?&gt;
          &lt;p class="hint"&gt;&lt;?php echo $this-&gt;element-&gt;getDescription(); ?&gt;&lt;/p&gt;
      &lt;?php endif; ?&gt;
  &lt;/div&gt;
  </code></pre>