I have a form I’ve built for an insurance client and they always want the “Effective Date” dropdown selection to be dynamic for their clients, outputting the next Month/Year from today’s date and the next 11 months/years after. So from right now (November 5, 2024) the dropdown options are dynamically created as follows:
December 2024
January 2025
February 2025
etc…
This all is working just fine. The problem that was just reported to me is that apparently because this is a multi-step form, when a user goes back to Step 1 to change something else (or let’s say they try to proceed to step 2 but they miss a required field and step 1 reloads), the dynamic dropdown selection is wiped out and it pre-populates with whatever the first option is in the dropdown (in this case December 2024 because that’s the next option). I’ve tested this by selecting February 2025 as the dropdown option and proceeding to step 2 and then going back to step 1 and it shows me December 2024 by default again. I’ve also purposely skipped a required field and tried to proceed to step 2 and it will revert back to December 2024 again.
How do I modify the code so that if a selection has been made when progressing through the form steps, it won’t get overridden by the dynamic population hook? Thanks in advance!
My code for reference:
add_filter( 'gform_pre_render_11', 'populate_effective_date' );
add_filter( 'gform_pre_validation_11', 'populate_effective_date' );
add_filter( 'gform_pre_submission_filter_11', 'populate_effective_date' );
add_filter( 'gform_admin_pre_render_11', 'populate_effective_date' );
function populate_effective_date( $form ) {
$choices = array();
$currentMonth = (int)date('m');
for ($x = $currentMonth+1; $x < $currentMonth + 13; $x++) {
$choices[] = array( 'text' => date('F Y', mktime(0, 0, 0, $x, 1)) );
}
foreach ( $form['fields'] as $field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-effective-date' ) === false ) {
continue;
}
$field->choices = $choices;
}
return $form;
}