Zend Framework 2 - Integer Form Validation

I have the following problem. I wrote (based on a tutorial) form validation. Text fields work fine, but an integer field behaves oddly.

This is my validator:

$inputFilter->add($factory->createInput(array( 'name' => 'zip', 'required' => false, 'filters' => array( array('name' => 'Int'), ), ))); 

It is inside my Entity.php, like other filters. It is strange that this element even accepts a string, but ignores the required when I set it to true . I tried replacing Int with Digits , which then makes the form accept required , but accept strings anyway.

Any ideas? Thanks!

+4
source share
3 answers

Try using Between :

 $inputFilter->add($factory->createInput(array( 'name' => 'zip', 'required' => true, 'filters' => array( array('name' => 'Int'), ), 'validators' => array( array( 'name' => 'Between', 'options' => array( 'min' => 1, 'max' => 1000, ), ), ), ))); 
+9
source

this is an old topic, but I must mention that Filters do not cause validation errors, they work in the background and do their work silently.

For example, an Int filter will remove an odd integer from the input, so when you do $form->getData() , the Int filter field will only have integer values ​​and 0 if it is empty.

+3
source
  array( 'name' => 'not_empty', ), array( 'name' => 'Digits', ), array( 'name' => 'Between', 'options' => array( 'min' => 0, 'max' => 1, ), ), 
+1
source

All Articles