I've created a custom filter for my form and have set the prefix path
correctly (the file/class is being found and loaded).  However, it is not
being instanciated properly: the options are not being passed/set.  I've
tried digging through the ZF code (Zend_Form_Element::addFilters()), but am
a bit confused.

Am I doing everything right?  TIA!


class My_Filter_MyFilter implements Zend_Filter_Interface
{
    protected $_strict = false;

    protected $_values = array();

    public function __construct($options = null)
    {
        if (is_array($options))
        {
            $this->setOptions($options);
        }
    }

    public function setOptions(array $options)
    {
        if (isset($options['strict']))
        {
            $this->_strict = (bool) $options['strict'];
        }
        if (isset($options['values']))
        {
            $this->_values = $options['values'];
        }
        return $this;
    }

    public function filter($value)
    {
        if (key_exists($value, $this->_values)
        {
            return $this->_values[$value];
        }
        elseif ($this->_strict)
        {
            throw new Zend_Exception("Invalid value: [$value]");
        }
        return $value;
    }
}



Then, add the filter using PHP,

$form->getElement('field')->addFilter('MyFilter', array(
    'strict' => true,
    'values' => array('foo' => 'bar', 'baz' => 'bat')
));


or, using a config file

form.elementPrefixPath.filter.prefix = "My_Filter"
form.elementPrefixPath.filter.path = "My/Filter/"
form.elementPrefixPath.filter.type = "filter"

form.elements.field.type = "text"
form.elements.field.filters.my.filter = "MyFilter"
form.elements.field.filters.my.options.strict = 1
form.elements.field.filters.my.options.values.foo = "bar"
form.elements.field.filters.my.options.values.baz = "bat"

-- 
View this message in context: 
http://www.nabble.com/Custom-Filter-with-options-and-Config-tp24237075p24237075.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to