开发者

Zend Framework: My custom form filter is not filtering!

So I have a form that is using a custom filter (which is really just a copy of Zend_Filter_Null). When I call it directly, it works:

$makeZeroNull = new My_Filter_MakeZeroNull();
$null = $makeZeroNull->filter('0');
//$null === null

However, when I try to add it to an element in my form, it doesn't filter the value when I call getV开发者_如何学运维alue().

class My_Form extends Zend_Form {
    public function init() {
        $makeZeroNull = new My_Filter_MakeZeroNull();
        $this->addElement('text', 'State_ID', array('filters' => array($makeZeroNull)));
    }
}

//in controller
if ($form->isValid($_POST)) {
    $zero = $form->State_ID->getValue();
    //getValue() should return null, but it is returning 0
}

What is going on? What am I doing wrong?


Update: now I realize that this isn't working, so I need to come up with a different solution

The only way I was able to solve this issue was to specifically add the filter after adding it to the form:

class My_Form extends Zend_Form {
    public function init() {
        $makeZeroNull = new Zend_Filter_Null();
        $this->addElement('text', 'State_ID');
        $this->getElement('State_ID')->addFilter($makeZeroNull);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜