2012-04-02 23 views
1

私はZendの初心者です。私はユーザーのコミュニティのための記事を書くことができる小さなWebサイトを持っています。各記事について、ユーザーは1つまたは複数のファイルを添付することができます。私は管理者が記事を更新できるようにするためのフォームを作った。Zend_Form_Decorator_File how to

記事に添付されている各ファイルの入力ファイルの一覧を表示できます。

ここで、入力ファイルコントロールの右側にあるファイルへのリンクを表示します。

私はデコレータを使用する必要があると思いますが、それを動作させる方法を理解するのは苦労します。

お願いします。

答えて

0

あなたのファイルの要素へのリンクを追加するViewHelper Decoratorと一緒にView Helperを使用する簡単な方法です。

まず、あなたはすでに、セットアップヘルパーパスを持っているapplication.iniにこれを追加しない場合:

resources.view.helperPath.My_View_Helper = "My/View/Helper/" 

次に、あなたのパスに(libraryフォルダが素晴らしい作品)、ディレクトリツリーMy/View/Helperを作成します。

私はそれLinkを呼び出しています例えば、上記のディレクトリにビューヘルパーを作成し、そのLink.phpのMy/View/Helper/Link.php

内容を作成することです:

<?php 

class My_View_Helper_Link extends Zend_View_Helper_Abstract 
{ 
    public function link($name, $value, $attribs, $elOptions) 
    { 
     if (!isset($attribs['linkOpts']) || !is_array($attribs['linkOpts'])) 
      return ''; 

     $linkOpts = $attribs['linkOpts']; 

     $link = (isset($linkOpts['href'])) ? $linkOpts['href'] : ''; 
     $text = (isset($linkOpts['text'])) ? $linkOpts['text'] : ''; 

     if ($link == '' || $text == '') return ''; 

     return sprintf('<a href="%s">%s</a>', $link, htmlspecialchars($text)); 
    } 
} 

さて、あなたが作成したときに、あなたの要素を追加するには、ViewHelperデコレータを追加し、いくつかのリンクオプションを渡すだけです。あなたはファイルの要素に対するデコレータのスタックを使用して、要素にlinkOptsを提供する場合

$fileDecorators = array(
    'File', 
    array('ViewHelper', array('helper' => 'link')), // Add ViewHelper decorator telling it to use our Link helper 
    'Errors', 
    array('Description', array('tag' => 'p', 'class' => 'description')), 
    array('HtmlTag',  array('class' => 'form-div')), 
    array('Label',  array('class' => 'form-label', 'requiredSuffix' => '*')) 
); 

$this->addElement('file', 'file1', array(
    'label'  => 'File Upload:', 
    'required' => false, 
    'decorators' => $fileDecorators, 
    'validators' => array(
     /* validators here */ 
    ), 
    'linkOpts' => array('href' => 'http://site.com/page/link', 
         'text' => 'This is the link text', 
    ), 
)); 

さて、それはファイルの入力後にリンクをレンダリングします。 linkOptsが指定されていない場合、またはhrefまたはtext要素の場合、File要素の後にはリンクが出力されません。

希望に役立ちます。