-- Steve Rayner <[email protected]> wrote
(on Monday, 17 September 2012, 09:54 PM +0100):
> Can anyone point me to (or give me) an example of an input filter for
> an integer? My requirements are that we ensure the input is an
> integer, it is required and it must fall between and min and max
> values (inclusive). I can probably just about figure this out, but
> should i be doing things like strip tags? I'm guessing not because if
> we are ensuring it is a number the tags can't be present can they?
> 
> I'm guessing something like this;
> 
> $inputFilter->add($factory->createInput(array(
>     'name' => 'somevalue',
>     'required' => true,
>     'validators' => array(
>         array(
>             'name' => 'Int',
>             'options' => array(
>                 'min' => 1,
>                 'max' => 50,
>             ),
>         ),
>     ),
> )));

Try this (untested):

    $inputFilter->add($factory->createInput(array(
        'name' => 'somevalue',
        'required' => true,
        'validators' => array(
            array(
                'name'                   => 'Digits',
                'break_chain_on_failure' => true,
            ),
            array(
                'name' => 'Between',
                'options' => array(
                    'min' => 1,
                    'max' => 50,
                ),
            ),
        ),
    )));

This sets up two validators. The first checks to ensure we have only
Digits, and will halt the chain if it fails. If we do have only digits,
we then hit the "Between" validator, and the value is tested to see if
it lies between the min and max provided.

-- 
Matthew Weier O'Phinney
Project Lead            | [email protected]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to