diff --git a/CHANGELOG.md b/CHANGELOG.md index b74d0aea..d33edad0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix slow page loads when many entities are active: the containers available to the user were computed with one query per container on every request - Fix item clone failing when a mandatory dropdown field has an empty default value - Fix search on number fields: `is` / `is not` ignoring values from 1000, `<` / `>` comparing values as text, and `is not` excluding items without value +- Fix `Some mandatory fields are empty` error on a filled dropdown field when its block table contains a leftover column with the same name ## [1.24.7] - 2026-10-28 diff --git a/inc/container.class.php b/inc/container.class.php index 56e9e844..6b9ec560 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -2133,12 +2133,14 @@ public static function validateValues($data, $itemtype, $massiveaction, $is_crea continue; } } - } elseif (isset($data[$name])) { - $value = $data[$name]; - } elseif (isset($data['plugin_fields_' . $name . 'dropdowns_id'])) { - $value = $data['plugin_fields_' . $name . 'dropdowns_id']; - } elseif ($massiveaction) { - continue; + } else { + // Key chosen from the field type: a leftover column named after the field must not hide a dropdown value + $value_key = $field['type'] === 'dropdown' ? sprintf('plugin_fields_%sdropdowns_id', $name) : $name; + if (isset($data[$value_key])) { + $value = $data[$value_key]; + } elseif ($massiveaction) { + continue; + } } //translate label diff --git a/tests/Units/ContainerItemUpdateTest.php b/tests/Units/ContainerItemUpdateTest.php index 8cdc6d01..940937a2 100644 --- a/tests/Units/ContainerItemUpdateTest.php +++ b/tests/Units/ContainerItemUpdateTest.php @@ -31,7 +31,9 @@ namespace GlpiPlugin\Field\Tests\Units; use Auth; +use Change; use Computer; +use DBmysql; use Glpi\Tests\DbTestCase; use Glpi\Tests\GLPITestCase; use GlpiPlugin\Field\Tests\FieldTestTrait; @@ -755,6 +757,61 @@ public function testUpdateIsNotBlockedWhenMandatoryTabFieldIsAlreadyFilled(): vo $this->assertSame('Renamed after tab field was filled', $problem->fields['name']); } + /** + * A leftover column named after a dropdown field (ex: field recreated with another type) + * must not hide the stored dropdown value when an update is checked against stored values. + */ + public function testUpdateIsNotBlockedByLeftoverColumnNamedAfterMandatoryDropdownField(): void + { + /** @var DBmysql $DB */ + global $DB; + + $this->login(); + + $container = $this->createFieldContainer([ + 'label' => 'Dropdown Leftover Column Container', + 'type' => 'dom', + 'itemtypes' => [Change::class], + 'is_active' => 1, + 'entities_id' => 0, + 'is_recursive' => 1, + ]); + $field = $this->createField([ + 'label' => 'Environment', + 'type' => 'dropdown', + PluginFieldsContainer::getForeignKeyField() => $container->getID(), + 'ranking' => 1, + 'is_active' => 1, + 'is_readonly' => 0, + 'mandatory' => 1, + ]); + $field_name = $field->fields['name']; + $dropdown_key = sprintf('plugin_fields_%sdropdowns_id', $field_name); + $value = $this->createItem(PluginFieldsDropdown::getClassname($field_name), ['name' => 'Production']); + + $change = $this->createItem(Change::class, [ + 'name' => 'Change with a mandatory dropdown field', + 'content' => 'Test', + 'entities_id' => 0, + 'c_id' => $container->getID(), + $dropdown_key => $value->getID(), + ], ['c_id', $dropdown_key]); + + $table = getTableForItemType(PluginFieldsContainer::getClassname(Change::class, $container->fields['name'])); + $DB->doQuery(sprintf('ALTER TABLE %s ADD %s INT NOT NULL DEFAULT 0', $DB->quoteName($table), $DB->quoteName($field_name))); + + // Update without submitted plugin values (ex: closing on solution approval) + $updated = $change->update([ + 'id' => $change->getID(), + 'name' => 'Renamed with the leftover column', + ]); + + $this->assertTrue($updated); + $this->assertEmpty($_SESSION['MESSAGE_AFTER_REDIRECT'][ERROR] ?? []); + $change->getFromDB($change->getID()); + $this->assertSame('Renamed with the leftover column', $change->fields['name']); + } + /** * Update a ticket with explicit c_id through an API-like context. */