From f8c9f42f655632e570f0fda843b9009e62ca9b7a Mon Sep 17 00:00:00 2001 From: Marie Bressy <75746914+Mary-Clb@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:53:52 +0100 Subject: [PATCH] =?UTF-8?q?FIX=20/=20Mandatory=20dropdown=20value=20hidden?= =?UTF-8?q?=20by=20a=20leftover=20column=20with=20the=20sam=E2=80=A6=20(#1?= =?UTF-8?q?362)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - [x] I have performed a self-review of my code. - [x] I have added tests (when available) that prove my fix is effective or that my feature works. - [x] I have updated the CHANGELOG with a short functional description of the fix or new feature. - [ ] This change requires a documentation update. ## Description - It fixes !47541 - Follow-up of #1269. When a block table contains a leftover column named after a dropdown field (for example after the field was recreated with another type), approving the solution of a change fails with "Some mandatory fields are empty" although the dropdown value is filled. The lookup order in `validateValues`, which checks the field name before the dropdown key, is older but was harmless while only submitted values were validated. Since #1269, updates without submitted values are checked against the whole stored row, so the leftover column (0) now hides the real value stored in `plugin_fields_dropdowns_id`. The value key is now chosen from the field type, as already done elsewhere in the plugin: the dropdown key for dropdown fields, the field name otherwise. A test reproducing the leftover column on a change update is added. ## Screenshots (if appropriate): (cherry picked from commit db0d7f02bc57e2c689b8f46442fb1836a4ab0318) --- CHANGELOG.md | 1 + inc/container.class.php | 14 +++--- tests/Units/ContainerItemUpdateTest.php | 57 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a344b677..5a0913b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix `Some mandatory fields are empty` error displayed when creating an item with an assigned technician - Fix item creation failing when a Yes/No field has a non boolean 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.25.1] - 2026-10-28 diff --git a/inc/container.class.php b/inc/container.class.php index dd4c7559..05e793a1 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -2110,12 +2110,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 ab8d325e..5e00582a 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. */