Populate date from choice

Dear All,
Please help again…

I have forms with three fields.

Field 1 is in the form of a dropdown with choices:
A value 1
B value 2
C value 3

Field 2 is in the form of a date.

Field 3 is in the form of a date.

I want field 3 to be filled automatically from Field 1 and Field 2 choices

example:

  • Field 1 Select A and Field 2 select on 06/10/2020 then Field 3 will be filled in automatically on 06/11/2020, this is because A is the date Field 2 plus 1.
  • Field 1 Select B and Field 2 select on 06/10/2020 then Field 3 will be filled automatically on 06/12/2020, this is because B is the date Field 2 plus 2.
  • Field 1 Select C and Field 2 select on 06/10/2020 then Field 3 will be filled in automatically on 06/13/2020, this is because C is the date Field 2 plus 3.

I have got the sample script and it works well, here is the sample script:

class getdata {

    public function __construct( $args = array() ) {

        // set our default arguments, parse against the provided arguments, and store for use throughout the class
        $this->_args = wp_parse_args( $args, array(
            'form_id'         => false,
            'target_field_id' => false,
            'source_field_id' => false,
            'format'          => 'Y-m-d',
            'modifier'        => false,
            'min_date'        => false
        ) );

        if( ! $this->_args['form_id'] || ! $this->_args['target_field_id'] ) {
            return;
        }

        // time for hooks
        add_action( 'init', array( $this, 'init' ) );

    }

    public function init() {

        // make sure we're running the required minimum version of Gravity Forms
        if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
            return;
        }

        if( $this->_args['source_field_id'] ) {
            add_action( 'gform_pre_submission', array( $this, 'populate_date_on_pre_submission' ) );
        } else {
            add_filter( 'gform_pre_render', array( $this, 'populate_date_on_pre_render' ) );
        }

    }

    public function populate_date_on_pre_render( $form ) {

        if( ! $this->is_applicable_form( $form ) ) {
            return $form;
        }

        foreach( $form['fields'] as &$field ) {
            if( $field['id'] == $this->_args['target_field_id'] ) {

                $key = sprintf( 'gwpd_%d_%d', $form['id'], $field['id'] );
                $value = $this->get_modified_date( $field );

                $field['allowsPrepopulate'] = true;
                $field['inputName'] = $key;

                add_filter( "gform_field_value_{$key}", function() use( $value ) { return $value; } );

            }
        }

        return $form;
    }

    public function populate_date_on_pre_submission( $form ) {

        if( ! $this->is_applicable_form( $form ) ) {
            return;
        }

        foreach( $form['fields'] as &$field ) {
            if( $field['id'] == $this->_args['target_field_id'] ) {

                $timestamp = $this->get_source_timestamp( GFFormsModel::get_field( $form, $this->_args['source_field_id'] ) ); 
                $value = $this->get_modified_date( $field, $timestamp );

                $_POST[ "input_{$field['id']}" ] = $value;

            }
        }

    }

    public function get_source_timestamp( $field ) {

        $raw = rgpost( 'input_' . $field['id'] );
        if( is_array( $raw ) ) {
            $raw = array_filter( $raw );
        }

        list( $format, $divider ) = $field['dateFormat'] ? array_pad( explode( '_', $field['dateFormat' ] ), 2, 'slash' ) : array( 'mdy', 'slash' );
        $dividers = array( 'slash' => '/', 'dot' => '.', 'dash' => '-' );

        if( empty( $raw ) ) {
            $raw = date( implode( $dividers[ $divider ], str_split( $format ) ) );
        }

        $date = ! is_array( $raw ) ? explode( $dividers[ $divider ], $raw ) : $raw;

        $month = $date[ strpos( $format, 'm' ) ];
        $day   = $date[ strpos( $format, 'd' ) ];
        $year  = $date[ strpos( $format, 'y' ) ];

        $timestamp = mktime( 0, 0, 0, $month, $day, $year );

        return $timestamp;
    }

    public function get_modified_date( $field, $timestamp = false ) {

        if( ! $timestamp ) {
            $timestamp = current_time( 'timestamp' );
        }

        if( GFFormsModel::get_input_type( $field ) == 'date' ) {

            list( $format, $divider ) = $field['dateFormat'] ? array_pad( explode( '_', $field['dateFormat' ] ), 2, 'slash' ) : array( 'mdy', 'slash' );
            $dividers = array( 'slash' => '/', 'dot' => '.', 'dash' => '-' );

            $format = str_replace( 'y', 'Y', $format );
            $divider = $dividers[$divider];
            $format = implode( $divider, str_split( $format ) );

        } else {

            $format = $this->_args['format'];

        }

        if( $this->_args['modifier'] ) {
            $timestamp = strtotime( $this->_args['modifier'], $timestamp );
        }

        if( $this->_args['min_date'] ) {
            $min_timestamp = strtotime( $this->_args['min_date'] ) ? strtotime( $this->_args['min_date'] ) : $this->_args['min_date'];
            if( $min_timestamp > $timestamp ) {
                $timestamp = $min_timestamp;
            }
        }

        $date = date( $format, $timestamp );

        return $date;
    }

    function is_applicable_form( $form ) {

        $form_id = isset( $form['id'] ) ? $form['id'] : $form;

        return $form_id == $this->_args['form_id'];
    }

}

# Configuration

new getdata( array(
    'form_id' => 17,
    'target_field_id' => 3,
    'source_field_id' => 2,
	'modifier' => '+45 day'
) );

My question is how to modify the code below so that +1 code can read the value from Field 1 ??

'modifier' => '+1 day'

I tried as below but it didn’t work:

$data = $entry[1],
'modifier' => '+$data day'

Please help…

Hi there

Did you resolve this ?

If you need field 3 to be displaying (as opposed to being a hidden field) I think you need a jQuery solution.

Hi Zunaidk,
Still not resolve…yes there is no problem field 3 displaying or hidden…
can you give example what kind jQuery?

© 2008 - 2019. Gravity Forms is a project by Rocketgenius Inc.