Snippets Collections
function example_return_select_label ($field_selector, $post_id) {
	$field = get_field_object($field_selector, $post_id);

	$choices = $field['choices'];
	$value = $field['value'];

	if ( isset($choices[$value]) ) {
		return $choices[$value];
	}
}


// Example usage (https://d.pr/i/Xj7DQ3):
//[example_return_select_label("field_6247468a6a53a",{ID})]
<?php

add_filter( 'acf/validate_value/name=property_size', 'validate_property_size_field', 10, 4 );

function validate_property_size_field( $valid, $value, $field, $input ){

  // bail early if value is already invalid
  if ( ! $valid ) { return $valid; }

  $property_type = $_POST['acf']['field_5f0aa92348bb6'];

  if ( $property_type == 'Rent' ){
    if ( ! $value ) {
      $valid = __( 'This field is required for Rental Properties' );
    }
  }

  return $valid;

}
star

Thu Sep 08 2022 08:24:18 GMT+0000 (Coordinated Universal Time) https://itsmereal.com/advanced-custom-fields-conditional-required-field/

#wordpress #acf #require #field #conditionally #filter

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension