【问题标题】:CakePHP validation for datesCakePHP 日期验证
【发布时间】:2009-09-10 04:22:14
【问题描述】:

在 CakePHP 中,是否有一种内置方法可以验证日期是否在特定范围内?例如,检查某个日期是否在将来?

如果唯一的选择是编写我自己的自定义验证函数,因为它对我的所有控制器都非常通用且有用,那么将其放入哪个文件最好?

【问题讨论】:

    标签: php validation cakephp


    【解决方案1】:

    我刚刚使用 Cake 2.x 想出了一个很好的简单解决方案,请务必将以下内容放在您的模型类上方:

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

    使用如下验证规则:

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

    最后是自定义验证规则:

    /**
     * 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'));
    }
    

    【讨论】:

    • 如果您想更加灵活并检查另一个字段(例如,如果您发布了两个日期并希望一个在另一个之后) - 从这里尝试 validateDate() 等:github.com/dereuromark/tools/blob/master/Model/…
    • 很好的建议,我将来可能不得不使用它,因为需求总是在变化,看起来很棒。
    • 你也可以使用 CakeTime::isFuture 来简化一些事情。这是在 v2.4 中添加的
    【解决方案2】:

    AFAIK 没有针对日期范围的内置验证。最接近它的是range,但前提是您希望所有日期都是 UNIX 时间戳。

    您可以将自己的验证方法放在AppModel 中,它将适用于所有型号。

    【讨论】:

      【解决方案3】:

      在 Google 上快速搜索“CakePHP 未来日期验证”会为您提供以下页面:http://bakery.cakephp.org/articles/view/more-improved-advanced-validation(对“未来”进行页面搜索)

      此代码(来自链接)应该可以满足您的需要

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

      【讨论】:

        【解决方案4】:

        在您的 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;
            }
        

        用法

            'date' => array(
                'not in future' => array(
                    'rule' =>array('dateRange', array('max'=>'today')),
                )
            ),
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-08
          • 2014-09-04
          • 1970-01-01
          相关资源
          最近更新 更多