• Resolved RikkerdNL

    (@rikkerdnl)


    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

    (@hwk-fr)

    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

    (@rikkerdnl)

    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

    (@hwk-fr)

    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/

    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

    (@rikkerdnl)

    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

    (@hwk-fr)

    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.