rukabillaan
rukabillaan

Reputation: 241

Validating date format with Zend_validator

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

Upvotes: 1

Views: 4299

Answers (3)

dixromos98
dixromos98

Reputation: 754

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

Upvotes: 0

Ludwig
Ludwig

Reputation: 3729

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}%',
));

Upvotes: 0

Phil
Phil

Reputation: 164776

You simply use the Date validator (Zend_Validate_Date).

Eg

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

Upvotes: 4

Related Questions