Split date-field in 3 parts and fill hidden fields

Hi,

Is it possible to split a date field in 3 parts (Day, Month and Year) and fill 3 hidden fields with them
for example
I have date field 2024/04/18
Hidden field year should be filled with 2024
Hidden field month should be filled with 04
Hidden field day should be filled with 18

Is this possible?

Thanks!

You can do this using the gform_pre_submisison action hook, which runs during form submission, before the entry is saved.

The following example is based on form ID 5, date field ID 1, and the hidden fields having IDs 3, 4, and 5.

add_action( 'gform_pre_submission_5', function ( $form ) {
	// Get the date field.
	$date_field_id = '1';
	$date_field    = GFAPI::get_field( $form, $date_field_id );

	// Get the date field value.
	$value  = $date_field->get_value_submission( array() );
	$format = $date_field->dateFormat ? $date_field->dateFormat : 'ymd';
	$date   = GFCommon::parse_date( $value, $format );

	// Populate the other fields.
	$_POST['input_3'] = rgar( $date, 'year' );
	$_POST['input_4'] = rgar( $date, 'month' );
	$_POST['input_5'] = rgar( $date, 'day' );
} );

The filter can be used in the theme functions.php file or with a code snippets plugin.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.