diff --git a/phpunit/functional/Glpi/Asset/CustomFieldDefinitionTest.php b/phpunit/functional/Glpi/Asset/CustomFieldDefinitionTest.php index 1a5e758e5a0..ed65223dab4 100644 --- a/phpunit/functional/Glpi/Asset/CustomFieldDefinitionTest.php +++ b/phpunit/functional/Glpi/Asset/CustomFieldDefinitionTest.php @@ -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', @@ -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, + ], ]; } diff --git a/src/Glpi/Asset/CustomFieldType/DateTimeType.php b/src/Glpi/Asset/CustomFieldType/DateTimeType.php index 2ab9254ba59..480d87f8c07 100644 --- a/src/Glpi/Asset/CustomFieldType/DateTimeType.php +++ b/src/Glpi/Asset/CustomFieldType/DateTimeType.php @@ -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)) { @@ -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')); diff --git a/src/Glpi/Asset/CustomFieldType/DateType.php b/src/Glpi/Asset/CustomFieldType/DateType.php index 523791ddb5e..bd83b4ee5a2 100644 --- a/src/Glpi/Asset/CustomFieldType/DateType.php +++ b/src/Glpi/Asset/CustomFieldType/DateType.php @@ -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)) { @@ -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'));