gpt4 book ai didi

zend-framework - Zend Form File,如何向用户指示文件先前已上传?

转载 作者:行者123 更新时间:2023-12-01 09:38:31 25 4
gpt4 key购买 nike

我有一个 zend 表单,可以让用户填写对象的属性。例如,音乐艺术家。该表单包含有关艺术家的基本信息,例如姓名、电话号码、地址等等,但它也有一个文件上传字段。如果用户上传一个文件,它与该对象的该实例一起存储在数据库中(基本上是一行),如果用户想要编辑该特定实例,则该表单将重新填充对象信息。但是,表单不会重新填充文件上传信息,因为它不会重新上传它。

如何向用户发出文件已上传的信号,指出它是哪个文件,甚至可能链接到它?类似于填充表单元素的东西是首选。理想情况下,我希望能够为用户提供上传先前上传文件的能力,因此文件上传元素应该保留。任何想法?非常感谢您的阅读。

干杯,乔钦

最佳答案

我发现实现此功能的最快方法是结合 the answer from Chelmertzthis thread .

我没有定义装饰器来显示之前上传的文件,而是将链接放在元素的描述中,然后将其转储到 View 脚本中。

最终的解决方案是这样的:

Zend 表单文件:

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)
)));
}

...
}}

查看脚本以处理文件详细信息和表单元素:

<!-- 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>

最后在我拥有的 Controller 中:

//$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);

...

}

这应该给你一些看起来像:

alt text

关于zend-framework - Zend Form File,如何向用户指示文件先前已上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3689880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com