0
votes

When I use InputFilter required is not considered in int filters

$inputFilter->add($factory->createInput(array(
    'name'     => 'id',
    'required' => true,
    'filters'  => array(
        array('name' => 'Int'),
    ),
)));
2

2 Answers

0
votes

You need to use allowEmpty attribute in order to force your InputFilter component to reject empty values.

$inputFilter->add($factory->createInput(array(
    'name'     => 'id',
    'required' => true,
    'allowEmpty' => false, // <--- Look ma
    'filters'  => array(
        array('name' => 'Int'),
    ),
)));
0
votes

Are you sure you want to use the Int InputFilter? Its function is to convert a scalar value to an Integer. Its code is just:

public function filter($value)
{
    return (int) ((string) $value);
}

Which transforms the value to a string. If your value is an empty string, it'll convert it to 0, the reason why afterwards, in the validator's execution process, the required is not working (it checks a 0).

It seems to me, because of the name id of your input, that you just want to force users to enter something that must be a numeric. You can do it this way:

    $inputFilter->add( $factory->createInput( array(
        'name'      => 'id',
        'required'  => true,
        'validators' => array(
            array( 'name' => 'Digits' ),
        )
    ) );