Form input filter

hi there, here i have the form model’s element definition like this:

class ContactForm extends Form
{
public function __construct($name = null)
{
    parent::__construct('contactform');
    $this->setAttribute('method','POST');
    $this->addElements();
    $this->addInputFilter();
}
private function addElements()
{
    $this->add([
        'type'=>'text',
        'name'=>'fname',
        'attributes'=>
            [
                'placeholder'=>'firstname',
                'class'=>'form-control',
            ]

    ]);
 }
   private function addInputFilter()
{
    $inputfilter = new InputFilter();
    $this->setInputFilter($inputfilter);
    $inputfilter->add([
        'name'=>'fname',
        'required'=>true,
        'filters'=>
            [
                ['name'=>'StripTags'],
                ['name'=>'StringTrim'],
            ],
        'validators'=>
            [
                [
                    'name'=>'StringLength',
                    'options'=>
                        [
                            'encoding'=>'UTF-8',
                            'min'=>1,
                            'max'=>20,
                        ],

                ],
            ],
    ]);
     }
    }

i got all other things prepared like Controller and View.
the problem is when I try submit this form with bunch of numbers instead of strings and in my surprise there is no filter or validation error in return and the form is submitted despite using
'type' => 'text'
inside my field definition.
what am I to do now?

The type option only sets the type for the input element in HTML and use the related zend-form element. This has nothing to do with a data type or which data type is accepted.
If you use the form element number, then you will get also a string. You must filter the value. (See: Zend\Filter\ToInt)
This is not a special behaviour of the ZF, this is the normal behaviour in HTTP und PHP.

1 Like