1

How can I validate the date format (dd-mm-yyyy) using zend_validate?

Peladao
  • 4,036
  • 1
  • 23
  • 43
rukabillaan
  • 241
  • 4
  • 13

3 Answers3

4

You simply use the Date validator (Zend_Validate_Date).

Eg

$validator = new Zend_Validate_Date(array(
    'format' => 'dd-mm-yyyy',
    'locale' => $yourLocale
);
Phil
  • 157,677
  • 23
  • 242
  • 245
  • @Ludwig Considering this answer was posted in 2011 (well before ZF2), I don't find that surprising at all. – Phil Mar 27 '14 at 01:09
  • But it is still worth mentioning it to save other ZF2 users the time to test ist – Ludwig Mar 27 '14 at 11:51
  • @Ludwig This question is tagged [tag:zend-framework], not [tag:zend-framework2]. Combine that with the complete lack of namespaces (`Zend_Validate_Date` vs `Zend\Validate\Date`) and I really don't think it's too much to assume a ZF2 dev is going to bother with this answer – Phil Mar 27 '14 at 22:51
0

It is not possible at the moment to validate against an exact date format in (see ZF2 Issue #4763) but you can add an extra regex validator (see example here) or write a custom validator to handle this (see Issue).

use Zend\Validator\Date;
use Zend\Validator\Regex;

$validator = new Date(array(
    'format' => 'd-m-Y',
));
$validator2 = new Regex(array(
    'pattern' => '%[0-9]{2}-[0-9]{2}-[0-9]{4}%',
));
Ludwig
  • 3,580
  • 2
  • 20
  • 24
0

This is how i did this,

$DateFormat = new \Zend\Validator\Date(array('format' => 'Y-m-d'));

    if(!($DateFormat->isValid($somedate))){
    //if not valid do something 

}else{

//do something else
}

i forgot to mention that this is for Zend 2.0

dixromos98
  • 756
  • 5
  • 18