error with setting default values

By: Henry King | Asked: 08/07/2023
ForumsCategory: Code Helperror with setting default values
Henry King asked 8 months ago
HI I don't know why my new forms are not executing number calculations. They used to work and are working in the older forms.   In this form that I've attached, Total = Number1 + Number 2. But it doesn't update. I've attached the XML file for you to take a look. thanks for helping to debug this.   I think this error is caused by the following code added to functions.php. However, it is weird that it only affects newly added fields or forms.   add_filter('frm_get_default_value', 'Form13_default_values', 10, 4); function Form13_default_values( $new_value, $form_id, $field, $is_default ) { if ($form_id === 13) {  and field  if ( ($field->id == 911) && $is_default )   I just wish to understand why would this add_filter affects the automatic calculations of all newly added fields since I filter it by form. This will be immensely helpful.          
Victor Font replied 8 months ago

Where is your 'return $new_value; statement? Is it outside of the if statement?

When you add any filter, it will execute for every entry and form. With filters you must include the return statement or else no amount of filtering will do anything because you're not passing any values back to the calling execution branch. If the return statement is in the wrong place, you'll experience the same outcome.

In further studying your problem, I went back and looked up this filter's entry in my Codex V2:

Codex V2 displays the actual line in code where the add_filter's related apply_filters line executes. This shows us the actual parameters that Formidable uses at the source code level. What's interesting is that there are 8 apply_filters for frm_get_default_value scattered through Formidable and Formidable Pro, each of which executes 4. Almost all of them use different numbers and types of parameters.

From code examination, you are using the filter as defined in the KnowledgeBase and executed in the FrmEntriesHelper class.

As defined in the KnowledgeBase and confirmed by code examination, the parameters you are using are incorrect for this filter. Your second parameter is form_id that is an invalid parameter for this filter. This aliases for this filter's callback parameters are:

$new_value (string or array)
$field (object)
$is_default (boolean) - will be false if setting a placeholder
$return_array (boolean) - true if an array can be handled correctly

$new_value is the content you can change as a developer. It is the return value that Formidable will continue to process after the filter does its job.

What you are receiving as your $form_id parameter is the $field object. This is where your code falls apart. You are using it as if it is really a form id. Because I teach this stuff to developers, I have to say I think your based code design on a faulty assumption. You may not familiar with the WordPress Hooks API, that defines the rules for actions and filters. The rules are pretty strict.

The correct way to implement your callback is:

function Form13_default_values( $new_value, $field, $is_default, $return_array )

As a developer, you need to examine the value of the $field object. The form id is most likely included in one of its properties. If you don't know how to examine a filter parameter, you create a stub file you can execute in functions.php. I use functions.php because it runs after all plugins are loaded but, with a proper debugger, you can halt WordPress execution before it fully paints to the screen so you can examine variables cleanly.

I use Kint for this type of debugging. Kint is the easiest debugger to use with WordPress. There are only 2 functions you need to remember, d() and ddd(). The d() function displays whatever content or logic results you need to test and continues execution. ddd() displays and halts execution.

I am a debug first developer. When I use hooks with which I am mildly familiar, I examine it in detail. I don't rely on assumptions. I want to know everything in Formidable that can be used in custom code. Kint helps me understand what Formidable provides and expects in return.

I'm going to take a break from work right now, but later today I will either post an image of the $field object contents or leave a test page available for a short time so you can examine its contents.

1 Answers
Victor Font answered 8 months ago
As promised, I'm back and added a Kint debugger image to the frm_get_default_value filter. You'll see the example of how to retrieve the form id from the $field object. ($field->form_id). Just visit this link:
Henry King replied 8 months ago

Hi thanks for your excellent detailed response to my issue.

My original and latest code is below. Forget about form_id as one of the parameters which I used as testing and apparently it doesn't work.
Setting default value for field 911 is not a problem as it works fine.

My key issue is that once I have this filter in functions.php, the number calculations of fields for new forms do not work. I don't see any mistake with my code.

// get data from user created database that is NOT wordpress defalt DB
// to set default values of various fields in biz value formTextTre
add_filter('frm_get_default_value', 'Form13_default_values', 10, 3);
function Form13_default_values( $new_value, $field, $is_default ) {

// field id 911 - RFR
if ( $field->id == 911 && $is_default ) { //change ID of the field

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community