Hello,
The fastest way I found to get this implemented was to combine the answer from Chelmertz with this thread http://stackoverflow.com/questions/2143462/how-do-i-use-viewscripts-on-zend-form-file-elements. Instead of defining a decorator to show the previously uploaded file, I am placing the link in the description for the element and then dumping it into a view script (at least I think thats what I am doing). The final solution looks like this:
Zend Form File:
class Application_Form_LabDetails extends Zend_Form{
private $_uploadPath;
private $_artistID;
private $_singleFile;
public function __construct($options = array()) {
if(isset($options['uploadPath'])) {
$this->_uploadPath = $options['uploadPath'];
unset($options['uploadPath']);
}
if(isset($options['artistID'])) {
$this->_artistID = sprintf("%06s",(string)$options['artistID']);
unset($options['artistID']);
}
if(isset($options['singleFile'])) {
$this->_singleID = $options['singleFile'];
unset($options['singleFile']);
}
return parent::__construct($options);
}
public function init()
{
$this->setName('artistDetails');
...
$singleID = $this->createElement('file', '$singleFile');
$singleID->setLabel('Current Single')
->setDestination('data/uploads')
->addValidator('count',true, 1)
->addValidator('Size', true, 5242880)
->addValidator('Extension', true, 'mp3');
if($this->_cocFile){
$singleID->setDescription(
'<a href="/'.$this->_uploadPath.'/'.$this->_artistID
.$this->_singleFile.'" taget="_blank">'
.$this->_singleFile.'</a>')
->setDecorators(
array('File',
array('ViewScript',
array('viewScript' => 'artist/file.phtml', 'placement' => false)
)));
}
...
}}
View script to handle file details and form element:
<!-- decorator through view script, placed in 'views/scripts/controller/file.phtml' -->
<!-- outputs form label markup -->
<dt id="<?php echo $this->element->getName(); ?>-label">
<label for="<?php echo $this->element->getName(); ?>"
class="<?php if ($this->element->isRequired()): ?>required<?php endif; ?>">
<?php echo $this->element->getLabel(); ?></label><br />
<span>Uploaded: <?php echo $this->element->getDescription(); ?></span>
</dt>
<!-- outputs form element markup -->
<dd id="<?php echo $this->element->getName(); ?>-element">
<?php echo $this->content; ?>
<?php if ($this->element->hasErrors()): ?>
<ul class="error">
<?php echo $this->formErrors($this->element->getMessages()); ?>
</ul>
<?php endif; ?>
</dd>
Lastly in the controller I have:
//$id is taken from the URI
if ($id > 0) {
$artistDetails = new Application_Model_DbTable_ArtistDetails();
$artistData = $artistDetails->getArtistDetails($id);
$options = array(
'uploadPath' => $config->uploads->labs->path,
'artistID' => $artistData['a_id'],
'singleFile' => $artistData['a_singleFile'],
);
$form = new Application_Form_LabDetails($options);
...
}
This should give you something that looks like:
If there are questions, let me know, I'm here to help.
Cheers,
JoeChin