diff --git a/src/IPub/FormDateTime/Controls/Date.php b/src/IPub/FormDateTime/Controls/Date.php index 345b8eb..57bc210 100644 --- a/src/IPub/FormDateTime/Controls/Date.php +++ b/src/IPub/FormDateTime/Controls/Date.php @@ -458,6 +458,11 @@ public function setValue($value) $rawValue = $value; $value = Utils\DateTime::createFromFormat($this->getDateFormat(TRUE), $value); + // Try default format + if ($value === FALSE) { + $value = Utils\DateTime::createFromFormat(\DateTime::ATOM, $rawValue); + } + // Check if value is valid string if ($value === FALSE) { throw new Nette\InvalidArgumentException; @@ -471,6 +476,10 @@ public function setValue($value) $rawValue = $value->format($this->getDateFormat(TRUE)); } + if($value instanceof \DateTime) { + $value->setTime(0, 0, 0); + } + $this->value = $value; $this->rawValue = $rawValue; @@ -504,7 +513,13 @@ public function getValue() public function loadHttpData() { try { - $this->setValue($this->getHttpData(Forms\Form::DATA_LINE, '[' . static::FIELD_NAME_DATE . ']')); + $date = $this->getHttpData(Forms\Form::DATA_LINE, '[' . static::FIELD_NAME_DATE . ']'); + + if($date !== null) { + $this->setValue($date); + } else { + $this->setValue($this->getHttpData(Forms\Form::DATA_LINE)); + } } catch (Nette\InvalidArgumentException $ex) { $this->value = NULL; diff --git a/src/IPub/FormDateTime/Controls/DateTime.php b/src/IPub/FormDateTime/Controls/DateTime.php index 672a260..47286d3 100644 --- a/src/IPub/FormDateTime/Controls/DateTime.php +++ b/src/IPub/FormDateTime/Controls/DateTime.php @@ -253,6 +253,11 @@ public function setValue($value) $value = Utils\DateTime::createFromFormat($this->getDateTimeFormat(TRUE), $value); + // Try default format + if ($value === FALSE) { + $value = Utils\DateTime::createFromFormat(\DateTime::ATOM, $rawValue); + } + // Check if value is valid string if ($value === FALSE) { throw new Nette\InvalidArgumentException; @@ -304,7 +309,11 @@ public function loadHttpData() // Get time value $time = $this->getHttpData(Forms\Form::DATA_LINE, '[' . static::FIELD_NAME_TIME . ']'); - $this->setValue(sprintf(static::MERGE_FIELDS_PATTERN, $date, $time)); + if($date !== null || $time !== null) { + $this->setValue(sprintf(static::MERGE_FIELDS_PATTERN, $date, $time)); + } else { + $this->setValue($this->getHttpData(Forms\Form::DATA_LINE)); + } } catch (Nette\InvalidArgumentException $ex) { $this->value = NULL; diff --git a/tests/IPubTests/Forms/DatePicker/DateInputTest.phpt b/tests/IPubTests/Forms/DatePicker/DateInputTest.phpt index 2bce8c7..b00ef04 100644 --- a/tests/IPubTests/Forms/DatePicker/DateInputTest.phpt +++ b/tests/IPubTests/Forms/DatePicker/DateInputTest.phpt @@ -38,8 +38,11 @@ class DateInputTest extends Tester\TestCase [NULL, NULL], [new Utils\DateTime('2015-01-10 00:00:00'), new Utils\DateTime('2015-01-10 00:00:00')], [new \DateTime('2015-01-10 00:00:00'), new Utils\DateTime('2015-01-10 00:00:00')], + [new \DateTime('2015-01-10 10:00:00'), new Utils\DateTime('2015-01-10 00:00:00')], [1421017200, new Utils\DateTime('2015-01-12 00:00:00')], ['2015-01-12', new Utils\DateTime('2015-01-12 00:00:00')], + [(new Utils\DateTime('2015-01-14 00:00:00 +0100'))->format(\DateTime::ATOM), new Utils\DateTime('2015-01-14 00:00:00 +0100')], + [(new Utils\DateTime('2015-01-14 10:00:00 +0100'))->format(\DateTime::ATOM), new Utils\DateTime('2015-01-14 00:00:00 +0100')], ]; } @@ -64,6 +67,20 @@ class DateInputTest extends Tester\TestCase [NULL, NULL], ['', NULL], ['2015-01-10', new Utils\DateTime('2015-01-10 00:00:00')], + [(new Utils\DateTime('2015-01-14 00:00:00 +0100'))->format(\DateTime::ATOM), new Utils\DateTime('2015-01-14 00:00:00 +0100')], + ]; + } + + /** + * @return array[]|array + */ + public function dataValidRawPostValues() + { + return [ + [NULL, NULL], + ['', NULL], + ['2015-01-10', new Utils\DateTime('2015-01-10 00:00:00')], + [(new Utils\DateTime('2015-01-14 00:00:00 +0100'))->format(\DateTime::ATOM), new Utils\DateTime('2015-01-14 00:00:00 +0100')], ]; } @@ -128,6 +145,21 @@ class DateInputTest extends Tester\TestCase Assert::equal($expected, $control->getValue()); } + /** + * @dataProvider dataValidRawPostValues + * + * @param string $input + * @param \DateTime|NULL $expected + */ + public function testLoadHttpDataRawValid($input, $expected) + { + $control = $this->createControl([ + 'date' => $input, + ]); + + Assert::equal($expected, $control->getValue()); + } + public function testHtml() { // Create form control diff --git a/tests/IPubTests/Forms/DatePicker/DateTimeInputTest.phpt b/tests/IPubTests/Forms/DatePicker/DateTimeInputTest.phpt index 0802b0b..1071837 100644 --- a/tests/IPubTests/Forms/DatePicker/DateTimeInputTest.phpt +++ b/tests/IPubTests/Forms/DatePicker/DateTimeInputTest.phpt @@ -40,6 +40,7 @@ class DateTimeInputTest extends Tester\TestCase [new \DateTime('2015-01-10 10:50:00'), new Utils\DateTime('2015-01-10 10:50:00')], [1421017200, new Utils\DateTime('2015-01-12 00:00:00')], ['2015-01-12 10:50', new Utils\DateTime('2015-01-12 10:50:00')], + [(new Utils\DateTime('2015-01-14 10:50:00 +0100'))->format(\DateTime::ATOM), new Utils\DateTime('2015-01-14 10:50:00 +0100')], ]; } @@ -69,6 +70,19 @@ class DateTimeInputTest extends Tester\TestCase ]; } + /** + * @return array[]|array + */ + public function dataValidRawPostValues() + { + return [ + [NULL, NULL], + ['', NULL], + ['2015-01-10 12:00', new Utils\DateTime('2015-01-10 12:00:00')], + [(new Utils\DateTime('2015-01-14 12:00 +0100'))->format(\DateTime::ATOM), new Utils\DateTime('2015-01-14 12:00:00 +0100')], + ]; + } + public function dataTemplates() { return [ @@ -132,6 +146,21 @@ class DateTimeInputTest extends Tester\TestCase Assert::equal($expected, $control->getValue()); } + /** + * @dataProvider dataValidRawPostValues + * + * @param string $input + * @param \DateTime|NULL $expected + */ + public function testLoadHttpDataRawValid($input, $expected) + { + $control = $this->createControl([ + 'datetime' => $input, + ]); + + Assert::equal($expected, $control->getValue()); + } + public function testHtml() { // Create form control