5

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
  • Is it a field of type number or of type text? Considering a text-input will result in a string and only a number would send the integer, this is where you may have your thought-problem Commented Dec 18, 2012 at 14:02

3 Answers 3

10

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,
                  ),
              ),
            ),
        )));
Sign up to request clarification or add additional context in comments.

2 Comments

ok, it works. but could you explain why adding an input filter doesn't work?
ok, i found the solution! the Int filter converts non-ints to zero so the database won't fail.
3

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.