Zend framework Zend Form File,如何向用户指示以前上载过文件?
我有一个zend表单,允许用户填写对象的属性。例如,一位音乐艺术家。表单中有艺术家的基本信息,比如姓名、电话号码、地址等等,但也有一个文件上传字段。如果用户上传一个文件,该文件与该对象的实例一起存储在数据库中(基本上是一行),如果用户想要编辑该特定实例,则表单将重新填充对象信息。但是表单不会重新填充文件上载信息,因为它不会重新加载 我如何向用户发出文件已上载的信号,指示该文件是哪个文件,甚至可能是指向该文件的链接?最好使用类似于填充表单元素的方法。理想情况下,我希望能够为用户提供上传之前上传的文件的能力,因此文件上传元素应该保留。有什么想法吗?非常感谢你的阅读 干杯, 乔琴 我如何向用户发出信号,表示 文件已上载,请指出 它是哪个文件,甚至可能是 链接到它 我会打印出以前上传的文件 要实现这一点,可以使用表单装饰器。这是一个草图,需要在考虑安全性的情况下进行修改和重构,但它确实打印了一个图像标记。(如果您希望显示文件列表,最后会有一个提示。)Zend framework Zend Form File,如何向用户指示以前上载过文件?,zend-framework,zend-form,zend-file,Zend Framework,Zend Form,Zend File,我有一个zend表单,允许用户填写对象的属性。例如,一位音乐艺术家。表单中有艺术家的基本信息,比如姓名、电话号码、地址等等,但也有一个文件上传字段。如果用户上传一个文件,该文件与该对象的实例一起存储在数据库中(基本上是一行),如果用户想要编辑该特定实例,则表单将重新填充对象信息。但是表单不会重新填充文件上载信息,因为它不会重新加载 我如何向用户发出文件已上载的信号,指示该文件是哪个文件,甚至可能是指向该文件的链接?最好使用类似于填充表单元素的方法。理想情况下,我希望能够为用户提供上传之前上传的文
//在我的情况下必须重写文件装饰程序
类ArtistDecorator扩展Zend_Form_Decorator_文件{
私人$\u artistId;
公共函数构造($options=array()){
如果(isset($options['artistId'])){
$this->_artistId=$options['artistId'];
未结算($options['artistId']);
}
返回父项::_构造($options);
}
公共功能渲染($content){
返回“\u artistId.”.jpg“/>”。$content;
}
}
类应用程序表单登录扩展了Zend表单{
私人$\u artistId;
公共函数构造($options=array()){
如果(isset($options['artistId'])){
$this->_artistId=$options['artistId'];
未结算($options['artistId']);
}
返回父项::_构造($options);
}
公共函数init(){
美元这个
->setAction(“/”)
->setMethod(“post”);
$el=新的Zend_表单_元素_文件(“图像”);
如果($this->\u artistId){
$el->setDecorators(数组(新的ArtistDecorator(数组('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>
将“歌曲”注入表单的构造函数。我发现实现这一点的最快方法是与 我没有定义一个decorator来显示先前上传的文件,而是将链接放在元素的描述中,然后将其转储到视图脚本中 最终的解决方案如下所示: 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)
)));
}
...
}}
这将为您提供如下内容:
您好,我已经使用Zend大约一个月了。在我的控制器中,我有一行$form->options=array('trackName'=>'trackFileName');我收到以下错误:“消息:没有为显示组指定有效的元素。”“我认为这是因为$options不是一个定义的表单元素。如何将此数组推送到装饰器的窗体中?ThanksHi,请忽略我最后的评论,通过更多的谷歌搜索,我发现我可以使用:$form=new Zend_form(array())传递数组;是的,您应该尝试将表单的逻辑移出控制器,以便表单可以轻松地在多个操作中重用(即使不需要,这也是非常好的分离实践)。让我们知道它是如何工作的。嗨,好的,我让它工作了。我需要能够做的一件事是仍然生成表单上传元素,以防文件需要用新文件重写。有了decorator,我可以显示哪些文件已经上传,但是元素本身消失了。这是我第一次使用装饰器,是否有一种简单的方法将此列表预先添加到内容中,以便在文件上载元素标签之前完成?它确实很混乱,但您需要将我的
ArtistDecorator::render()
-方法替换为以下内容:
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);
...
}