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’
2
Answers
Please Try this one code. it’s working for me.
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.
Updated Code as per your comment.
please try this one code I think this one works for you.