Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active July 16, 2024 20:34
Show Gist options
  • Save RadGH/95e424f23fa88af997a489579649d190 to your computer and use it in GitHub Desktop.
Save RadGH/95e424f23fa88af997a489579649d190 to your computer and use it in GitHub Desktop.
Gravity Forms: Validate two date fields to ensure the "End date" is after the "Start date"
<?php
// Form #215
// Start date field id: #18
// End date field id: #19
// Validation only applies to the end date, but checks the value of both dates
add_filter('gform_field_validation_215_19', 'rs_validate_two_gf_date_fields', 10, 4);
function rs_validate_two_gf_date_fields( $result, $value, $form, $field ) {
$start_date_field_id = 18;
$end_date_field_id = 19;
$start_date = rgpost("input_{$start_date_field_id}");
$end_date = rgpost("input_{$end_date_field_id}");
$end_date_value = new DateTime(GFFormsModel::prepare_date( $field->dateFormat, array_values($end_date) ));
$start_date_value = new DateTime(GFFormsModel::prepare_date( $field->dateFormat, array_values($start_date) ));
if ( $end_date_value < $start_date_value ) {
$result['is_valid'] = false;
$result['message'] = 'End Date must be on or after the Start date.';
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment