views:

188

answers:

1

Hi,

Is it possible to use Zend_Filter_Input as a generic input filter? I want to use it to filter all form fields (strip tags etc but no validation). All the examples seem to include a $validators array and pre-suppose that I will know the names of the fields on the way in.

Because of the nature of the project, timescales etc, it is not possible to rewrite the forms using Zend_Form. There is a generic Form class which handles all form input so I need to do the filtering in there.

Thanks!

Luke.

+2  A: 

You can simply pass an empty array for the $validators argument to skip validation and simply use filtering.

Are you saying that you don't know the field names you'll pass into the Zend_Filter_Input instance? You can use the wildcard *-field to apply a filter to all input fields. Is this what you're asking for?

$input = new Zend_Filter_Input(array(
    '*' => 'StripTags'
), array(), $data);

will filter all values in $data with the Zend_Filter_StripTags filter.

EDIT:

Retrieve the values with

$escaped = $input->getEscaped(); // will be automatically run through an HTML-entities-filter
// or
$unescaped = $input->getUnescaped(); // the values as they come out of the filter-chain.
Stefan Gehrig
Thanks, I tried: public static function zfFilterInput($input) { $filters = array('*' => array('StringTrim','HtmlEntities','StripTags')); $input = new Zend_Filter_Input($filters,array(),$input); return $input; }but get an error stating that argument 3 cannot be an array. Do I need to loop through the data array and how do I got my cleaned data out:?
codecowboy
You have a problem with using the variable $input twice. First your function argument is called $input and then you assign the Zend_Filter_Input to $input making your code useless.
Stefan Gehrig
Why do you first encode everything into HTML-entities and then do strip-tags? There aren't any tags left after running the HTML-entities filter.
Stefan Gehrig
See edited answer for how to retrieve the values.
Stefan Gehrig