Support

Account

Home Forums General Issues Conditional logic applied to 'Required'

Solved

Conditional logic applied to 'Required'

  • Hello,

    Any hint on how to face:

    Make a field required only if another field is true.

    Thanks

  • Hi @permanyer

    I think you can use the “acf/validate_value” hook to do it. You can get the value from other fields from the $_POST[‘acf’][‘field_123456789’] variable, where ‘field_123456789’ is the key of the field you want to check.

    I hope this helps.

  • That is exactly it. Thank you!

    My example:

    
    add_filter('acf/validate_value/name=event_category',	'validate_empty_fields', 10, 4);
    
    function validate_empty_fields( $valid, $value, $field, $input ){
    	
    	// bail early if value is already invalid
    	if( !$valid ) {return $valid; }
    	
    
    	$is_event_public = $_POST['acf']['field_55420ab8d2eb2']; // This field is a checkbox
    	
    	if($is_event_public){
    		if(!$value){
    			$valid = __('This field is required for public events');
    		}
    	}
    
    	return $valid;
    	
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Conditional logic applied to 'Required'’ is closed to new replies.