Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Filters/Validators in Zend Framework

I have a Zend Framework application structure as below:

/application
/library
    /Zend
    /Core
        /Filter
            /MyFilter.php
        /Validator
            /MyValidator.php

I would like to put custom filters and validators in their respective folders and have them loaded automatically when used. However, I cannot figure out how to best accomplish this.

I need the solution to work with Zend_Filter_Input in this fashion:

$filters = array(
    'month'   => 'Digits',
    'account' => 'StringTrim',
    'other'   => 'MyFilter'
);

$validators = array(
    'account' => 'Alpha',
    'other'   => 'MyValidator'
);

$inputFilter = new Zend_Filter_Input($filters, $validators);

What I already know:

  • Core_Filter_MyFilter implements Zend_Filter_Interface
  • Obviously, the filters and validators are already in my include path.
like image 735
leek Avatar asked Oct 08 '08 21:10

leek


1 Answers

I designed and implemented Zend_Filter_Input back in 2007.

You can add new class prefixes to help load your custom filter and validator classes. By default, Zend_Filter_Input searches for classes that have the prefixes "Zend_Filter" and "Zend_Validate". Try this:

$inputFilter->addNamespace('Core_Filter');

Before you run isValid() or other methods of the object.

Alternatively, you can also pass a new class prefix string in the options array, the fourth argument to the Zend_Filter_Input constructor:

$options = array('inputNamespace' => 'Core_Filter');
$inputFilter = new Zend_Filter_Input($filters, $validators, $data, $options);

See also the documentation I wrote for Zend_Filter_Input.

like image 159
Bill Karwin Avatar answered Sep 19 '22 15:09

Bill Karwin