Support

Account

Home Forums General Issues add_filter “acf/validate_save_post” and acf_add_validation_error

Solved

add_filter “acf/validate_save_post” and acf_add_validation_error

  • Sup guys!

    I need some explications please! I’m trying to understand how I can display a custom error message from custom validation and highlight specific fields that have this error.

    I use acf_form for display my form and I have this code for the validation:
    https://pastebin.com/f4qnmnbB

    In this code, I’m sure the variables $email and $confirmEmail work’s good. I’m sure too that the “acf_add_validation_error” function “work” because if it not there, the form pass.

    I have try this too
    acf_add_validation_error('field_65067bde5086e', 'ok')

    I can’t determine why, the “ok” message’s not displayed and the specific field don’t highlight.

    Is there something I don’t understand?

    Could you help me understand better please hehe?

    Thanks guys!

  • EDIT: I was wrong, forget this second post.

  • You are probably using the wrong method to validate values. I would suggest using acf/validate_value

  • Hello John ( @hube2 )! A really big thank you!

    Does this method just allow me to check one field at a time or can I verify that my “email” field and my “email confirmation” field are the same at the same time?

    (sorry for my bad english)

  • You can compare fields the same way, buy looking at the values in $_POST[‘acf’] to look at other values.

    I am assuming by looking at these that the fields are sub fields of a group field

    
    $email = $_POST['acf']['field_65067bd15086d']['field_65067bde5086e'];
    $confirmEmail = $_POST['acf']['field_65067bd15086d']['field_65067e4f26db1'];
    

    You can do the same thing or you can depend on the $value passed by ACF and compare it to the other value.

  • Thanks @hube2!

    I haven’t tested it yet, but I’ll let you know!

  • Works perfectly!

    Thanks Chief 💪

  • Here is an example for people who want to understand:

    
    add_filter('acf/validate_value/key=field_65067bde5086e', 'checkEmails', 10, 4);
    add_filter('acf/validate_value/key=field_65067e4f26db1', 'checkEmails', 10, 4);
    
    function checkEmails($valid, $value, $field, $input_name){
    
        if(!isset($_POST['acf']['field_65067bd15086d'])) return $valid;
    
        $email = $_POST['acf']['field_65067bd15086d']['field_65067bde5086e'];
        $confirmEmail = $_POST['acf']['field_65067bd15086d']['field_65067e4f26db1'];
    
        if(
            !filter_var($email, FILTER_VALIDATE_EMAIL)
    
            ||
    
            $email !== $confirmEmail
        ){
    
            return 'test';
        }
    
        return $valid;
    }
    
Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.