2012-01-05 18 views
0

を介して設定IDまたはクラスのいずれかが、私は自分のフォームを設定するZend_Config_Iniをで、次のデフォルトのデコレータを持っている必要があります:Zendのフォーム要素の行は、Zend_Config_Iniを

elementDecorators.viewHelper.decorator = "ViewHelper" 
elementDecorators.label.decorator = "Label" 
elementDecorators.errors.decorator = "Errors" 
elementDecorators.htmlTag.decorator = "HtmlTag" 
elementDecorators.htmlTag.options.tag = "li" 

私はZend_Config_Iniをしても、次の要素の定義を持っている:

elements.username.type = "text" 
elements.username.options.label = "Username:" 
elements.username.options.required = true 

と、次の出力が生成されます

<li> 
    <label for="username" class="required">Username:</label> 
    <input type="text" name="username" id="username" value="" /> 
</li> 

今WH私は知っておく必要があります、どのように私は(iniの設定ファイルを介して行うことができます)、LIタグのIDまたはクラスを設定するのですか?私は、次のような出力をしたいと思います:

<li id="form-username-element"> ... </li> 

または

<li class="form-2col"> ... </li> 

アップデート: 私はこのように自分自身をconfigの要素内のすべてのデコレータをオーバーライドすることでそれを得ることができました:

elements.username.options.decorators.viewHelper.decorator = "ViewHelper" 
elements.username.options.decorators.label.decorator = "Label" 
elements.username.options.decorators.errors.decorator = "Errors" 
elements.username.options.decorators.htmlTag.decorator = "HtmlTag" 
elements.username.options.decorators.htmlTag.options.tag = "li" 
elements.username.options.decorators.htmlTag.options.class = "username-row-element" 

しかし、それは動作しますが、すべての要素に移動する必要があるので、多くの重複を作成します(単一の変更o fはクラス設定自体である最後の行です)。では、iniファイルから、デフォルトのデコレータを使用してクラス名をオーバーライドする方法があります(各要素のすべてのデコレータを複製する必要はありません)。

答えて

0

最も簡単なことは、独自のデコレータを作成することです。たとえば、各要素をdivで囲み、必要なクラスとIDを追加するElementWrapデコレータを作成しました。次のように表示されます。

class Form_Decorator_ElementWrap extends Zend_Form_Decorator_Abstract 
{ 
    public function render($content) 
    { 
     $element = $this->getElement(); 
     if($this->getOption('openOnly')) { 
      return '<div class="'.$this->getClass().'" id="'.$this->getId().'">' . $content; 
     } else if($this->getOption('closeOnly')) { 
      return $content . PHP_EOL . '</div>' . PHP_EOL; 
     } else { 
      return '<div class="'.$this->getClass().'" id="'.$this->getId().'">' . $content . '</div>'; 
     } 
    } 

    public function getClass() 
    { 
     $element = $this->getElement(); 
     $classes = array(
      'field_wrap', 
      'field_' . strtolower(substr(strrchr($element->getType(), '_'), 1)), 
      $this->getOption('class'), 
     ); 
     if($element->hasErrors()) { 
      $classes[] = 'field_error'; 
     } 
     if($elementClass = $element->getAttrib('class')) { 
      $classes[] = $elementClass; 
     } 
     return implode(' ', array_filter($classes)); 
    } 

    public function getId() 
    { 
     return 'fieldwrap-' . $element->getId(); 
    } 
} 
関連する問題