Created
December 22, 2020 09:23
-
-
Save itsmereal/b394065f6ab4f9b514a602293e7493ad to your computer and use it in GitHub Desktop.
Advanced Custom Fields Conditional Required Field - itsmereal.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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