Override the setValue in zend form
I want the zend form to override the setValue function so that the value given to the function can be manipulated. How ca开发者_如何转开发n i implement this?
My current form structure is like this:
class My_Form_Login
{
public $Form;
public $Username;
public function __construct()
{
$this->Form = new Form_Abstract();
$this->init();
}
public function init()
{
$this->Username ->setValue('100');
}
}
class Form_Abstract extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setDecorators(array( 'FormElements','FormErrors','Form' ));
}
public function setValue($value)
{
$strippedValue = stripslashes($value);
return parent::setValue($strippedValue);
}
}
Thanks in advance..
You can extend the Zend_Form into your own class like:
My_Form extends Zend_Form
{
public function setValue($arg)
{
// My override code here
}
}
Then use this class instead of the Zend_Form directly
Consider using the Zend filter interface instead. For example
class My_Filter_Stripslashes implements Zend_Filter_Interface
{
public function filter($value)
{
return get_magic_quotes_gpc() ? $this->_clean($value) : $value;
}
protected function _clean($value)
{
return is_array($value) ? array_map(array($this, '_clean'), $value) : stripslashes($value);
}
}
Then apply this to your elements after adding them
$form->setElementFilters(array(new My_Filter_Stripslashes));
精彩评论