Programmer's Reference Guide
| Filter Chains |
Writing Filters
Zend_Filter supplies a set of commonly needed filters, but developers will often need to write custom filters for their particular use cases. The task of writing a custom filter is facilitated by implementing Zend_Filter_Interface.
Zend_Filter_Interface defines a single method, filter(), that may be implemented by user classes. An object that implements this interface may be added to a filter chain with Zend_Filter::addFilter().
The following example demonstrates how to write a custom filter:
- class MyFilter implements Zend_Filter_Interface
- {
- public function filter($value)
- {
- // perform some transformation upon $value to arrive on $valueFiltered
- return $valueFiltered;
- }
- }
To add an instance of the filter defined above to a filter chain:
- $filterChain = new Zend_Filter();
- $filterChain->addFilter(new MyFilter());
| Filter Chains |
Add A Comment
Please do not report issues via comments; use the ZF Issue Tracker.
If you have a JIRA/Crowd account, we suggest you login first before commenting.

Comments
* EmailMaskFilter
* A simple custom filter that will hide a part of the email
* letting user to see only the first letter and the domain
* the rest part uf the email is filled with wildcards
* ex: t***@gmail.com
*/
class EmailMaskFilter implements Zend_Filter_Interface
{
public function filter($value)
{
$mask = str_pad('', strpos($value, '@') - 1, '*');
$valueFiltered = substr_replace($value, $mask, 1, strlen($mask));
return $valueFiltered;
}
}
The question of DoubleG is totally appropriate! How can you think to illustate how to write a custom filter without even tell WHERE to put it???
This is simple crazy.
then you can call the filter something like
$usernameElement= new Zend_Form_Element_Text('username');
$usernameElement->addFilter(new CustomLibrary_Filter_MyFilter)