开发者

Validating date format with Zend_validator

How can I validate the date format (dd-mm-yyyy) using zend_v开发者_Go百科alidate?


You simply use the Date validator (Zend_Validate_Date).

Eg

$validator = new Zend_Validate_Date(array(
    'format' => 'dd-mm-yyyy',
    'locale' => $yourLocale
);


It is not possible at the moment to validate against an exact date format in zendframework2 (see ZF2 Issue #4763) but you can add an extra regex validator (see example here) or write a custom validator to handle this (see zf2 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}%',
));


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜