Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is not a simple or ideal solution because it requires an extension of the File decorator... but it's rather frustrating that they didn't make the effort to separate the hidden element generation logic from the file input generation logic. I'm not sure if the file view helper handles the issue of an element being an array (that seems to be the reason they did it this way.)</p> <p><b>Extension of File Decorator: </b> (the commented out part is what causes the extra input to be generated.)</p> <pre><code>&lt;?php class Sys_Form_Decorator_File extends Zend_Form_Decorator_File { public function render($content) { $element = $this-&gt;getElement(); if (!$element instanceof Zend_Form_Element) {return $content;} $view = $element-&gt;getView(); if (!$view instanceof Zend_View_Interface) {return $content;} $name = $element-&gt;getName(); $attribs = $this-&gt;getAttribs(); if (!array_key_exists('id', $attribs)) {$attribs['id'] = $name;} $separator = $this-&gt;getSeparator(); $placement = $this-&gt;getPlacement(); $markup = array(); $size = $element-&gt;getMaxFileSize(); if ($size &gt; 0) { $element-&gt;setMaxFileSize(0); $markup[] = $view-&gt;formHidden('MAX_FILE_SIZE', $size); } if (Zend_File_Transfer_Adapter_Http::isApcAvailable()) { $apcAttribs = array('id' =&gt; 'progress_key'); $markup[] = $view-&gt;formHidden('APC_UPLOAD_PROGRESS', uniqid(), $apcAttribs); } else if (Zend_File_Transfer_Adapter_Http::isUploadProgressAvailable()) { $uploadIdAttribs = array('id' =&gt; 'progress_key'); $markup[] = $view-&gt;formHidden('UPLOAD_IDENTIFIER', uniqid(), $uploadIdAttribs); } /* if ($element-&gt;isArray()) { $name .= "[]"; $count = $element-&gt;getMultiFile(); for ($i = 0; $i &lt; $count; ++$i) { $htmlAttribs = $attribs; $htmlAttribs['id'] .= '-' . $i; $markup[] = $view-&gt;formFile($name, $htmlAttribs); } } else {$markup[] = $view-&gt;formFile($name, $attribs);} */ $markup = implode($separator, $markup); switch ($placement) { case self::PREPEND: return $markup . $separator . $content; case self::APPEND: default: return $content . $separator . $markup; } } } ?&gt; </code></pre> <p><b>Form setup in controller action:</b></p> <pre><code>$form = new Zend_Form(); $form-&gt;addElement(new Zend_Form_Element_File('file')); $form-&gt;file-&gt;setLabel('File'); $form-&gt;file-&gt;setDescription('Description goes here.'); $decorators = array(); $decorators[] = array('File' =&gt; new Sys_Form_Decorator_File()); $decorators[] = array('ViewScript', array('viewScript' =&gt; '_formElementFile.phtml')); $form-&gt;file-&gt;setDecorators($decorators); $this-&gt;view-&gt;form = $form; </code></pre> <p><b>In action view:</b></p> <pre><code>&lt;?php echo $this-&gt;form; ?&gt; </code></pre> <p><b>In element script:</b></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><b>Output should be:</b></p> <pre><code>&lt;form enctype="multipart/form-data" action="" method="post"&gt; &lt;dl class="zend_form"&gt; &lt;input type="hidden" name="MAX_FILE_SIZE" value="134217728" id="MAX_FILE_SIZE" /&gt; &lt;div class="field" id="field_file"&gt; &lt;label for="file"&gt;File&lt;/label&gt; &lt;span class="value"&gt;&lt;input type="file" name="file" id="file" /&gt;&lt;/span&gt; &lt;span class="hint"&gt;Description goes here.&lt;/span&gt; &lt;/div&gt; &lt;/dl&gt; &lt;/form&gt; </code></pre> <p>A problem with this solution is that the hidden elements don't render within the viewscript; this might be a problem if you're using the div as a selector in a client-side script...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload