我有一个zend表单,允许用户填写对象的属性。例如,音乐艺术家。表单包含有关艺术家的基本信息,如姓名,电话号码,地址等等,但它还有一个文件上传字段。如果用户上传了一个文件,它将该对象的实例存储在数据库中(基本上是一行),如果用户想要编辑这个特定的实例,则用对象信息重新填充该表单。但是,表单不会重新填充文件上载信息,因为它不会重新上载它。
如何向用户发出已经上传文件的信号,指出它是哪个文件,甚至可能链接到该文件?类似于填充表单元素的东西是首选。理想情况下,我希望能够为用户提供上传以前上传的文件的功能,因此文件上传元素应该保留。任何的想法?非常感谢阅读。
干杯, Joe Chin
答案 0 :(得分:6)
我发现实现这一目标的最快方法是将the answer from Chelmertz与this thread合并。
我没有定义装饰器来显示先前上传的文件,而是将链接放在元素的描述中,然后将其转储到视图脚本中。
最终解决方案如下:
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>
最后在控制器中我有:
//$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);
...
}
这应该给你一些看起来像:
答案 1 :(得分:2)
我如何向用户发出信号? 文件已经上传,请注明 它是哪个文件,甚至可能 链接到它?
我会打印出之前上传的文件。
要完成此任务,您可以使用表单装饰器。这是一个需要在考虑安全性的情况下进行修改和重构的草图,但会打印出图像标记。(如果您更愿意显示文件列表,则会有一个提示端。)
// has to override a File-decorator in my case
class ArtistDecorator extends Zend_Form_Decorator_File {
private $_artistId;
public function __construct($options = array()) {
if(isset($options['artistId'])) {
$this->_artistId = $options['artistId'];
unset($options['artistId']);
}
return parent::__construct($options);
}
public function render($content) {
return '<img src="/images/'.$this->_artistId.'.jpg" />'.$content;
}
}
class Application_Form_Login extends Zend_Form{
private $_artistId;
public function __construct($options = array()) {
if(isset($options['artistId'])) {
$this->_artistId = $options['artistId'];
unset($options['artistId']);
}
return parent::__construct($options);
}
public function init() {
$this
->setAction('/')
->setMethod('post');
$el = new Zend_Form_Element_File('image');
if($this->_artistId) {
$el->setDecorators(array(new ArtistDecorator(array('artistId' => 3))));
}
$this->addElement($el);
}
}
你应该不填充文件元素,因为你不希望它反复上传同一个文件。
如果文件是mp3左右:遵循相同的例程,但使用如下输出:
<ul>
<li><a href="/music/song_1.mp3">Song 1</a></li>
<li><a href="/music/song_2.mp3">Song 2</a></li>
</ul>
将“歌曲”注入表单的构造函数。