Skip to content

Commit

Permalink
DateInput accepts string as value
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz committed Oct 29, 2021
1 parent 09122cb commit 62f3b17
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/DateInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Nette;
use Nette\Forms\Controls\TextInput;
use Nette\Forms\Validator;
use Throwable;

final class DateInput extends TextInput {

Expand Down Expand Up @@ -93,11 +94,21 @@ public function getControl(): Nette\Utils\Html {
}

/**
* @param DateTime|null $value
* @param DateTime|string|null $value
*/
public function setValue($value) {
if ($value !== null && !$value instanceof DateTime) {
throw new \LogicException("Must be a DateTime or null.");
if (is_string($value)) {
try {
$value = new DateTime($value);
} catch (Throwable $exception) {
throw new \LogicException(
sprintf('Cannot create datetime from string, %s given.', $value),
0,
$exception
);
}
} else if ($value !== null && !$value instanceof DateTime) {
throw new \LogicException("Must be a string or DateTime or null.");
}

$this->value = $value;
Expand Down

0 comments on commit 62f3b17

Please sign in to comment.