skip to Main Content

I want to update the default value of a field. Currently, I have a hidden text field called ‘Limit’ with a default value set to ‘No’. I want to change the default value to ‘Yes’ when the number of entries is greater or equal to 3. I use the gform_after_submission hook to call my function.

Please see my code below.

add_action( 'gform_after_submission_112', 'update_limit', 10, 2 );
function update_limit($entry, $form){
    $max_count = 3;
    $no_wait_field_id = 55;
    $entry_count = get_no_wait_count( $form['id'], $no_wait_field_id );
    if($entry_count >= $max_count){
        $no_wait_field->defaultValue = 'Yes';
        error_log("Default value set to Yes");
    }
}
function get_no_wait_count($form_id, $field_id){
    $search_criteria['field_filters'][] = array( 'key' => $field_id , 'value' => 'No' );
    $count = GFAPI::count_entries( $form_id, $search_criteria );
    return $count;
}

I can see the string "Default value set to Yes" output to the terminal, but the default value is not updated in the form. Could somebody please tell me how I can fix it? Thanks.
FYI, I have another text field called Notice. This Notice field has a conditional logic. It will only be displayed if the default value of the Limit field is set to ‘Yes’

enter image description here

2

Answers


  1. Please Try this one code. it’s working for me.

    add_action( 'gform_after_submission_112', 'update_limit', 10, 2 );
    function update_limit($entry, $form){
        $max_count = 3;
        $no_wait_field_id = 55;
        $entry_count = get_no_wait_count( $form['id'], $no_wait_field_id );
        if($entry_count >= $max_count){
            foreach( $form['fields'] as &$field ) {
                if ( $field->id == $no_wait_field_id ) {
                    $field->defaultValue = 'Yes';
                    break;
                }
            }
            unset( $field );
            error_log("Default value set to Yes");
        }
    }
    

    But yes if you are using any conditional logic then please also update the value of the Limit field in the $entry object, otherwise, the conditional logic will not work correctly.

    $entry[$no_wait_field_id] = 'Yes';
    

    Updated Code as per your comment.
    please try this one code I think this one works for you.

    add_action( 'gform_after_submission_112', 'update_limit', 10, 2 );
    function update_limit($entry, $form){
        $max_count = 3;
        $no_wait_field_id = 55;
        $entry_count = get_no_wait_count( $form['id'], $no_wait_field_id );
        if($entry_count >= $max_count){
            $entry[$no_wait_field_id] = 'Yes';
            GFAPI::update_entry($entry);
            error_log("Default value set to Yes");
        }
    }
    function get_no_wait_count($form_id, $field_id){
        $search_criteria['field_filters'][] = array( 'key' => $field_id , 'value' => 'No' );
        $count = GFAPI::count_entries( $form_id, $search_criteria );
        return $count;
    }
    
    Login or Signup to reply.
  2. add_filter( 'gform_pre_render_112', 'update_limit' );
    function update_limit( $form ) {
      foreach( $form['fields'] as &$field ) {
        if( 55 === $field->id && GFAPI::count_entries( '112' >= 3 )) { 
          $field->defaultValue = 'Yes';
        }else if( $field->id == 55  && GFAPI::count_entries( $form['id'] < 3 )) { 
          $field->defaultValue = 'No';
        }
      }
      // Return the updated form
      return $form;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search