Clearing a value from a field using gform_pre_render [RESOLVED]

So I have code that gets the value of a field on an earlier page in a multi-page form and populates it in a field on the current page:

add_filter( 'gform_pre_render_7', 'populate_value' );
function populate_value($form) {
  $current_page = GFFormDisplay::get_current_page( $form['id'] );
  if ( $current_page == 4 ) {
    foreach( $form['fields'] as &$field ) {
      if ( $field->id == 12 ) {
        if ( is_numeric(rgpost('input_87') ) ) {
          $field->defaultValue = rgpost('input_87');
        } else {
          $field->defaultValue = '';
        }
      }
    }
  }
  return $form;
}

This works for copying the value but the second part, where the field no longer has a value or the value has changed this doesn’t work, I’m assuming because “defaultValue” is only used if the field doesn’t already have a value set, which once you have been to the page and go back to the earlier page it then has a value set and ignore the value in “defaultValue”.

I’ve looked and looked but I can’t find any way to change the existing value of the field. Surely there must be a way to override the value?

Thanks.

Andrew.

I contacted support about this and for anyone wanting to know the answer, it is that you change the value in the $_POST object instead of using the defaultValue in the field object, e.g.

add_filter( 'gform_pre_render_7', 'populate_value' );
function populate_value($form) {
  $current_page = GFFormDisplay::get_current_page( $form['id'] );
  if ( $current_page == 4 ) {
    foreach( $form['fields'] as &$field ) {
      if ( $field->id == 12 ) {
        if ( is_numeric(rgpost('input_87') ) ) {
          $_POST['input_12'] = rgpost('input_87');
        } else {
          $_POST['input_12'] = '';
        }
      }
    }
  }
  return $form;
}
2 Likes