Skip to content

Instantly share code, notes, and snippets.

@itsmereal
Created December 22, 2020 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsmereal/b394065f6ab4f9b514a602293e7493ad to your computer and use it in GitHub Desktop.
Save itsmereal/b394065f6ab4f9b514a602293e7493ad to your computer and use it in GitHub Desktop.
Advanced Custom Fields Conditional Required Field - itsmereal.com
<?php
/**
* ACF Conditional Required Field
*/
function validate_property_size_field( $valid, $value, $field, $input ){
// bail early if value is already invalid
if ( ! $valid ) { return $valid; }
// We are checking an existing field value
$property_type = $_POST['acf']['field_5f0aa92348bb6'];
if ( $property_type == 'Rent' ){
if ( ! $value ) {
$valid = __( 'This field is required for Rental Properties' );
}
}
return $valid;
}
add_filter('acf/validate_value/name=property_size', 'validate_property_size_field', 10, 4);
function property_size_required_script_style() { ?>
<style>
.af-field-property-size label:after {
content: '*';
color: #b9051d !important;
margin-left: 5px;
}
.af-field-property-size label.not-required:after {
display:none;
}
</style>
<script>
(function( $ ) {
$(document).ready(function(){
$('.af-field-property-available-for .acf-radio-list li').last().click(function(){
$('.af-field-property-size label').addClass('not-required');
});
$('.af-field-property-available-for .acf-radio-list li').first().click(function(){
$('.af-field-property-size label').removeClass('not-required');
});
});
})( jQuery );
</script>
<?php }
add_action('acf/render_field/name=property_size', 'property_size_required_script_style');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment