Zuber Surya
Zuber Surya

Reputation: 869

Zend_Validate_Date returns true on 2011-02-31

What should i do ?

$edit_end_date  = '2011-02-31';
$validator_date = new Zend_Validate_Date(array('format' => 'yyyy-mm-dd'));  
$isval          =   $validator_date->isValid($edit_end_date);  
if((!$isval) || empty($edit_end_date))
    {
    echo "Please Enter Valid End Date. !";
    }else{
echo "Entered Is Valid End Date. !";
}

how come it returns true date ?

Upvotes: 1

Views: 1029

Answers (2)

BartekR
BartekR

Reputation: 3957

There are bugs in data validation (ZF-7583 at issue tracker). Look at Zend_Validate_Date just doesn't work properly

You can use regex validation like in answer to linked question, but it will only check for syntax, not if date exists for real. For this you can use checkdate() - as Mike Purcell suggested - combined with Zend_Validate_Callback:

$validator1 = new Zend_Validate_Regex(
    array('pattern' => '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/')
);
$validator1->setMessage(
    "Date does not match the format 'yyyy-mm-dd'",
    Zend_Validate_Regex::NOT_MATCH
);

$validator2 = new Zend_Validate_Callback(function($value)) {
    // using checkdate() or other function
});

Upvotes: 0

Mike Purcell
Mike Purcell

Reputation: 19989

According to the Zend API Docs, it appears that Zend_Validate_Date will only validate whether the argument passed to it, is a valid date construct (also considers locale), it will not validate if the date actually exists.

Zend_Validate_Date allows you to validate if a given value contains a date. This validator validates also localized input.

-- Edit --

Looks like you can use PHP's built in checkdate() function to determine if a date is valid or not.

Upvotes: 1

Related Questions