Does exist Zend Filter similar to Zend Validator Identical?

552 Views Asked by At

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

3

There are 3 best solutions below

0
On BEST ANSWER

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.

1
On

Your question isn't all that clear - do you want a filter which removes the word test? Or are you talking about filtering a form input? So taking your example you want to remove from the el input what the test input contains?

If you want to remove test from your input, you could use Zend_Filter_PregReplace

$filter = new Zend_Filter_PregReplace(array('match' => '/test/', 'replace' => ''));
$input  = 'What is this test about!';
$filter->filter($input);

Should give you What is this about!

There isn't a filter which would filter identical form input if its been entered into another input I don't think. You could try to create your own input filter and perform your own logic on the input.

2
On

It is not that clear what you are trying to do. If you give more explanation that would be good.

I need to remove all input, soo its not good to use regex.

If you just want to clear the data in form elements you can use one of the following:

  1. Clear an element value by setting the value of the element to nothing.

    $el->setValue(null);

  2. or reset all form elements

    $form->reset();