Search code examples
zend-frameworkzend-validate

Why is my Zend Validate Digits not recognised?


I'm creating a form with the Zend Framework to capture user demographics. I'm struggling with validation for digits on two text fields.

I build my form in ../application/forms/UserDemographics.php like this:

class Application_Form_UserDemographics extends Zend_Form
    {
        public function init()
        {
            $this->form = new Zend_Form;            
            $dbAdapter = Zend_Registry::get("db");  

                        ...

                        $this->fax_number = new Zend_Form_Element_Text('fax_number');
            $this->fax_number->setLabel('Fax Number')
                ->addFilter('Int')
                ->addFilter('StripTags')
                ->addValidator('Digits');   

                        $this->mobile_number = new Zend_Form_Element_Text('mobile_number');
            $this->mobile_number->setLabel('Mobile Number')
                ->addFilter('Int')
                ->addFilter('StripTags')
                ->addValidator('Digits');

                        ...

                }
         }

My database is currently configured like this with regards to the fax_number and mobile_number fields so that I could test whether or not digits are accepted or not:

+-----------------+-------------+------+-----+---------+----------------+
| Field           | Type        | Null | Key | Default | Extra          |
+-----------------+-------------+------+-----+---------+----------------+
...
| fax_number      | varchar(32) | NO   |     | NULL    |              
| mobile_number   | int(15)     | YES  |     | NULL    |              
...

When I enter anything but digits in either one of the fields in the zend form - no error message is displayed even though I have a validator for digits added to both of the elements.

The text entered into the mobile number text field is replace by a 0 (zero) in the database:

mysql> select fax_number, mobile_number from user_demographics;
+--------------------+---------------+
| fax_number         | mobile_number |
+--------------------+---------------+
| var char no digits |             0 |
+--------------------+---------------+

At the very least the mobile_number field should show an error message saying that only digits can be entered into the text field, right?

And why does it seem that the Digits validator is not recognised?

This basically shows that the Digits validator is almost ignored. The text fields have 0's (zeros) in them when the form is rendered but no error messages are displayed when I enter text into the text field - not even for the mobile_number field that is set to be int in the database.

Please advise?


Solution

  • This is because the filters are processed before the validators.

    So what your code does is the following:

    1. Take the Input
    2. Convert it to int, which will be 0 in case any non-convertable text is contained
    3. Strip the tags
    4. Check if the input contains only digits, which will always be the case because of the int-filter.

    So to work around this, simply remove the int-filter:

    class Application_Form_UserDemographics extends Zend_Form
    {
        public function init()
        {
            $this->form = new Zend_Form;            
            $dbAdapter = Zend_Registry::get("db");  
    
                        ...
    
                        $this->fax_number = new Zend_Form_Element_Text('fax_number');
            $this->fax_number->setLabel('Fax Number')
                ->addFilter('StripTags')
                ->addValidator('Digits');   
    
                        $this->mobile_number = new Zend_Form_Element_Text('mobile_number');
            $this->mobile_number->setLabel('Mobile Number')
                ->addFilter('StripTags')
                ->addValidator('Digits');
    
                        ...
    
                }
         }