2009-09-10 5 views
1

CakePHP에서 특정 범위 내에 날짜를 확인하는 기본 제공 방법이 있습니까? 예를 들어 특정 날짜가 미래에 있는지 확인하십시오.날짜에 대한 CakePHP 유효성 확인

유일한 옵션은 내 일반적인 사용자 정의 유효성 검사 기능을 작성하는 것이므로 모든 컨트롤러에 매우 일반적이며 유용 할 것입니다.

답변

1

AFAIK 날짜 범위에 대한 기본 제공 유효성 검사가 없습니다. 가장 가까운 날짜는 range이지만, 모든 날짜가 UNIX 시간 소인이라고 예상하는 경우에만 해당됩니다.

AppModel에 고유 한 유효성 검사 방법을 넣을 수 있으며 모든 모델에서 사용할 수 있습니다.

3

"CakePHP의 미래의 날짜 확인"에 대한 빠른 Google 검색은이 페이지를 제공합니다 http://bakery.cakephp.org/articles/view/more-improved-advanced-validation는 ("미래"에 대한 페이지 검색을 수행)

이 코드 (링크)에서 당신이 필요로 어떻게해야을

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

가 validat를 사용

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

내가 무엇을 놓쳤는가? ;) –

+0

적절한 Markdown 코드 형식 지정. :) – deceze

4

난 그냥 당신의 모델 클래스 위에 다음을 배치, 반드시 케이크 2.X를 사용하여이 문제에 좋은 쉽게 수정 함께했다

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')); 
} 
+1

더 유연하고 다른 필드 (예 : 두 날짜를 게시하고 다른 날짜를 게시하려는 경우)를 확인하려면 여기에서 validateDate() 등을 시도하십시오. https://github.com/dereuromark /tools/blob/master/Model/MyModel.php#L1191 – mark

+0

훌륭한 제안입니다. 요구 사항이 항상 바뀌고 있기 때문에 미래에이를 사용해야 할 수도 있습니다. – HelloSpeakman

+2

CakeTime :: isFuture를 사용하여 약간의 작업을 단순화 할 수도 있습니다. 이것은 v2.4에서 추가되었습니다. –

1

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')), 
     ) 
    ), 
0 다음과 같은 이온 규칙