2011-07-07 7 views
2

私はZend Frameworkで独自のフォーム要素を作成しました。私がしたいのは、要素が最初に作成されたとき(つまり、 '新しい'アクションによって要求されたとき)、要素が編集されるようにレンダリングされるときに、アクション)。Zend_Form_Element別のアクションで別のレンダリング

どうすればよいですか?私はドキュメンテーションでそれを見つけることができませんでした。

これは私のコードです:

<?php 

class Cms_Form_Element_Location extends Zend_Form_Element { 

    public function init() { 

     App_Javascript::addFile('/static/scripts/cms/location.js'); 

     $this 
      ->setValue('/') 
      ->setDescription('Enter the URL') 
      ->setAttrib('data-original-value',$this->getValue()) 

     ; 

    } 

} 

>

答えて

4

あなたは、パラメータとして要素にアクションを渡すことができます?

$element = new Cms_Form_Element_Location(array('action' => 'edit'); 

次に読むためにあなたの要素でセッターを追加パラメータを保護された変数に変換します。この変数を 'new'にデフォルト設定した場合は、フォームが編集モードの場合のみアクションを渡すか、要求オブジェクトを使用してコントローラからパラメータを動的に設定する必要があります。

<?php 

class Cms_Form_Element_Location extends Zend_Form_Element 
{ 

    protected $_action = 'new'; 

    public function setAction($action) 
    { 
     $this->_action = $action; 
     return $this; 
    } 

    public function init() 
    { 

     App_Javascript::addFile('/static/scripts/cms/location.js'); 

     switch ($this->_action) { 
      case 'edit' : 

       // Do edit stuff here 

       break; 

      default : 

       $this 
        ->setValue('/') 
        ->setDescription('Enter the URL') 
        ->setAttrib('data-original-value',$this->getValue()); 
      } 

    } 

} 
+0

非常にスマートです!ありがとう! – sparkle

関連する問題