Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(function($) {
acf.add_action('ready', function( $el ){
//Identify the field you want to check against.
//Use a class to handle scenarios involving repeater / flexible content where multiple instances exist
var $field = $('.example input');
$field.on('change', function (evt) {
//Comparing against taxonomy field values to determine which other fields to show / hide
switch($(this).val()) {
case '27':
//A semi-strange "up over down" jQuery to ensure if inside a repeater / flexibl content you show/hide only the desired field
$(this).closest(".example").siblings(".conditional-display").removeClass("hidden-by-conditional-logic");
break;
default:
//All other states, lets ensure they are hidden
$(this).closest(".example").siblings(".conditional-display").addClass("hidden-by-conditional-logic");
}
});
//On initial page load - ACF data may indicate that a field should be shown instead of hidden
//Each ensures it craws for all repeaters / flexible content
$field.each(function( index ) {
if ($(this).val() == '27') {
$(this).closest(".example").siblings(".conditional-display").removeClass("hidden-by-conditional-logic");
}
});
});
})(jQuery);
<?php
add_filter('acf/validate_value/name=conditional-display', 'validate_conditional_display_field', 10, 4);
function validate_conditional_display_field($valid, $value, $field, $input) {
// bail early if value is already invalid
if (!$valid) {
return $valid;
}
//Loop through each flexible content
foreach ($_POST['acf']['field_5775b7d7f4f20'] as $k => $data) {
//Compare Taxonomy field and whether the 'conditional-display' field is empty
if( $data['field_577aa1d7d9713'] == "27" && !$data['field_5798d4fa12790']):
$valid = __('You have chosen (taxonomy) so you must provide a (conditional display field name).');
endif;
}
return $valid;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment