views:

1971

answers:

1

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.
+8  A: 

I designed and implemented Zend_Filter_Input.

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.

Bill Karwin
Excellent! I spent hours reading through the documentation but I must have missed this being mentioned. Thank you!
leek
It's at the end of the ZFI manual page. :-)
Bill Karwin
So this is what SO defines as being 'the definitive answer on a given question' :)
Andrew Taylor