The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

doing a calculation 'frm_validate_field_entry' hook doesn't update field on form submission

Labels

This Discussion is public

Notifications

Hi All,

I'm new to messing around inside a hook/php to get to data I want to end up graphing in Formidable PRO.

I'm using the hook 'frm_validate_field_entry' and function 'calculate_time'. I hope i even stated that correctly.

What I am trying to achieve is to calculate 'speed of a persons progress'.

A person can have a 'RATING' between 1 - 6.
He has a 'START' date and a 'END' date, both of which he may select from the date drop down formidable element.

I'm told that 'frm_validate_field_entry' will calculate the time between the above two dates. However in one of Stephanie's posts she mentions that one cannot do calculations within a form AND use a hook because the former use javascript and the latter uses PHP. She goes on to show an example of how to manipulate the hook to do his calculations within the hook. Here's the thread for ref (see way at the bottom) https://formidableforms.com/help-desk/calculation-based-on-dates/

 

So here's what I have done in following her example:

add_filter('frm_validate_field_entry', 'calculate_time2', 11, 3);
function calculate_time2($errors, $field, $value){
if($field->id == 460){ //460 is the ID of the hidden or admin only field I have added to hold the SPEED calculation
global $frmpro_settings;

$start = $_POST['item_meta'][465]; //465 is the ID of the first field - START date
$end = $_POST['item_meta'][473]; //473 is the ID of the first field - END date

if (!preg_match('/^d{4}-d{2}-d{2}$/', trim($start)))
$start = FrmProAppHelper::convert_date($start, $frmpro_settings->date_format, 'Y-m-d'); //convert date format

if (!preg_match('/^d{4}-d{2}-d{2}$/', trim($end)))
$end = FrmProAppHelper::convert_date($end, $frmpro_settings->date_format, 'Y-m-d'); //convert date format

$calc = round((strtotime($end)-strtotime($start))/86400); // number of days between dates
$speed = ($_POST['item_meta'][287])/$calc; // 287 is the person's RATING - this is the line i created and need help with
$value = $_POST['item_meta'][460] = $speed; // passing the speed calculation to $value
}
return $errors;
}

So when either I go into my form and enter a new entry or modify an old one, field id [460] aka 'SPEED' is NOT being calculated. According to what Steph mentioned, the hook PHP is fired upon form submission. I don't see my 'SPEED' field being updated to a value. I have tried a 'hidden' and visible by 'admin' only and it does not update. Oddly weird numbers very large numbers show up at times or NO numbers. I've called 'SPEED' - velocity in my example.

I've been wracking my brain on this for several months and short of hiring someone to fix this, my first inclination is to try and learn some of this stuff.. and if I have no hair left, and this forum can't help me, I guess I'll have to look for pro help.

Thanks so much for looking at this.


Attachment:

You have the filter set with the wrong priority. You have the priority set as 11 and the corresponding apply_filters executes at priority 10. You're applying your filter after the filter has already executed. It also has 4 parameters, not 3. See this: http://community.formidableforms.com/knowledgebase/frm_validate_field_entry/#kb-change-the-value-in-a-field

Thanks for helping Victor.

I just spent the last two hours pulling my hair out trying every conceivable thing, not really fully understanding the syntax of function parameters etc. (I think i figured it out.. by the way i have a question at the end for you if you don't mind helping me understand a bit)... but in short...

IT WORKED! : )

It turned out that the Snippet plugin needed me to hit 'save and ACTIVATE'!!! LOL...  my duh.

Here's my question re. parameters.

You know how you mentioned i had four parameters but declared only three? (i think that's what you meant).
Is that because you saw my new variable called $speed?

I'm wondering why $calc and $start and $end don't need to be declared and counted?

The WordPress hook system is one of the most powerful features you can take advantage of when developing code. Hooks are either actions or filters. The difference being that a filter always expects a callback function to return a value. Whenever you use add_filter or add_action, there must be a corresponding apply_filters or do_action somewhere in the application's code, whether that application is a plugin, theme, or WordPress itself. The apply_filters and do_action hooks model the format expected of the add_filter or add_action callbacks. A callback is a function.

All I did was search through Formidable's source code for the apply_filters('frm_validate_field_entry', ... to see how the Formidable developers modeled the callback. They use four parameters and the filter executes as priority 10, which is the WordPress default. Technically, with a filter, the only parameter that is absolutely required is the first one. All the parameters can be passed to your custom callback, but the first is the only one that can be returned.

The priority determines where in the function stack your callback is executed. The higher the priority, the later it is called. The lower the priority, the earlier it is called. The priority often trips up developers, especially with filters. If you assign a callback with a priority before or after the apply_filters executes, and you will not have the result you expect.

Thanks Victor.

Discussion closed.