2

Hey all I'm trying to validate a 'time' field using Zend Framework.

The docs seem to be pointing out that the following:

$tstarttime =  $form->createElement('text','t_start_time');
$tstarttime->setRequired(true)
    ->setLabel('Start Time')
    ->addValidator(new Zend_Validate_Date('H:i'));

I've tried this but no luck. Any ideas or alternative ways to validate a time in the format of HH:MM (H:i in php)

Thanks all!

Charles
  • 50,943
  • 13
  • 104
  • 142
remedix
  • 492
  • 6
  • 13

3 Answers3

3

Try this

 $tstarttime =  $form->createElement('text','t_start_time');
 $tstarttime->setRequired(true)
      ->setLabel('Start Time')
      ->addValidator(new Zend_Validate_Date(array("format" => 'H:i')));
Jake N
  • 10,535
  • 11
  • 66
  • 112
1

55:55 is not a valid time if it is supposed to be in hours:minutes and not minutes:seconds. there are only 24 hours in one day. apart from that, your example should work.

cycore
  • 11
  • 1
0

I think you can validate with Regex (ZF2) .

public static function isValidTime($time){
    $validator = new \Zend\Validator\Regex ( '/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/' );
    return $validator->isValid ( $time );
}

Regexp Expression: http://www.mkyong.com/regular-expressions/how-to-validate-time-in-24-hours-format-with-regular-expression/

calraiden
  • 1,686
  • 1
  • 27
  • 37