2

ZendFrameworkで独自のフォーム要素を作成しました。私がやりたいのは、要素が最初に作成されたとき(つまり、「new」アクションによって要求されたとき)に別の機能を追加し、要素が編集用にレンダリングされたとき(「edit」によって要求されたとき)に他の機能を追加することだけです。アクション)。

それ、どうやったら出来るの?ドキュメントで見つかりませんでした。

これは私のコードです:

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

1 に答える 1

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

    }

}
于 2011-07-07T10:21:03.783 に答える