Skip to content
This repository has been archived by the owner on Feb 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from MartinMystikJonas/setvalue
Browse files Browse the repository at this point in the history
setValue try also default DateTime constructor format, Date input removes time on setValue
  • Loading branch information
akadlec authored Dec 21, 2018
2 parents 5badd11 + f008b9e commit 95fa1fa
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/IPub/FormDateTime/Controls/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion src/IPub/FormDateTime/Controls/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions tests/IPubTests/Forms/DatePicker/DateInputTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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')],
];
}

Expand All @@ -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')],
];
}

Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions tests/IPubTests/Forms/DatePicker/DateTimeInputTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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')],
];
}

Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 95fa1fa

Please sign in to comment.