Zend_Form_Element different render on different action

本秂侑毒 提交于 2019-12-11 02:38:31

问题


I created my own form element in Zend Framework. The only thing i would like to do is add a different functionality to the element when it's first created (so it's requested by the 'new' action), and other functionality when the element is rendered to be edited (requested by the 'edit' action).

How do i do that? I couldn't find it in the documentation.

This is my code:

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

        ;

    }

}

?>


回答1:


You could pass the action to the element as a parameter:

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

Then add a setter in your element to read the parameter into a protected variable. If you default this variable to 'new' you will only need to pass the action if the form is in edit mode (or you could use the request object to dynamically set the parameter from your controller).

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

    }

}


来源:https://stackoverflow.com/questions/6607915/zend-form-element-different-render-on-different-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!