Home › Forums › General Issues › No duplicate or dont show user that already been selected in other field › Reply To: No duplicate or dont show user that already been selected in other field
This is not something that is easily done and cannot be done without a significant amount of custom coding.
First this is that you’re not likely going to be able to use a “User” field. The results and choices of this field are generated dynamically by ACF. ACF populates this field using AJAX. It may be possible but this will increase the work that needs to be done.
You will need to create your own select fields. You will also have to make this select field searchable yourself by adding custom JavaScript.
At any rate, you will need to add custom JS to each of the fields and when the field is changed then you alter the values in the other fields to remove the selected values from those other fields.
A simpler approach would be create acf/validate_value filters for each field https://www.advancedcustomfields.com/resources/acf-validate_value/ In your filter you can then look at the other values and if they are not unique then the validation fails. This will not prevent duplicates from being selected but it will prevent duplicates from being submitted.
// array of fields that you want to be unique
// I am using field keys here
$my_fields = array(
'field_123456',
'field_345678',
'field_56789A'
);
foreach ($my_fields as $key) {
add_filter('acf/validate_value/key='.$key, 'prevent_duplicate_uses', 10, 4);
}
function prevent_duplicate_uses($valid, $value, $field, $input_name) {
if (!$valid) {
return $valid;
}
$unique = true;
switch ($field['key']) {
case 'field_123456':
if ($value == $_POST['acf']['field_345678'] ||
$value == $_POST['acf']['field_56789A']) {
$unique = false;
}
break;
case 'field_345678':
if ($value == $_POST['acf']['field_123456'] ||
$value == $_POST['acf']['field_56789A']) {
$unique = false;
}
break;
case 'field_56789A':
if ($value == $_POST['acf']['field_123456'] ||
$value == $_POST['acf']['field_345678']) {
$unique = false;
}
break;
}
if (!$unique) {
$valid = 'User Selection Must Be Unique';
}
return $valid;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.