Skip to content

Commit 74bf933

Browse files
authored
Merge pull request #6514 from christianbeeznest/contidos-22802-2
User: Improve user import and extra‑field validation - refs BT#22802
2 parents c400202 + 4c3fe93 commit 74bf933

File tree

1 file changed

+65
-8
lines changed

1 file changed

+65
-8
lines changed

main/inc/lib/extra_field.lib.php

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -824,32 +824,89 @@ public function addElements(
824824
$help
825825
);
826826

827+
// Ensure $requiredFields is an array
827828
$requiredFields = is_array($requiredFields) ? $requiredFields : [];
828-
829+
// Fetch the configured “unique extra field” var
829830
$uniqueField = api_get_configuration_value('extra_field_to_validate_on_user_registration');
830-
if (!empty($uniqueField) && !in_array($uniqueField, $requiredFields, true)) {
831+
// Determine if we’re editing an existing user (item_id present) or creating a new one
832+
$currentUserId = $form->getElementValue('item_id') ?: null;
833+
834+
// Always mark the unique extra field as required on user forms
835+
if (
836+
$this->type === 'user'
837+
&& !empty($uniqueField)
838+
&& !in_array($uniqueField, $requiredFields, true)
839+
) {
831840
$requiredFields[] = $uniqueField;
832841
}
833842

834843
/** @var HTML_QuickForm_element $element */
835844
foreach ($form->getElements() as $element) {
845+
// Strip the “extra_” prefix to get the field name
836846
$name = str_replace('extra_', '', $element->getName());
847+
848+
// 1) Mark as required if configured
837849
if (in_array($name, $requiredFields, true)) {
838850
$form->setRequired($element);
839851
}
840-
if (!empty($uniqueField) && $name === $uniqueField) {
841-
$form->addRule(
842-
'extra_' . $name,
843-
sprintf(get_lang('A user with the same %s already exists in this portal'), $name),
844-
'callback',
845-
['UserManager', 'isExtraFieldValueUniquePerUrl']
852+
853+
// 2) If this is the special extra field on a user form, add uniqueness validation
854+
if ($this->type === 'user') {
855+
$this->applyExtraFieldUniquenessRule(
856+
$form,
857+
$name,
858+
$uniqueField,
859+
$currentUserId
846860
);
847861
}
848862
}
849863

850864
return $extra;
851865
}
852866

867+
/**
868+
* Add the “unique per URL” validation rule for the extra‑field.
869+
*
870+
* @param &$form
871+
* @param string $fieldVar The extra‐field variable name (without “extra_”)
872+
* @param string $uniqueField Configured unique field var
873+
* @param int|null $currentUserId Null for creation, or the existing user ID when editing
874+
*/
875+
protected function applyExtraFieldUniquenessRule(&$form, string $fieldVar, string $uniqueField, ?int $currentUserId): void
876+
{
877+
// Only apply if this is the configured unique field
878+
if ($fieldVar !== $uniqueField) {
879+
return;
880+
}
881+
882+
$elementName = 'extra_' . $fieldVar;
883+
$message = sprintf(
884+
get_lang('A user with the same %s already exists in this portal'),
885+
$fieldVar
886+
);
887+
888+
if ($currentUserId === null) {
889+
// Creation: forbid any existing match
890+
$form->addRule(
891+
$elementName,
892+
$message,
893+
'callback',
894+
['UserManager', 'isExtraFieldValueUniquePerUrl']
895+
);
896+
} else {
897+
// Editing: allow if the only match is this same user
898+
$form->addRule(
899+
$elementName,
900+
$message,
901+
'callback',
902+
function(string $value) use ($currentUserId) {
903+
$existingId = UserManager::isExtraFieldValueUniquePerUrl($value, true);
904+
return $existingId === null || $existingId == $currentUserId;
905+
}
906+
);
907+
}
908+
}
909+
853910
/**
854911
* Return an array of all the extra fields available for this item.
855912
*

0 commit comments

Comments
 (0)