1

I am using zend form. I want to validate a field and want to allow only flat and integer values in the field. It means user can either enter any floating value like 2.0 or 3.56, etc OR 4 or 7, etc. But I dont want to receive alpha numeric or alphabet input.

I have used digit validator but it only allows digits not floating number.

Can any body tell me how to put both validations together?

My code is as follows

$parent_affiliate_commission = new Zend_Form_Element_Text('parent_affiliate_commission');
$parent_affiliate_commission->setRequired(true)
            ->addFilter('StringTrim')
            ->addFilter('StripTags')
            ->addValidator('Digits')
            ->setAttrib('class', 'small')
            ->addValidator('StringLength', false, array(2, 100))
            ->setDecorators(array('ViewHelper', 'errors'))

2 Answers 2

2

It would be really easy to create and use a custom validator. You could validate simply by using PHP is_int and is_float functions and still do in the 'Zend way'.

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

Comments

0

By using the callback method I have validated the value in numeric.Because it will not allow the characters.

$inputFilter->add(
                $factory->createInput(
                        array(
                                'name' => 'proximity',
                                'required' => true,
                                'validators' => array(
                                        array(
                                                'name' => 'Callback',
                                                'options' => array(
                                                        'messages' => array(
                                                                \Zend\Validator\Callback::INVALID_VALUE => 'The proximity value should be numbers'
                                                        ),
                                                        'callback' => function  (
                                                                $value, 
                                                                $context = array())
                                                        {
                                                            $isValid = is_numeric(
                                                                    $value);
                                                            return $isValid;
                                                        }
                                                )
                                        )
                                )
                        )));

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.