Search code examples
zend-frameworkzend-formzend-decorators

Form's element filter doesnt work


I would like to add a filter to the text form element. The code is:

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

The filter seems not work because when i type e.g. "78abc" the value stays unchanged in the filed when i press the form submit button. Moreover i get "78abc" not "78" as "product_amt" POST parameter.


Solution

  • 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?