2

I use Zend\InputFilter\InputFilter class for form validation. point field should accept only integers between 1 and 5. But it doesn't work properly it accepts string starting with integers between 1 and 5. For example 1kjhkjh, 2tgfjhgfjhf, 4jhkljg... What is wrong in my code?

$inputFilter->add (
            $inputFilter->getFactory()->createInput (
                array (
                    'name' => 'point',
                    'required' => true,
                    'validators' => array ( 
                        array(
                            'name' => 'Digits'),    
                        array (
                            'name' => 'Between',
                            'options' => array (
                                'min' => 1,
                                'max' => 5,
                                'messages' => array('notBetween' => 'Point must be between %min% and %max%')
                            )
                        )
                    )
                )
            )
        );
1
  • A workaround would be to add a string length of 1. Commented Jul 23, 2013 at 11:55

2 Answers 2

3

Use the the second parameter of zend validator to break the validators chain and return an error, breakChainOnFailure (documentation) tells the element to stop validating if this error is triggered, so in your case if it is not a digit the user gets an error, when the user has fixed the error the second validator will get triggered too:

$inputFilter->add (
            $inputFilter->getFactory()->createInput (
                array (
                    'name' => 'point',
                    'required' => true,
                    'validators' => array ( 
                        array(
                            'name' => 'Digits', 'breakChainOnFailure' => true),    
                        array (
                            'name' => 'Between',
                            'options' => array (
                                'min' => 1,
                                'max' => 5,
                                'messages' => array('notBetween' => 'Point must be between %min% and %max%')
                            )
                        )
                    )
                )
            )
        );

Another option would be to replace Zend_Validate_Digits with Zend_Validate_Int (docmentation) depending of what error message you prefer to give to the user if he enters non valid data. Of course as suggested in the comments you could also use the Zend_Filter_Int if what you want is to handle any non valid data by fixing it yourself and do not give the user feedback about what he did wrong.

Sign up to request clarification or add additional context in comments.

Comments

0
'breakChainOnFailure': true

should be

'breakChainOnFailure'=> true

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.