Support

Account

Home Forums Feature Requests Validation of a field based on the value of another field Reply To: Validation of a field based on the value of another field

  • This helped me immensely. God bless you. Here’s a solution using a repeater field as one of the fields to confirm a total of 100%, in case it helps anyone.

    add_filter('acf/validate_value/key=field_582494bd7b140', 'my_validate_function', 10, 4);
    
    function my_validate_function($valid, $value, $field, $input) {
    	
    	// bail early if value is already invalid
    	if( !$valid ) {
    		
    		return $valid;
    		
    	}	
    	
    	$mv_commission 		= $_POST['acf']['field_582494bd7b140'];
    	$vendor_commission	= $_POST['acf']['field_58249ac80b697'];
    	$other_commissions	= $_POST['acf']['field_582492287b13c'];
    	
    	if ( is_array($other_commissions) ) {
    		foreach($other_commissions as $commission){
    			$other_total += $commission['field_582494367b13e'];
    		}
    	}
    
    	$commission_total = $mv_commission + $vendor_commission + $other_total;
    	
    	if ( $commission_total !== 100 ) {
    		return "Error: The total commissions for a voucher must equal 100%. The total commissions set on this voucher currently equal $commission_total%.";
    	}
    
    	return $valid;
    
    }