Of course, it seems that creating a custom element that supports filtering after validation will be a way. How about this:
class My_Form_Element_PostValidateFilterable extends Zend_Form_Element_Text { protected $_postValidateFilters = array(); public function setPostValidateFilters(array $filters) { $this->_postValidateFilters = $filters; return $this; } public function getPostValidateFilters() { return $this->_postValidateFilters; } public function isValid($value, $context = null) { $isValid = parent::isValid($value, $context); if ($isValid){ foreach ($this->getPostValidateFilters() as $filter){ $value = $filter->filter($value); } $this->setValue($value); } return $isValid; } }
Usage will be something like this:
$elt = $form->addElement('PostValidateFilterable', 'myElement', array( 'label' => 'MyLabel', 'filters' => array( 'StringTrim',
This keeps correctness and filtering in shape - a thin controller.
Not verified, just a blow in the dark. And, of course, you could adjust / change the API to add / remove filters with a key, etc.
What do you think?
David Weinraub
source share