views:

605

answers:

2

In CakePHP, is there a built-in way of validating a date to be within a certain range? For example, check that a certain date is in the future?

If the only option is to write my own custom validation function, since it'll be pretty generic and useful to all my controllers, which is the best file to put it in?

+1  A: 

AFAIK there's no built-in validation for date ranges. The closest to it would be range, but only if you expect all your dates to be UNIX timestamps.

You can put your own validation method in the AppModel and it'll be available in all models.

deceze
+3  A: 

A quick Google search for "CakePHP future date validation" gives you this page: http://bakery.cakephp.org/articles/view/more-improved-advanced-validation (do a page search for "future")

This code (from the link) should do what you need

function validateFutureDate($fieldName, $params)
    {       
        if ($result = $this->validateDate($fieldName, $params))
        {
            return $result;
        }
        $date = strtotime($this->data[$this->name][$fieldName]);        
        return $this->_evaluate($date > time(), "is not set in a future date", $fieldName, $params);
    }
Arms
What did I miss? ;)
Arms
Proper Markdown code formatting. :)
deceze