Home › Forums › Add-ons › Repeater Field › Avoid duplicate content on repeater field › Reply To: Avoid duplicate content on repeater field
Hi, I have used the below code to avoid the duplicate entry on repeater field, But it is not working to me, could you help me to resolve the issue.
Thanks
function unique_repeater_sub_field($valid, $value, $field, $input) {
if (!$valid) {
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 cur rent row
$array = $_POST['acf'];
$repeater_key = false;
$repeater_value = false;
$row_key = false;
$row_value = false;
$field_key = false;
$field_value = false;
for ($i=0; $i<count($matches); $i++) {
if (isset($array[$matches[$i]])) {
$repeater_key = $row_key;
$repeater_value = $row_value;
$row_key = $field_key;
$row_value = $field_value;
$field_key = $matches[$i];
$field_value = $array[$matches[$i]];
if ($field_key == $field['<strong>field_5731ff201fa2e</strong>']) {
break;
}
$array = $array[$matches[$i]];
}
}
if (!$repeater_key) {
// this should not happen, but better safe than sorry
return $valid;
}
// look for duplicate values in the repeater
foreach ($repeater_value as $index => $row) {
if ($index != $row_key && $row[$field_key] == $value) {
// this is a different row with the same value
$valid = 'this value is not unique';
break;
}
}
return $valid;
}
add_filter('acf/validate_value/key='.$field_key, 'unique_repeater_sub_field', 20, 4);
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.