OEIC

Date validation in Zend Framework

The Zend Framework has a powerful set of validation features, ranging from predefined data type checking (like email addresses and DNS hostnames) to classes for chaining together validations and input filters to create custom validation stacks. Since Rule #1 of web application security is Don’t trust ANYTHING that comes from the browser, I use the validation tool set a lot. One task that I often comes across is date validation: checking if the specified date is in the correct format, if it falls in a valid range of dates or if the date is even a date at all. Many applications try to simplify date validation by providing a separate select box for month, date and year.

date_selection.gif

I think that providing a standard text box and making the expected date format clear is a more usable solution. Providing a Javascript/CSS date picker is another nicety. I don’t have any metrics comparing text input to select box input, so I’d be happy to learn of such a comparison. Regardless, the user input must be validated rigorously at the server side anyway, as it’s quite easy to forge form input.

The Zend_Validate_Date class that comes with the Zend Framework is lacking in the functionality I listed above. It doesn’t allow a developer to specify the desired input format, instead only allowing dates in YYYY-MM-DD format, and it doesn’t provide a way to determine if a date is within a given range. I needed such features for a project I was working on, and I was lucky enough to find a Zend_Validate_Date replacement class by Ralf Eggert that allowed custom date formats.

However, it used an unfamiliar syntax for specifying the date format, didn’t support textual months, and it didn’t provide for date ranges either, so I decided I’d have to build out Ralf’s class. If you have similar needs, feel free to use and distribute it. It’s very similar to the stock Zend_Validate_Date, except with a few optional parameters available to the constructor.

Download it: Zend_Validate_Date

new Zend_Validate_Date([string $format] [,string $startDate] [,string $endDate]);

$format is specified with PHP’s standard date format codes. Allowable codes are d, j, F, m, M, n, y and Y.

$startDate is a date delimiting the beginning of a valid date range. Any date before $startDate will be invalid.

$endDate is a date delimiting the end of a valid date range. Any date after $endDate will be invalid.

Notes: the class provides the constant TODAY to use the current date for either $startDate or $endDate parameters. Also, due to limitations in PHP’s date handling prior to PHP 5.2, this class will not handle date ranges prior to 1970 in the expected fashion. If you need to validate dates are in a date window prior to 1970, look into PHP’s dateTime object.

Instantiating the validator with default m/d/Y date format
$v = new Zend_Validate_Date();

Instantiating the validator with a custom date format
$v = new Zend_Validate_Date('d-F-Y');

Instantiating the validator with a custom date format and a start date
$v = new Zend_Validate_Date('d-F-Y', '06-September-2007');

Instantiating the validator with a custom date format and an end date
$v = new Zend_Validate_Date('d-F-Y', null, '06-September-2007');

Instantiating the validator with a custom date format and a start date of whatever day it is
$v = new Zend_Validate_Date('d-F-Y', Zend_Validate_Date::TODAY);

Instantiating the validator with a custom date format and a start date and end date
$v = new Zend_Validate_Date('M-d-Y', 'Sep-01-2007', 'Sep-30-2007');

Download it: Zend_Validate_Date

No comments     Jump to comment form | comments rss | trackback uri

Say what?

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>





About this entry

Categories