Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store


It's often useful and/or necessary to perform some normalization on input prior to validation. For example, you may want to strip out all HTML, but run your validations on what remains to ensure the submission is valid. Or you may want to trim empty space surrounding input so that a StringLength validator will use the correct length of the input without counting leading or trailing whitespace characters. These operations may be performed using Zend_Filter. Zend_Form_Element has support for filter chains, allowing you to specify multiple, sequential filters. Filtering happens both during validation and when you retrieve the element value via getValue():


Filters may be added to the chain in two ways:

  • passing in a concrete filter instance

  • providing a short filter name

Let's see some examples:

// Concrete filter instance:
$element->addFilter(new Zend_Filter_Alnum());

// Short filter name:

Short names are typically the filter name minus the prefix. In the default case, this will mean minus the 'Zend_Filter_' prefix. The first letter can be upper-cased or lower-cased.

Using Custom Filter Classes

If you have your own set of filter classes, you can tell Zend_Form_Element about these using addPrefixPath(). For instance, if you have filters under the 'My_Filter' prefix, you can tell Zend_Form_Element about this as follows:


(Recall that the third argument indicates which plugin loader on which to perform the action.)

If at any time you need the unfiltered value, use the getUnfilteredValue() method:


For more information on filters, see the Zend_Filter documentation.

Methods associated with filters include:

  • addFilter($nameOfFilter, array $options = null)

  • addFilters(array $filters)

  • setFilters(array $filters) (overwrites all filters)

  • getFilter($name) (retrieve a filter object by name)

  • getFilters() (retrieve all filters)

  • removeFilter($name) (remove filter by name)

  • clearFilters() (remove all filters)

Zend Framework