From cb0773b5f61af25b904f9ea1f931b57615f7f5e7 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 31 Oct 2023 11:01:53 +0100 Subject: [PATCH] DateTimeControl: fixed loading empty string [Closes #311] --- src/Forms/Controls/DateTimeControl.php | 2 +- .../Controls.DateTimeControl.loadData.phpt | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Forms/Controls/DateTimeControl.php b/src/Forms/Controls/DateTimeControl.php index 72732b6d3..a4f16b97c 100644 --- a/src/Forms/Controls/DateTimeControl.php +++ b/src/Forms/Controls/DateTimeControl.php @@ -121,7 +121,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) { diff --git a/tests/Forms/Controls.DateTimeControl.loadData.phpt b/tests/Forms/Controls.DateTimeControl.loadData.phpt index 26eb3c936..a5236bf07 100644 --- a/tests/Forms/Controls.DateTimeControl.loadData.phpt +++ b/tests/Forms/Controls.DateTimeControl.loadData.phpt @@ -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;