9

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.

1 Answer 1

19

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Excellent! I spent hours reading through the documentation but I must have missed this being mentioned. Thank you!
So this is what SO defines as being 'the definitive answer on a given question' :)
@BillKarwin, The documentation link is broken. Can you provide an update one ?
@FopaLéonConstantin, I have fixed the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.