Skip to content

Commit

Permalink
DateTimeControl: fixed loading 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 cb0773b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Forms/Controls/DateTimeControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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

0 comments on commit cb0773b

Please sign in to comment.