0
votes
$inputFilter->add(array(
    'name'     => 'seatingCapacity',
    'required' => TRUE,
    'filters'  => array(
        array('name' => 'Int'),
    ),
));

In my Doctrine Entity, I have a getInputFilter method which I use for form validation. The above is code snippet for one of the input elements. My problem is the required => true is not working even if I submit an empty form.

After researching I found out, that the Int filter converts the empty input to 0 and submits it and that is why the required validator is not working. I just need reasons why it may not be working.

For reference where I searched

Zend Framework 2 - Integer Form Validation

where he suggests to use 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,
                )
            )
        )
    )
));

I want to know why I should use Between and why required is failing.

2
It feels like you've answered your own question. "...and that is why the 'required' validator is not working"srayner
I haven't tested the thing (so it's rather a comment than an answer) but can't you use a (combination of) the to null filter, is int validator and not empty validator?Jurian Sluiman

2 Answers

0
votes

You should only use the Between validator to validate if the given value is Between a min and max value. It has nothing to do with solving your empty value to 0 issue.

You should ask yourself 2 questions:

  • Is 0 a valid value?
  • Do I want to automatically cast null empty string ('') and other empty values to this 0 or not?

If you actually want to prevent that the Int filter sets an empty value to 0, maybe then you should not use this filter at all then?

You can instead add an IsInt validator to check if the given value is an integer. Your required => true setting will work as expected and validation will fail on any other (not integer) input so also on null, empty strings etc.

0
votes

Thank you for your time, energy & effort. Actually I solved the problem.

I had copy-pasted the code, which I should have been careful while doing so.

What I found out is there is no such filter named 'Int' and actually it's 'Digits'

$inputFilter->add(array(
'name'     => 'seatingCapacity',
'required' => TRUE,
'filters'  => array(
    array('name' => 'Int'),
),

));

shows me error because the filter name should have been 'Digits'. Only doing so solved my problem and works as per my requirement.

$inputFilter->add(array(
'name'     => 'seatingCapacity',
'required' => TRUE,
'filters'  => array(
    array('name' => 'Digits'),
),

));

This is the right way to do and I would advice you to be careful while referring code from your sources because it's an illogical mistake I did.

Happy coding