Changeset 1484

Show
Ignore:
Timestamp:
01/10/07 23:13:42 (2 years ago)
Author:
dominik
Message:

add minimum and maximum constraints for the date validator (allows referencing already validated dates too)
refs #359

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/0.11/src/validator/AgaviDateValidator.class.php

    r1482 r1484  
    5353 *   'arguments_format' A string which will be used as the format string for  
    5454 *                 sprintf. 
     55 *   'min'         Either an string or an array. When its a string the the  
     56 *                 its assumed to be in the format 'yyyy-MM-dd[ HH:mm:ss[.S]]'. 
     57 *                 When its an array it will take the minimum value from a  
     58 *                 request field. These indizes apply: 
     59 *     'format'      A custom format string which should be used when the field  
     60 *                   is an string. 
     61 *     'field'       The name of the field to use as minimum value (could be a  
     62 *                   previous exported calendar object). Do NOT use unvalidated  
     63 *                   fields here. Lax parsing will be used. 
     64 *                 This value is inclusive. 
     65 *   'max'         The same as min except that the max is exclusive. 
    5566 * 
    5667 * @package    agavi 
     
    101112        $cal->getTime(); 
    102113      } catch(Exception $e) { 
    103         $this->throwError(); 
     114        $this->throwError('check'); 
    104115        return false; 
    105116      } 
     
    143154 
    144155      if(!$matchedFormat) { 
    145         $this->throwError(); 
     156        $this->throwError('format'); 
    146157        return false; 
    147158      } 
     
    175186    } 
    176187 
     188    $defaultParseFormat = new AgaviDateFormat('yyyy-MM-dd HH:mm:ss.S'); 
     189 
     190    if($this->hasParameter('min')) { 
     191      $min = $this->getMinOrMaxValue('min', $defaultParseFormat, $locale); 
     192 
     193      $isAfterEqual = $cal->after($min) || $cal->equals($min); 
     194      if(!$isAfterEqual) { 
     195        $this->throwError('min'); 
     196        return false; 
     197      } 
     198    } 
     199 
     200    if($this->hasParameter('max')) { 
     201      $max = $this->getMinOrMaxValue('max', $defaultParseFormat, $locale); 
     202 
     203      $isBefore = $cal->before($max); 
     204      if(!$isBefore) { 
     205        $this->throwError('max'); 
     206        return false; 
     207      } 
     208    } 
     209 
    177210    if($this->hasParameter('export')) { 
    178211      $export = $this->getParameter('export'); 
     
    190223    return true; 
    191224  } 
     225 
     226  /** 
     227   * Returns the calendar object for a max or min definition. 
     228   * 
     229   * @param      string 'min' or 'max' 
     230   * @param      AgaviDateFormat The default format when parsing strings. 
     231   * @param      AgaviLocale The locale to use. 
     232   * 
     233   * @return     AgaviCalendar The calendar object storing the date. 
     234   *  
     235   * @author     Dominik del Bondio <ddb@bitxtender.com> 
     236   * @since      0.11.0 
     237   */ 
     238  protected function getMinOrMaxValue($minMax, $defaultParseFormat, $locale) 
     239  { 
     240    $format = $defaultParseFormat; 
     241 
     242    $minMax = $this->getParameter($minMax); 
     243    if(is_array($minMax)) { 
     244      $minMaxValue = $this->validationParameters->getParameter($minMax['field']); 
     245      if(!$minMaxValue instanceof AgaviCalendar) { 
     246        if(isset($minMax['format'])) { 
     247          $format = new AgaviDateFormat($minMax['format']); 
     248        } 
     249        $result = $format->parse($minMaxValue, $locale, false); 
     250      } else { 
     251        $result = $minMaxValue; 
     252      } 
     253    } else { 
     254      $result = $format->parse($minMax, $locale, false); 
     255    } 
     256 
     257    return $result; 
     258  } 
     259 
    192260} 
    193261