Skip to content

Instantly share code, notes, and snippets.

@tradesouthwest
Created November 16, 2019 19:14
Show Gist options
  • Save tradesouthwest/848d0fbfc114081816632d99167dc28b to your computer and use it in GitHub Desktop.
Save tradesouthwest/848d0fbfc114081816632d99167dc28b to your computer and use it in GitHub Desktop.
Gravity Forms date field validation filters by cutoff date.
<?php // Gravity Forms date field validation
add_filter( 'gform_field_validation_2_10', 'shopguide_gform_validation_2_10', 10, 4 );
function shopguide_gform_validation_2_10( $result, $value, $form, $field )
{
$valid = false;
$day_select = date( 'D', $value );
$date_selected = strtotime( $value );
$now = strtotime( 'now' );
$wed_next = strtotime( 'next Wednesday' );
$this_fri = strtotime( 'next Friday');
if( $now < $this_fri )
{
$wed_to_use = strtotime('+7 days', $wed_next );
} else
{
$wed_to_use = strtotime('+14 days', $wed_next );
}
// false on !wed
if ( intval($date_selected) < $wed_to_use - 388800 )
{
$valid = false;
}
/* otherwise true */
else
{
$valid = true;
}
if ( !$valid ) {
$result["is_valid"] = false;
$result["message"] = '<div class="validation_error">' . esc_html__( 'You missed the cutoff time for ads.
Please select a Wednesday on or after ', 'gravityforms' ) . ' ' . date('m/d/Y', $wed_to_use) . '</div>';
} else {
$result["is_valid"] = true;
}
return $result;
}
/**
* $result, $value, $form, $field
* @param string $value Value of date selection
* @param string $now Time now
* @param string $day_select Day option
* @param string $date_selected Day option
* @param string $cutoff 5PM
*
* @return Boolean Process success or error message
* @see https://docs.gravityforms.com/using-gform-validation-hook/
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment