Changeset 6875
- Timestamp:
- 01/01/08 11:01:15 (10 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/validator/sfValidatorDate.class.php
r5882 r6875 78 78 } 79 79 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); 81 81 } 82 82 … … 92 92 protected function convertDateArrayToTimestamp($value) 93 93 { 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 94 118 if ($this->getOption('with_time')) 95 119 { 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 ); 97 148 } 98 149 else 99 150 { 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'])); 101 152 } 102 153 branches/1.1/test/unit/validator/sfValidatorDateTest.php
r5816 r6875 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test( 18, new lime_output_color());13 $t = new lime_test(26, new lime_output_color()); 14 14 15 15 $v = new sfValidatorDate(); … … 43 43 $t->diag('validate date array'); 44 44 $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'); 47 try 48 { 49 $v->clean(array('year' => '', 'month' => 1, 'day' => 15)); 50 $t->fail('->clean() throws a sfValidatorError if the date is not valid'); 51 } 52 catch (sfValidatorError $e) 53 { 54 $t->pass('->clean() throws a sfValidatorError if the date is not valid'); 55 } 46 56 try 47 57 { … … 88 98 $v->setOption('with_time', true); 89 99 $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'); 104 try 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 } 109 catch (sfValidatorError $e) 110 { 111 $t->pass('->clean() throws a sfValidatorError if the time is not valid'); 112 } 90 113 $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'); 91 114 $t->is($v->clean(time()), date('Y-m-d H:i:s', time()), '->clean() can accept date time with the with_time option');