Skip to content

Commit

Permalink
Fix error formatting values for custom date fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 authored Dec 9, 2024
1 parent 846fd74 commit 2ff2182
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions phpunit/functional/Glpi/Asset/CustomFieldDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ public static function validateValueProvider()
'expected_value' => 'test',
'is_valid' => false,
],
[
'field_params' => ['type' => DateType::class],
'given_value' => '',
'expected_value' => null,
'is_valid' => true,
],
[
'field_params' => ['type' => DateTimeType::class],
'given_value' => '2021-01-01 00:00:00',
Expand All @@ -247,6 +253,12 @@ public static function validateValueProvider()
'expected_value' => 'test',
'is_valid' => false,
],
[
'field_params' => ['type' => DateTimeType::class],
'given_value' => '',
'expected_value' => null,
'is_valid' => true,
],
];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Glpi/Asset/CustomFieldType/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getFormInput(string $name, mixed $value, ?string $label = null,

public function normalizeValue(mixed $value): ?string
{
if ($value === null) {
if (empty($value)) {
return null;
}
if (!preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $value)) {
Expand All @@ -71,7 +71,7 @@ public function normalizeValue(mixed $value): ?string

public function formatValueFromDB(mixed $value): ?string
{
if ($value === null || $value === '') {
if (empty($value)) {
return null;
}
return date('Y-m-d H:i:s', strtotime($value . ' UTC'));
Expand Down
4 changes: 2 additions & 2 deletions src/Glpi/Asset/CustomFieldType/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getFormInput(string $name, mixed $value, ?string $label = null,

public function normalizeValue(mixed $value): ?string
{
if ($value === null || $value === '') {
if (empty($value)) {
return null;
}
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
Expand All @@ -71,7 +71,7 @@ public function normalizeValue(mixed $value): ?string

public function formatValueFromDB(mixed $value): ?string
{
if ($value === null) {
if (empty($value)) {
return null;
}
return date('Y-m-d', strtotime($value . ' UTC'));
Expand Down

0 comments on commit 2ff2182

Please sign in to comment.