CakePHP validation for dates

12,996

Solution 1

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.

Solution 2

I just came up with a nice easy fix to this issue using Cake 2.x, be sure to place the following above your model class:

App::uses('CakeTime', 'Utility');

Use a validation rule like the following:

public $validate = array(
    'deadline' => array(
        'date' => array(
            'rule' => array('date', 'ymd'),
            'message' => 'You must provide a deadline in YYYY-MM-DD format.',
            'allowEmpty' => true
        ),
        'future' => array(
            'rule' => array('checkFutureDate'),
            'message' => 'The deadline must be not be in the past'
        )
    )
);

Finally the custom validation rule:

/**
 * checkFutureDate
 * Custom Validation Rule: Ensures a selected date is either the
 * present day or in the future.
 *
 * @param array $check Contains the value passed from the view to be validated
 * @return bool False if in the past, True otherwise
 */
public function checkFutureDate($check) {
    $value = array_values($check);
    return CakeTime::fromString($value['0']) >= CakeTime::fromString(date('Y-m-d'));
}

Solution 3

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);
    } 

Solution 4

Add the below function in your appmodel

  /**
   * date range validation
   * @param array $check Contains the value passed from the view to be validated
   * @param array $range Contatins an array with two parameters(optional) min and max
   * @return bool False if in the past, True otherwise
   */
    public function dateRange($check, $range) {

        $strtotime_of_check = strtotime(reset($check));
        if($range['min']){
            $strtotime_of_min = strtotime($range['min']);
            if($strtotime_of_min > $strtotime_of_check) {
                return false;
            }
        }

        if($range['max']){
            $strtotime_of_max = strtotime($range['max']);
            if($strtotime_of_max < $strtotime_of_check) {
                return false;
            }
        }
        return true;
    }

Usage

    'date' => array(
        'not in future' => array(
            'rule' =>array('dateRange', array('max'=>'today')),
        )
    ),
Share:
12,996
nickf
Author by

nickf

Javascript nerd. Senior Software Engineer at Google. Ex-SoundClouder.

Updated on June 04, 2022

Comments

  • nickf
    nickf almost 2 years

    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?