Skip to content

Commit

Permalink
DateTimeControl: fixed handling of empty string [Closes #311]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 31, 2023
1 parent 94bad1c commit 6b8561b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Forms/Controls/DateTimeControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function __construct($label = null, int $type = self::TypeDate, bool $wit
$this->withSeconds = $withSeconds;
parent::__construct($label);
$this->control->step = $withSeconds ? 1 : null;
$this->setOption('type', 'datetime');
}


Expand All @@ -69,7 +70,9 @@ public function setFormat(string $format)
*/
public function setValue($value)
{
$this->value = $value === null ? null : $this->normalizeValue($value);
$this->value = $value === null || $value === ''
? null
: $this->normalizeValue($value);
return $this;
}

Expand All @@ -96,7 +99,7 @@ private function normalizeValue($value): \DateTimeImmutable
{
if (is_numeric($value)) {
$dt = (new \DateTimeImmutable)->setTimestamp((int) $value);
} elseif (is_string($value)) {
} elseif (is_string($value) && $value !== '') {
$dt = new \DateTimeImmutable($value); // createFromFormat() must not be used because it allows invalid values
} elseif ($value instanceof \DateTime) {
$dt = \DateTimeImmutable::createFromMutable($value);
Expand All @@ -121,7 +124,7 @@ public function loadHttpData(): void
{
$value = $this->getHttpData(Nette\Forms\Form::DataText);
try {
$this->value = is_string($value) && preg_match('~^(\d{4}-\d{2}-\d{2})?T?(\d{2}:\d{2}(:\d{2}(\.\d+)?)?)?$~', $value)
$this->value = is_string($value) && preg_match('~^[\dT.:-]+$~', $value)
? $this->normalizeValue($value)
: null;
} catch (\Throwable $e) {
Expand Down
27 changes: 27 additions & 0 deletions tests/Forms/Controls.DateTimeControl.loadData.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ test('invalid time', function () {
});


test('empty date', function () {
$_POST = ['date' => ''];
$form = new Form;
$input = $form->addDate('date');
Assert::null($input->getValue());
Assert::false($input->isFilled());
});


test('empty time', function () {
$_POST = ['time' => ''];
$form = new Form;
$input = $form->addTime('time');
Assert::null($input->getValue());
Assert::false($input->isFilled());
});


test('empty date-time', function () {
$_POST = ['date' => ''];
$form = new Form;
$input = $form->addDateTime('date');
Assert::null($input->getValue());
Assert::false($input->isFilled());
});


test('valid date', function () {
$_POST = ['date' => '2023-10-22'];
$form = new Form;
Expand Down
9 changes: 9 additions & 0 deletions tests/Forms/Controls.DateTimeControl.value.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ test('invalid argument', function () {
});


test('empty string', function () {
$form = new Form;
$input = $form->addDate('date')
->setValue('');

Assert::null($input->getValue());
});


test('date as string', function () {
$form = new Form;
$input = $form->addDate('date')
Expand Down

0 comments on commit 6b8561b

Please sign in to comment.