Support

Account

Home Forums Feature Requests Rearrangeable fields Reply To: Rearrangeable fields

  • Using the acf validation filters it is possible to prevent the user from selecting the same choice more than once.

    Let’s say that you have a repeater field with a key of ‘field_0123456789abc’ and it has a sub field with a key of ‘field_cba9876543210’ and you want to make sure that the sub field in each rows has a unique value.

    
    <?php 
    // validate one row of a repeater against other rows of same repeater
    add_filter('acf/validate_value/key=field_cba9876543210', 'sub_field_unique', 10, 3);
    function sub_field_unique($valid, $value, $field, $input) {
      if (!$valid) {
        return $valid;
      }
      $repeater = 'field_0123456789abc';
      $subfield = 'field_cba9876543210';
      $values = $_POST['acf'][$repeater];
      if (!count($values)) {
        return $valid;
      }
      // extract the current row index from $input
      $current_row = preg_replace('/^\s*acf\[[^\]]+\]\[([^\]]+)\].*$/', '\1', $input);
      $count = count($values);
      foreach ($values as $index => $row) {
        if ($index != $current_row && $value == $row[$subfield]) {
          $valid = 'value must be unique';
        }
      }
      return $valid;
    }
    ?>
    

    The exact logic may be different depending on the field type.