Home › Forums › ACF PRO › Validate 2 fields › Reply To: Validate 2 fields
All of the field data will be contained in $_POST['acf']
I think. I have not tested this, it is only based on my assumption.
The indexes of this array are the field key values of the fields. It would depend on your fields. But you can get the value of both fields from the $_POST array and check to make sure that they are both not empty.
add_filter('acf/validate_value/name=first_field_name', 'my_acf_validate_value', 10, 4);
add_filter('acf/validate_value/name=second_field_name', 'my_acf_validate_value', 10, 4);
function my_acf_validate_value( $valid, $value, $field, $input ){
// bail early if value is already invalid
if( !$valid ) {
return $valid;
}
// get two values
// you need to change these based on your field keys
$value_1 = $_POST['acf']['field_123abc789']
$value_2 = $_POST['acf']['field_987cba321']
if (empty($value_1) && empty($value_2)) {
$valid = 'Please complete at least one of the fields';
}
// return
return $valid;
}
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!
ACF PRO’s Flexible Content field allows you to create smaller pieces of UI and compose them into whole flexible patterns. In our latest article, we show how to use it to create swappable site sections and integrate it all in a theme.https://t.co/ZRocH8oJSp
— Advanced Custom Fields (@wp_acf) January 24, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.