• I have added a custom validation as a code snippet using code snippets plugin. The validation works fine when fields group are attached to a page or custom post type. However, when using the same field group on elementor using acf frontend then the validation is being ignored.

    Background:
    using facetwp user post type. it basically creates a hidden custom post type for users so users can be queried as a post type. Very handy for creating users list in elementor

    Using acf to create a repeater field

    Using a custom validation to ensure unique entries in the repeater based on the first field in the repeater group. The code was found on acf forum

    The main function is here:

    add_filter(‘acf/validate_value/type=repeater’, ‘grly_acf_validate_repeater’, 10, 4);

    function grly_acf_validate_repeater($valid, $value, $field, $input) {

    if (!$valid || !$field[‘preventduplicates’]) {
    return $valid;
    }

    // get list of array indexes from $input
    // [ <= this fixes my IDE, it has problems with unmatched brackets
    preg_match_all(‘/\[([^\]]+)\]/’, $input, $matches);
    if (!count($matches[1])) {
    // this should actually never happen
    return $valid;
    }
    $matches = $matches[1];

    // walk the acf input to find the repeater and current row
    $array = $_POST[‘acf’];

    $field_key = false;
    $field_value = false;

    for ($i = 0; $i < count($matches); $i++) {
    if (isset($array[$matches[$i]])) {

    $field_key = $matches[$i];
    $field_value = $array[$matches[$i]];

    if ($field_key == $field[‘key’]) {
    break;
    }
    $array = $field_value;

    }
    }

    $used = [];
    foreach ($field_value as $index => $row) {
    $first_entry = reset($row); // Extracts the first entry in the object for that row;
    if ($first_entry) {
    if (!in_array($first_entry, $used)) {
    $used[] = $first_entry;
    } else {
    $valid = ‘The value ‘ . $first_entry . ‘ is used more than once. The first field in this repeater must be unique across the repeater.’;
    break;
    }
    }
    }

    return $valid;
    }
    … then I’ve added a ‘prevent duplicates’ field to all repeaters so that it can be toggled on and off:

    add_action(‘acf/render_field_settings/type=repeater’, ‘add_no_duplicates_field_to_repeater’);
    function add_no_duplicates_field_to_repeater($field) {
    acf_render_field_setting($field, [
    ‘label’ => __(‘Prevent duplicates?’, ‘acf’),
    ‘instructions’ => ‘Prevents rows being saved where the first value is the same as that in another row’,
    ‘type’ => ‘radio’,
    ‘name’ => ‘preventduplicates’,
    ‘choices’ => array(
    1 => __(“Yes”, ‘acf’),
    0 => __(“No”, ‘acf’),
    ),
    ‘default_value’ => 0,
    ‘layout’ => ‘horizontal’,
    ]);

    }`

    Source: https://support.advancedcustomfields.com/forums/topic/avoid-duplicate-content-on-repeater-field/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Shabti Kaplan

    (@shabti)

    Please let me know if this works:

    If this is post data, change $_POST[‘acf’] to $_POST[‘acff’][‘post’]

    If this is user data, change $_POST[‘acf’] to $_POST[‘acff’][‘user’]

    Thread Starter welduae

    (@welduae-1)

    Thank you for your reply. I will give it a try and let you know

    Thread Starter welduae

    (@welduae-1)

    $_POST[‘acff’][‘user’] worked like charm.

    Thank you so much

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘ACF Custom Validation Ignored’ is closed to new replies.