Development

Changeset 6875

You must first sign up to be able to contribute.

Changeset 6875

Show
Ignore:
Timestamp:
01/01/08 11:01:15 (2 years ago)
Author:
fabien
Message:

fixed special cases for sfValidatorDate

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/validator/sfValidatorDate.class.php

    r5882 r6875  
    7878    } 
    7979 
    80     return date($this->getOption('with_time') ? $this->getOption('datetime_output') : $this->getOption('date_output'), $clean); 
     80    return $clean === $this->getEmptyValue() ? $clean : date($this->getOption('with_time') ? $this->getOption('datetime_output') : $this->getOption('date_output'), $clean); 
    8181  } 
    8282 
     
    9292  protected function convertDateArrayToTimestamp($value) 
    9393  { 
     94    // all elements must be empty or a number 
     95    foreach (array('year', 'month', 'day', 'hour', 'minute', 'second') as $key) 
     96    { 
     97      if (isset($value[$key]) && !preg_match('#^\d+$#', $value[$key]) && !empty($value[$key])) 
     98      { 
     99        throw new sfValidatorError($this, 'invalid', array('value' => $value)); 
     100      } 
     101    } 
     102 
     103    // if one date value is empty, all others must be empty too 
     104    $empties = 
     105      (!isset($value['year']) || !$value['year'] ? 1 : 0) + 
     106      (!isset($value['month']) || !$value['month'] ? 1 : 0) + 
     107      (!isset($value['day']) || !$value['day'] ? 1 : 0) 
     108    ; 
     109    if ($empties > 0 && $empties < 3) 
     110    { 
     111      throw new sfValidatorError($this, 'invalid', array('value' => $value)); 
     112    } 
     113    else if (3 == $empties) 
     114    { 
     115      return $this->getEmptyValue(); 
     116    } 
     117 
    94118    if ($this->getOption('with_time')) 
    95119    { 
    96       $clean = mktime(isset($value['hour']) ? $value['hour'] : 0, isset($value['minute']) ? $value['minute'] : 0, isset($value['second']) ? $value['second'] : 0, $value['month'], $value['day'], $value['year']); 
     120      // if one time value is empty, all others must be empty too 
     121      // but second can be empty 
     122      $empties = 
     123        (!isset($value['hour']) || !$value['hour'] ? 1 : 0) + 
     124        (!isset($value['minute']) || !$value['minute'] ? 1 : 0) + 
     125        (!isset($value['second']) || !$value['second'] ? 1 : 0) 
     126      ; 
     127      if ($empties > 0 && $empties < 3) 
     128      { 
     129        if (1 == $empties && !isset($value['second']) || empty($value['second'])) 
     130        { 
     131          // OK 
     132        } 
     133        else 
     134        { 
     135          throw new sfValidatorError($this, 'invalid', array('value' => $value)); 
     136        } 
     137      } 
     138 
     139      // if minute is not empty, hour cannot be empty 
     140      $clean = mktime( 
     141        isset($value['hour']) ? intval($value['hour']) : 0, 
     142        isset($value['minute']) ? intval($value['minute']) : 0, 
     143        isset($value['second']) ? intval($value['second']) : 0, 
     144        intval($value['month']), 
     145        intval($value['day']), 
     146        intval($value['year']) 
     147      ); 
    97148    } 
    98149    else 
    99150    { 
    100       $clean = mktime(0, 0, 0, $value['month'], $value['day'], $value['year']); 
     151      $clean = mktime(0, 0, 0, intval($value['month']), intval($value['day']), intval($value['year'])); 
    101152    } 
    102153 
  • branches/1.1/test/unit/validator/sfValidatorDateTest.php

    r5816 r6875  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(18, new lime_output_color()); 
     13$t = new lime_test(26, new lime_output_color()); 
    1414 
    1515$v = new sfValidatorDate(); 
     
    4343$t->diag('validate date array'); 
    4444$t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15)), '2005-10-15', '->clean() accepts an array as an input'); 
    45  
     45$t->is($v->clean(array('year' => '2005', 'month' => '10', 'day' => '15')), '2005-10-15', '->clean() accepts an array as an input'); 
     46$t->is($v->clean(array('year' => '', 'month' => '', 'day' => '')), null, '->clean() accepts an array as an input'); 
     47try 
     48
     49  $v->clean(array('year' => '', 'month' => 1, 'day' => 15)); 
     50  $t->fail('->clean() throws a sfValidatorError if the date is not valid'); 
     51
     52catch (sfValidatorError $e) 
     53
     54  $t->pass('->clean() throws a sfValidatorError if the date is not valid'); 
     55
    4656try 
    4757{ 
     
    8898$v->setOption('with_time', true); 
    8999$t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => 12, 'minute' => 10, 'second' => 15)), '2005-10-15 12:10:15', '->clean() accepts an array as an input'); 
     100$t->is($v->clean(array('year' => '2005', 'month' => '10', 'day' => '15', 'hour' => '12', 'minute' => '10', 'second' => '15')), '2005-10-15 12:10:15', '->clean() accepts an array as an input'); 
     101$t->is($v->clean(array('year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '')), null, '->clean() accepts an array as an input'); 
     102$t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => 12, 'minute' => 10, 'second' => '')), '2005-10-15 12:10:00', '->clean() accepts an array as an input'); 
     103$t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => 12, 'minute' => 10)), '2005-10-15 12:10:00', '->clean() accepts an array as an input'); 
     104try 
     105{ 
     106  $v->clean(array('year' => 2005, 'month' => 1, 'day' => 15, 'hour' => 12, 'minute' => '', 'second' => 12)); 
     107  $t->fail('->clean() throws a sfValidatorError if the time is not valid'); 
     108} 
     109catch (sfValidatorError $e) 
     110{ 
     111  $t->pass('->clean() throws a sfValidatorError if the time is not valid'); 
     112} 
    90113$t->is($v->clean('18 october 2005 12:30'), '2005-10-18 12:30:00', '->clean() can accept date time with the with_time option'); 
    91114$t->is($v->clean(time()), date('Y-m-d H:i:s', time()), '->clean() can accept date time with the with_time option'); 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.