0
votes

I'd really appreciate some help with this its been driving me nuts. I'm trying to validate numbers submitted by a zend form either in the form of integers or decimals (2 decimal places) allowing for 0 or 0.00 but not empty inputs.

$form->element->addValidator ('regex', false, array(
  'pattern'=>'/^\d+(\d{1,5})?(\.\d{1,2})?$/', 
  'messages'=>array(
   'regexInvalid'=>'required',
   'regexNotMatch'=>'number required')
  )
);

For some reason a float like 100.00 generates the following validation error message: "'100.00' contains characters which are not digits; but only digits are allowed". The regexNotMatch message or regexInvalid message aren't generated which is what I'd expect.

This validation error message seems to be generated by the digits validator which I'm not calling is there something I can do to stop it kicking in?

Also empty inputs aren't generating any validation error messages.

If I try a string like 'rt67' my regexNotMatch message is displayed correctly.

I'm using zend components without the mvc if this makes any difference.

3

3 Answers

1
votes

I was calling the digit validator so there wasn't actually a problem with it. I've worked around the acceptance of empty inputs being accepted by simply using setRequired. Its amazing what a nights sleep can do! I would however be grateful if anyone knows why the regex accepted empty input.

0
votes

Why not use Zend_Validate_Float and/or Zend_Validate_Int?

0
votes

Add

$form->element->setRequired();

to ban empty inputs.

You code works, check other attached validators. I used it like

    $this->addElement('text','price', array(
        'label'      => 'Price:',
        'filters'    => array('StringTrim'),
        'required'   => true,
        'requiredSuffix'=>'*',
        'size'       => 10,
        'maxlength'     => 10,
        'validators' => array(
            array('validator' => 'Regex', 'options' => array(
                'pattern'=>'/^\d+(\d{1,5})?(\.\d{1,2})?$/', 
                  'messages'=>array(
                   'regexInvalid'=>'required',
                   'regexNotMatch'=>'number in money format (x.xx) is required')
            ))
        )
    ));