utilityValidateDate

Validates that a variable matches a date pattern and that it is a valid date. The pattern is a regular expression. This validator only handles the date component of a date/time. See also the utilityValidateTime and utilityValidateDateTime validators. If pattern is not specified it defaults to CCYY-MM-DD.

To validate that a date is an actual date in the Gregorian calendar, set the dateComponentOrder option. This should reflect the order that the regex is in using Y - year, M - month, D - day e.g. Y/M/D. The separator must also be specified.

Please note: if using the dateComponentOrder your must set the PATTERN to reflect your order, otherwise the validator will default to CCYY-MM-DD validation which will not match your order. Similarly you must specify the separator required to split the date.

// validate a date using defaults
$oValidator = new utilityValidateDate(
    array(
        // don't specify anything
    )
);
if ( !$oValidator->isValid($date) ) {
    print_r($oValidator->getMessages());
}

// validate a date as M/D/Y, remember to set the PATTERN
$oValidator = new utilityValidateDate(
    array(
        utilityValidateDate::PATTERN => '/^\d{2}\/\d{2}\/\d{4}$/',
        utilityValidateDate::DATE_COMPONENT_ORDER => 'M/D/Y',
        utilityValidateDate::DATE_SEPARATOR => '/',
    )
);
if ( !$oValidator->isValid($date) ) {
    print_r($oValidator->getMessages());
}

Methods


Inherited Methods

<  1  >