Support

Account

Home Forums General Issues No duplicate or dont show user that already been selected in other field

Helping

No duplicate or dont show user that already been selected in other field

  • Hi!

    Is there a way to avoid duplicates on User fields.
    I have 3 user type fields, I want to not show a user that has been already selected in 1 of the 3 fields. Thankyou!

    screenshot

  • 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;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.