Zend form file, How do I tell the user that the file was previously uploaded?

I have a zend form that allows the user to populate the attributes of an object. For example, a music artist. The form has basic information about the artist, such as name, phone number, address, and what is not, but it also has a field for downloading files. If the user uploads a file, this instance of the object stores it in the database (essentially a string), if the user ever wants to edit this particular instance, the form will be populated with information about the objects. However, the form does not update the file upload information because it is not going to reload it.

How can I tell the user that the file has already been downloaded, indicate which file it was, and possibly even a link to it? something similar to filling out form elements is preferred. Ideally, I would like to offer the user the ability to download a previously downloaded file, so the file upload element should remain. Any ideas? Thanks so much for reading.

Cheers, Joe Chin

+3
source share
2 answers

The fastest way I've found this is to combine the answer from Chelmertz with this thread .

, , , script.

:

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

    ...
}}

script :

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

    ...

}

- :

alt text

+6

, , , , , ?

.

. , , . ( , .)

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

"" .

+2

Source: https://habr.com/ru/post/1764274/


All Articles