Support

Account

Forum Replies Created

  • Thanks, I found the keys. Now the only issue is that it says there are 4 errors when only two of the 4 entries are duplicates. It puts a red flag on each of the 4 fields.

    Example:
    gallery_id: network
    gallery_id: network
    gallery_id: automation
    gallery_id: security

     add_filter('acf/validate_value/key=field_58d453ce55ea1', 'acf_unique_repeater_sub_field', 10, 4);
    function acf_unique_repeater_sub_field($valid, $value, $field, $input) {
      // set up an array to hold all submitted values for rows
      $list = array();
      foreach ($_POST['acf']['field_58bbc19f95a04'] as $row) {
        if (in_array($row['field_58d453ce55ea1'], $list)) {
          // this one already exists
          $valid = 'There are duplicate '.$field['name'].' values';
          // found a duplicate so we don't need to continue looping
          break;
        }
        // add the value of this row to the list
        $list[] = $row['field_58d453ce55ea1'];
      }
      return $valid;
    }
  • How do I find the keys?

  • Sorry, I’m trying to have a unique value for the sub-field in one post. I am creating a gallery page. Each page has several groups of images (gallery groups). I would like to have a unique ID (<div id=”home-entertainment”>) that I can use as an anchor to link to.

    For example:

    <div class="gallery-group" id="home-entertainment">
    <h2>Home Entertainment</h2>
    <img src="image1.jpg" />
    <img src="image2.jpg" />
    </div>
    
    <div class="gallery-group" id="home-security">
    <h2>Home Security</h2>
    <img src="image3.jpg" />
    <img src="image4.jpg" />
    </div>
    
    <div class="gallery-group" id="home-automation">
    <h2>Home Automation</h2>
    <img src="image5.jpg" />
    <img src="image6.jpg" />
    </div>
  • It might help to know that this is inside of a repeater field used on the same wordpress page.

    The repeater field is called gallery_group.
    The inner field is called gallery_group_id.

  • It doesn’t seem to be working. I created a field called gallery_group_id. Here is the code I added to functions.php. When I enter two values of the same I am not given any error and it accepts them.

     
      add_filter('acf/validate_value/name='.$field_name, 'acf_unique_value_field', 10, 4);
      
      function acf_unique_value_field($valid, $value, $field, $input) {
        if (!$valid || (!isset($_POST['post_ID']) && !isset($_POST['post_id']))) {
          return $valid;
        }
        if (isset($_POST['post_ID'])) {
          $post_id = intval($_POST['post_ID']);
        } else {
          $post_id = intval($_POST['post_id']);
        }
        if (!$post_id) {
          return $valid;
        }
        $post_type = get_post_type($post_id);
        $field_name = $field['name'];
        $args = array(
          'post_type' => $post_type,
          'post_status' => 'publish, draft, trash',
          'post__not_in' => array($post_id),
          'meta_query' => array(
            array(
              'key' => $field_name,
              'value' => $value
            )
          )
        );
        $query = new WP_Query($args);
        if (count($query->posts)){
          return 'This Value is not Unique. Please enter a unique '.$field['label'];
        }
        return true;
      }
        
    add_filter('acf/validate_value/name=gallery_group_id', 'acf_unique_value_field', 10, 4);
  • I am wanting to prevent a user from entering a value that has already been entered. I am looking at John’s final code above. Does this go in the functions.php file or somewhere else? Will it create a new field type called unique value or will I have to edit the code whenever I create a new field that I want to restrict unique values?

Viewing 6 posts - 1 through 6 (of 6 total)