spivurno
1/20/2016 - 5:05 PM

GP Limit Dates // Gravity Perks // Validate Date Ranges for Excepted Dates

GP Limit Dates // Gravity Perks // Validate Date Ranges for Excepted Dates

<?php
/**
 * GP Limit Dates // Gravity Perks // Validate Date Ranges for Excepted Dates
 */
// update "1174" to your form ID, update "17" to your end date field ID
add_filter( 'gform_field_validation_1174_17', function( $result, $date, $form, $field ) {

	// if minDate is dictated by another field, make sure the selected range does not contain a disabled date
	if( $result['is_valid'] && $field->{gp_limit_dates()->key( 'minDate' )} && is_numeric( $field->{gp_limit_dates()->key( 'minDate')} ) ) {
		$start_date = rgpost( sprintf( 'input_%d', $field->{gp_limit_dates()->key( 'minDate')} ) );
		if( $start_date ) {
			$end_date = strtotime( $date );
			$_date    = strtotime( $start_date );
			while( $_date < $end_date ) {
				$_is_excepted  = gp_limit_dates()->is_excepted( $_date, $field );
				$_date = strtotime( '+1 day', $_date );
				if( $_is_excepted ) {
					$result['is_valid'] = false;
					$result['message'] = 'One or more dates in the selected range are not available.';
					break;
				}
			}
		}
	}

	return $result;
}, 11, 4 );