Zend Framework 2 - Integer Form Validation

时光总嘲笑我的痴心妄想 提交于 2019-12-04 12:26:20

问题


I've got the following problem. I wrote (based on the tutorial) a form validation. The text fields work just fine but the integer field behave odd.

This is my validator:

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

It lies within my Entity.php like the other filters. The odd thing is that this one accepts not even a string but ignores the required when I set it to true. I tried to replace Int with Digits which then causes the form to accept required but still accepts strings.

Any ideas? Thanks!


回答1:


Try using the Between validator:

$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,
                  ),
              ),
            ),
        )));



回答2:


this is a old topic but i should mention that Filters don't cause validation errors, they work in background and do their jobs silently .

for example Int filter will remove any non-integer from the input , so when you do $form->getData() the field with the Int filter will only have integer values and 0 if its empty.




回答3:


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


来源:https://stackoverflow.com/questions/13931080/zend-framework-2-integer-form-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!