Zend framework Zend_Form_元素在不同动作上呈现不同

Zend framework Zend_Form_元素在不同动作上呈现不同,zend-framework,action,zend-form-element,Zend Framework,Action,Zend Form Element,我在Zend框架中创建了自己的表单元素。我想做的唯一一件事是在元素第一次创建时向其添加不同的功能(因此它是由“新建”操作请求的),以及在元素呈现为可编辑时向其添加其他功能(由“编辑”操作请求的) 我该怎么做?我在文档中找不到它 这是我的代码: <?php class Cms_Form_Element_Location extends Zend_Form_Element { public function init() { App_Javascript::add

我在Zend框架中创建了自己的表单元素。我想做的唯一一件事是在元素第一次创建时向其添加不同的功能(因此它是由“新建”操作请求的),以及在元素呈现为可编辑时向其添加其他功能(由“编辑”操作请求的)

我该怎么做?我在文档中找不到它

这是我的代码:

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

        ;

    }

}

您可以将操作作为参数传递给元素:

$element = new Cms_Form_Element_Location(array('action' => 'edit');
然后在元素中添加setter,将参数读入受保护的变量。如果将此变量默认为“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());
            }

    }

}