Home › Forums › General Issues › 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!
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.
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;
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.