Home › Forums › Front-end Issues › ACF Form Unique Value Validation
I created an ACF Form and use CPT. The form creates a new CPT but I am trying to have the “Inventory ID” field be unique. I have the code below which works but it only does it when adding a new CPT from the WordPress Dashboard side rather than the frontend using the ACF Form. The ACF Form still allows for duplicate ID’s.
// Unique ID field
add_filter('acf/validate_value/name=inventory_id', '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;
}
Is there any way to have the ACF Form on the front end validate for a unique value as well or is it only for on the WP Dashboard side? Thanks!!
The values isset($_POST[‘post_ID’] or $_POST[‘post_id’] are not set on the front end when creating a new post with acf_form().
You’re going to have to alter the code to account for not having a post ID
add_filter('acf/validate_value/name=inventory_id', 'acf_unique_value_field', 10, 4);
function acf_unique_value_field($valid, $value, $field, $input) {
if (!$valid) {
return $valid;
}
if (isset($_POST['post_ID'])) {
$post_id = intval($_POST['post_ID']);
} elseif (isset($_POST['post_id'])) {
$post_id = intval($_POST['post_id']);
} else {
$post_id = false;
}
$post_type = get_post_type($post_id);
$field_name = $field['name'];
$args = array(
'post_type' => $post_type,
'post_status' => 'publish, draft, trash',
'meta_query' => array(
array(
'key' => $field_name,
'value' => $value
)
)
);
if (!empty($post_id) {
$args['post__not_in'] = array($post_id);
}
$query = new WP_Query($args);
if (count($query->posts)){
return 'This Value is not Unique. Please enter a unique '.$field['label'];
}
return true;
}
Thanks for the reply John!
I tried that before and it didn’t seem to work and I just tried your code as well and it still seems to let me submit the ACF Form on the frontend even with a duplicate ID. It works on the Dashboard side still but the frontend allows for the submission.
There is also a problem with this that I missed
$post_type = get_post_type($post_id);
You are going to have to hard code the post type instead of depending on getting the value. If the post type is empty then it defaults to “post” in the query.
Ah perfect, thanks John you are a legend! Just in case anyone in the future needs the code, here is my final code
replaced
'post_type' => $post_type,
with
'post_type' => 'inventory',
since my CPT was inventory.
Final code:
add_filter('acf/validate_value/name=inventory_id', 'acf_unique_value_field', 10, 4);
function acf_unique_value_field($valid, $value, $field, $input)
{
if (!$valid) {
return $valid;
}
if (isset($_POST['post_ID'])) {
$post_id = intval($_POST['post_ID']);
} elseif (isset($_POST['post_id'])) {
$post_id = intval($_POST['post_id']);
} else {
$post_id = false;
}
$field_name = $field['name'];
$args = array(
'post_type' => 'inventory',
'post_status' => 'publish, draft, trash',
'meta_query' => array(
array(
'key' => $field_name,
'value' => $value
)
)
);
if (!empty($post_id)) {
$args['post__not_in'] = array($post_id);
}
$query = new WP_Query($args);
if (count($query->posts)) {
return 'This Value is not Unique. Please enter a unique ' . $field['label'];
}
return true;
}
You must be logged in to reply to this topic.
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.