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

Commit

Permalink
DateTimeInput: add default time option
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrtak-CZ committed Apr 20, 2015
1 parent 7bbfc1e commit bf3666c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Nella/Forms/DateTime/DateTimeInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class DateTimeInput extends \Nette\Forms\Controls\BaseControl
/** @var bool */
private $strict = FALSE;

/** @var \DateTimeImmutable|null */
private $defaultTime;

/**
* @param string
* @param string
Expand Down Expand Up @@ -215,7 +218,9 @@ public function getControlPart($key)
} elseif ($key === static::NAME_TIME) {
$control = \Nette\Utils\Html::el('input')->name($name . '[' . static::NAME_TIME . ']');
$control->data('nella-time-format', $this->timeFormat);
$control->value($this->time);
$control->value(
$this->time === null && $this->defaultTime !== null ? $this->defaultTime->format($this->timeFormat) : $this->time
);
$control->type('text');

$control->disabled($this->disabled);
Expand Down Expand Up @@ -292,6 +297,11 @@ public function disableShortHourSanitizer()
$this->sanitizeShortHour = false;
}

public function setDefaultTime(\DateTimeImmutable $defaultTime = NULL)
{
$this->defaultTime = $defaultTime;
}

/**
* @param string
* @return string
Expand Down
10 changes: 10 additions & 0 deletions tests/Nella/Forms/DateTime/DateTimeInputTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,16 @@ class DateTimeInputTest extends \Tester\TestCase
Assert::equal(array('test'), $control->getErrors());
}

public function testDefaultTime()
{
$control = $this->createControl();
$control->setDefaultTime(new \DateTimeImmutable('2015-04-20 12:00:00'));

$dq = \Tester\DomQuery::fromHtml((string) $control->getControlPart(DateTimeInput::NAME_TIME));

Assert::true($dq->has("input[value='12:00']"));
}

/**
* @throws \Nette\InvalidStateException
*/
Expand Down

0 comments on commit bf3666c

Please sign in to comment.