Title: Missing if exist in validation
Last modified: April 2, 2020

---

# Missing if exist in validation

 *  Resolved [RikkerdNL](https://wordpress.org/support/users/rikkerdnl/)
 * (@rikkerdnl)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/missing-if-exist-in-validation/)
 * Hello,
 * Firstly great plugin. I want to use the extended validation option but I am missing
   a function if the value already exist to show a message. There are already a 
   lot of options but that one not, I think this is handy for a lot of people.

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

 *  Plugin Author [Konrad Chmielewski](https://wordpress.org/support/users/hwk-fr/)
 * (@hwk-fr)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/missing-if-exist-in-validation/#post-12623619)
 * Hello,
 * Thanks for the feedback! I’m not sure to fully understand what you mean with “
   the value already exist”? Can you elaborate a little more? Do you have a php 
   function documentation about that behavior?
 * Have a nice day!
 * Regards.
 *  Thread Starter [RikkerdNL](https://wordpress.org/support/users/rikkerdnl/)
 * (@rikkerdnl)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/missing-if-exist-in-validation/#post-12624190)
 * Hello,
 * Thank you for the reply. What mean is there are validation options in your great
   plugin. Like if x exist show y message.
 * But what I am missing an validation when the same value already has been used
   on a another post of the same field type.
 * Let’s say we have a field with an ean number that need to be unique in wordpress.
   At the moment this is not possible. (So far I know in a relative simple way)
 *  Plugin Author [Konrad Chmielewski](https://wordpress.org/support/users/hwk-fr/)
 * (@hwk-fr)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/missing-if-exist-in-validation/#post-12626998)
 * Hello,
 * Well, this looks like a custom validation implementation, and I don’t think this
   would be something that could be integrated within the UI, because there’s so
   many possible conditions/parameters.
 * You can add your own validation using the following ACF hook: [https://www.advancedcustomfields.com/resources/acf-validate_value/](https://www.advancedcustomfields.com/resources/acf-validate_value/)
 * Here is an implementation example, which should work in your case:
 * In this example we’ll validate a field named `my_number` and check if the value
   already exists in any other `page` Post Type:
 *     ```
       add_filter('acf/validate_value/name=my_number', 'my_acf_number_validation', 10, 4);
       function my_acf_number_validation($valid, $value, $field, $input_name){
   
           // Bail early if value is already invalid (example: required).
           if($valid !== true)
               return $valid;
   
           // Setup search to check other pages
           $args = array(
               'post_type' => 'page',
               'posts_per_page' => 1,
               'fields' => 'ids',
               'meta_query' => array(
                   array(
                       'key' => 'my_number',
                       'compare' => '=',
                       'value' => $value // input value
                   )
               )
           );
   
           // Get current post ID
           $post_id = acf_maybe_get_POST('post_ID');
   
           // Exclude current post ID from the search
           if(!empty($post_id)){
   
               $args['post__not_in'] = array($post_id);
   
           }
   
           // Execute the search
           $get_posts = get_posts($args);
   
           // Search is empty. No other page is using this value
           if(empty($get_posts))
               return $valid;
   
           // Search has found something. Return an error
           $valid = 'This number is already used in an another page';
   
           return $valid;
   
       }
       ```
   
 * Hope it helps!
 * Have a nice day.
 * Regards.
 *  Thread Starter [RikkerdNL](https://wordpress.org/support/users/rikkerdnl/)
 * (@rikkerdnl)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/missing-if-exist-in-validation/#post-12635054)
 * Hello,
 * Thank you very much, this is working like I wanted. I only changed the line:
 * ‘key’ => ‘my_number’,
 * To:
 * ‘key’ => $field,
 * So i only need to change the fieldname once.
 *  Plugin Author [Konrad Chmielewski](https://wordpress.org/support/users/hwk-fr/)
 * (@hwk-fr)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/missing-if-exist-in-validation/#post-12635482)
 * Hello,
 * You should use `$field['name']` instead, because `$field` is an array in this
   context.
 * have a nice day!
 * Regards.

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

The topic ‘Missing if exist in validation’ is closed to new replies.

 * ![](https://ps.w.org/acf-extended/assets/icon-256x256.png?rev=2071550)
 * [Advanced Custom Fields: Extended](https://wordpress.org/plugins/acf-extended/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/acf-extended/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/acf-extended/)
 * [Active Topics](https://wordpress.org/support/plugin/acf-extended/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/acf-extended/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/acf-extended/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [Konrad Chmielewski](https://wordpress.org/support/users/hwk-fr/)
 * Last activity: [6 years, 2 months ago](https://wordpress.org/support/topic/missing-if-exist-in-validation/#post-12635482)
 * Status: resolved