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