• Resolved flabrea

    (@flabrea)


    hi guys, wow, what a great extension to ACF this plugin is! maybe someone can help me with this?

    I would like to validate a slug field (name: uq_slug) as unique within all submitted values of a specific field group.

    I hope you got any ideas about that! THANKS!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback. Please note that your question is about ACF development, and not the ACF Extended plugin. This forum should be used for the plugin itself.

    I’ll give you a hand tho. In order to do what you want to do, you’ll have to use the acf/validate_value hook. See documentation: https://www.advancedcustomfields.com/resources/acf-validate_value/

    In order to retrieve others fields input values in that hook, you’ll have to use $_POST['acf']['field_5f18a7e13e96f'] data. But that solution isn’t really easy, since you’ll have to use field keys and it could become really hard to read/maintain the code if you have sub fields.

    The best solution is to use acf_setup_meta(). It will basically create an isolated container which will let you do get_field() or have_rows('my_repeater'): the_row() inside the actual form values, instead of getting the data from the database (remember we’re in the Ajax validation process here, datas have not been saved to the database yet!).

    Here is a code example:

    add_filter('acf/validate_value/name=my_meta', 'my_acf_validate_value', 10, 4);
    function my_acf_validate_value($valid, $value, $field, $input_name){
        
        // Bail early if there is no ACF Values
        if(empty(acf_maybe_get_POST('acf')))
            return $valid;
        
        /*
         * Old method: use $_POST['acf'] to retrieve others field values
         *
         * $_POST['acf']['field_5f18a7e13e96f']; // 'my_meta' value
         * $_POST['acf']['field_5f18a7cf6c9b1']; // 'my_meta_2' value
         */
        
        
        /*
         * Easier method: acf_setup_meta
         */
        
        // Setup a custom loop to easily use get_field()
        acf_setup_meta($_POST['acf'], 'validate_my_meta', true);
        
            /*
             * Retrieve 'my_meta_2' input value
             * Note: This is the actual input value entered by the user during submission, not the value in DB!
             * Note2: To retrieve the value in the DB add the post id. ie: get_field('my_meta_2', 123);
             */
            $my_meta_2 = get_field('my_meta_2');
            
            // Check if 'my_meta_2' has the same value as 'my_meta'
            if(!empty($my_meta_2) && $my_meta_2 === $value){
        
                $valid = 'Same values not possible.';
                
            }
        
        // Reset the loop (important)
        acf_reset_meta('validate_my_meta');
        
        // Return $valid. See documentation
        return $valid;
        
    }
    

    Video example: https://i.imgur.com/PZ3kaZO.mp4

    Hope it helps!

    Regards.

    Thread Starter flabrea

    (@flabrea)

    Hey, this is awesome. Thanks so much for your support and the time you have spent on it! I appreciate that. Your solution works great for me.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Validate slug field as unique’ is closed to new replies.