• Resolved mwhdesign

    (@mwhdesign)


    I could use some help, I am using ACF Pro Relationship > Post Object for a frontend form which build/edits a post. I’d like to use AFC Extended Validation to prevent the user from selecting the same post, or itself. For example, BusinessXYZ will select a parter business from a list of all other businesses, but should not be able to select themselves. I am new to the validation values and have not had any luck so far. Any help would be greatly appreciated, thank you.

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

    (@hwk-fr)

    Hello,

    Thanks for the feedback, and sorry for the late answer, I was kinda busy with the latest big patch lately.

    You can do that using the acfe/form_validate_form hook (see documentation), and check that the value of your Post Object doesn’t include the current post id (where the form is displayed)

    add_action('acfe/form/validate_form/form=my-form', 'my_form_validation');
    function my_form_validation($form){
        
        // get current post id
        // where the form is displayed
        $post_id = $form['post_id'];
        
        // get field input value (unformatted)
        $my_post_object = get_field('my_post_object', false, false);
        
        // check field value
        if($my_post_object){
    
            // if the Post Object allow multiple selection, use:
            //
            // if(is_array($my_post_object) && in_array($post_id, $my_post_object)){
            //     acfe_add_validation_error(...);
            // }
            //
            // if the Post Object allow a single selection, use:
            //
            // if($my_post_object === $post_id){
            //     acfe_add_validation_error(...);
            // }
            //
            
            // add validation error
            // acfe_add_validation_error('my_post_object', 'Current page cannot be selected');
            
        }
        
    }

    If you want to completely remove the current as being selectable in your Post Object, you can use the native acf/fields/post_object/query hook (see documentation) to excldue a post.

    The equivalent also exist for the ACF Relationship field in case you need it. See documentation here.

    Hope it helps!

    Have a nice day!

    Regards.

Viewing 1 replies (of 1 total)
  • The topic ‘ACF Relationship, Post Object Validation’ is closed to new replies.