Support

Account

Home Forums Front-end Issues Validate two sub fields against each other from a repeater field Reply To: Validate two sub fields against each other from a repeater field

  • Hello again, I tried solving this by myself, but no luck so far.
    For context :
    – “Cities” is a repeater field. Each row is a “city” (a city where you stop while on a road trip).
    – For each row (city), I ask for start_date and end_date (arrival & departure); these fields (respectively field_5d720d97435c3 and field_5d720db1435c4 are inside a group field field_5d720d70435c2)

    I’d like to make sure, for each row INDEPENDANTLY that start_date < end_date
    So far, as soon as a row is not valid, error appears for each row, even if its valid.
    I have no idea to validate each row independantly.

    Here’s my code :

    function hotel_form_validate_voyage_form( $valid, $value, $field, $input )
    {
    	// bail early if value is already invalid
    	if( !$valid ) { return $valid; }
    
    	$cities= $_POST['acf']['field_5d6fb2c56a359'];
    
    	foreach($citiesas $city)
    	{
    		$start_date= $city['field_5d720d70435c2']['field_5d720d97435c3'];
    		$end_date= $city['field_5d720d70435c2']['field_5d720db1435c4'];
    
    		$start_date_timestamp= turn_acf_date_into_timestamp($start_date);
    		$end_date_timestamp= turn_acf_date_into_timestamp($end_date);
    
    		if($end_date_timestamp<= $start_date_timestamp)
    		{
    			$valid = "ERROR";
    		}
    	}
    	return $valid;
    }
    add_filter('acf/validate_value/key=field_5d720d97435c3', 'hotel_form_validate_voyage_form', 10, 4);
    add_filter('acf/validate_value/key=field_5d720db1435c4', 'hotel_form_validate_voyage_form', 10, 4);

    (function turn_acf_date_into_timestamp is defined elsewhere and works fine)

    I’d greatly appreciate help here 🙂