0

我想为文本表单元素添加一个过滤器。代码是:

$this->addElement('text', 'product_amt', array(
    'filters' => array('Digits'),       
    'required' => true )
);

过滤器似乎不起作用,因为当我输入例如“78abc”时,当我按下表单提交按钮时,字段中的值保持不变。此外,我得到“78abc”而不是“78”作为“product_amt”POST 参数。

4

1 回答 1

0

The value coming from POST will be the value the user has entered in the input field. The filters are applied when you call isValid on the form object:

$form = new My_Form(); // your form object
if( $form->isValid( $this->getRequest()->getPost() ) ) { //we pass in the POST data into the isValid function
    //Valid data - do something
} else {
    //data is not valid - do something else
}

During the isValid function, the value for each element is filtered as required (in this case into only Digits) then set as the elements' value before the elements' isValid function is called to make sure the value passes any validation (in this case 'isRequired' as you have set 'required' to true).

Are you calling isValid on your form?

于 2013-11-07T12:03:50.793 回答