diff --git a/main/inc/lib/extra_field.lib.php b/main/inc/lib/extra_field.lib.php index 15175056571..b3d5f09cfcb 100755 --- a/main/inc/lib/extra_field.lib.php +++ b/main/inc/lib/extra_field.lib.php @@ -824,25 +824,39 @@ public function addElements( $help ); + // Ensure $requiredFields is an array $requiredFields = is_array($requiredFields) ? $requiredFields : []; - + // Fetch the configured “unique extra field” var $uniqueField = api_get_configuration_value('extra_field_to_validate_on_user_registration'); - if (!empty($uniqueField) && !in_array($uniqueField, $requiredFields, true)) { + // Determine if we’re editing an existing user (item_id present) or creating a new one + $currentUserId = $form->getElementValue('item_id') ?: null; + + // Always mark the unique extra field as required on user forms + if ( + $this->type === 'user' + && !empty($uniqueField) + && !in_array($uniqueField, $requiredFields, true) + ) { $requiredFields[] = $uniqueField; } /** @var HTML_QuickForm_element $element */ foreach ($form->getElements() as $element) { + // Strip the “extra_” prefix to get the field name $name = str_replace('extra_', '', $element->getName()); + + // 1) Mark as required if configured if (in_array($name, $requiredFields, true)) { $form->setRequired($element); } - if (!empty($uniqueField) && $name === $uniqueField) { - $form->addRule( - 'extra_' . $name, - sprintf(get_lang('A user with the same %s already exists in this portal'), $name), - 'callback', - ['UserManager', 'isExtraFieldValueUniquePerUrl'] + + // 2) If this is the special extra field on a user form, add uniqueness validation + if ($this->type === 'user') { + $this->applyExtraFieldUniquenessRule( + $form, + $name, + $uniqueField, + $currentUserId ); } } @@ -850,6 +864,49 @@ public function addElements( return $extra; } + /** + * Add the “unique per URL” validation rule for the extra‑field. + * + * @param &$form + * @param string $fieldVar The extra‐field variable name (without “extra_”) + * @param string $uniqueField Configured unique field var + * @param int|null $currentUserId Null for creation, or the existing user ID when editing + */ + protected function applyExtraFieldUniquenessRule(&$form, string $fieldVar, string $uniqueField, ?int $currentUserId): void + { + // Only apply if this is the configured unique field + if ($fieldVar !== $uniqueField) { + return; + } + + $elementName = 'extra_' . $fieldVar; + $message = sprintf( + get_lang('A user with the same %s already exists in this portal'), + $fieldVar + ); + + if ($currentUserId === null) { + // Creation: forbid any existing match + $form->addRule( + $elementName, + $message, + 'callback', + ['UserManager', 'isExtraFieldValueUniquePerUrl'] + ); + } else { + // Editing: allow if the only match is this same user + $form->addRule( + $elementName, + $message, + 'callback', + function(string $value) use ($currentUserId) { + $existingId = UserManager::isExtraFieldValueUniquePerUrl($value, true); + return $existingId === null || $existingId == $currentUserId; + } + ); + } + } + /** * Return an array of all the extra fields available for this item. *