Skip to content

Instantly share code, notes, and snippets.

@Idealien
Last active September 19, 2018 17:38
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Idealien/b6cb1c5be9f858d37160c6e3eb0b3fd1 to your computer and use it in GitHub Desktop.
Save Idealien/b6cb1c5be9f858d37160c6e3eb0b3fd1 to your computer and use it in GitHub Desktop.
(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