zend-frameworkzend-formzend-filter

Does exist Zend Filter similar to Zend Validator Identical?


Does exist Zend Filter similar to Zend Validator Identical?

The case I should filter input=='test'

$el->addFilter('Identical','test');

The problem that such filter not exist.

Thanks, Yosef


Solution

  • I'm not sure how this filter should work, since it is not clear from your question. Anyway, I made some custom filter that will check if a value of input filed is equal to some $token. If they are equal than the input value will be empty string.

    The filter looks as follows:

    // file: APPLICATION_PATH/filters/Identical.php 
    
    class My_Filter_Identical implements Zend_Filter_Interface {
    
        /**
         * Token with witch input is compared
         *
         * @var string
         */
        protected $_token;
    
        /**
         * Set token
         *
         * @param  string
         * @return void
         */
        public function __construct($token = '') {
            $this->_token = $token;
        }
    
        /**
         * Filtering method 
         *
         * @param string $value value of input filed
         * @return string
         */
        public function filter($value) {
    
            if ($value !== $this->_token) {
                return $value;
            }
    
            return '';
        }
    
    }
    

    To apply it to a given form element:

    require_once (APPLICATION_PATH . '/filters/Identical.php');
    $el1->addFilter(new My_Filter_Identical('test'));
    

    Off course instead of require_once it could be added to your resource autoloader, but as an example I think it is not needed right now.

    Edit:

    Forgot to mention pregReplace filter. The same what the custom filter above does could be done using pregReplace filter:

    $el1->addFilter('pregReplace',array('/test/',''));
    

    But, as I said, I'm not sure how you want your filter to work. If you provide more info maybe I could help more.