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.
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!
❓Ever wondered when and why ACF uses JSON instead of the database? Check out our summary of the most recent session of ACF Chat Friday for the answer, and make sure to register for the next session.
— Advanced Custom Fields (@wp_acf) February 23, 2023
👉 https://t.co/3UtvQbDwNmhttps://t.co/wfFEVcXVKc
© 2023 Advanced Custom Fields.
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.