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


Validate two sub fields against each other from a repeater field

  • Hello guys,
    in an ACF front-end form i’ve made, i’ve got a repeater field to add “cities” (it’s a road trip planning tool).
    Each line ( = city) of the repeater have a starting date and an endind date field.

    I’d like, for each city, make sure that starting date is anterior to ending date.

    My plan was to hook on acf/validate_value with key=my_repeater_field and then for each, do my comparison stuff.

    Something along the lines of :

    $cities = $_POST['acf']['repeater_field_key'];
    foreach($cities as $city)
        $date_start = $city[date_start_field_key];
        $date_end = $city[date_end_field_key];
        if($date_start > $date_end)
            // return error

    My problem is : how to return error properly ?
    Knowing that several cities can have incorrect dates, how to handle that properly ?



  • 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 🙂

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Validate two sub fields against each other from a repeater field’ is closed to new replies.