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
frm_validate_field_entry to calculate final date
Hi everyone,
I have a form with 2 date fields. I want the first 'start date' field to show the current date, and the second 'end date' to show a year from the current date. But I'm having issues getting it to work. Is the hook still valid and working?
My Global FormidablePro settings are using the date format d/m/Y, and I'm using the following code with the Code Snippets plugin. I have the start date populated with [date], and that shows up, and the final date field is blank. But the code below doesn't seem to be populating my end date field with anything.
I did have this working a couple of years ago, so I don't know why I can't get it to work now. Any pointers would be greatly appreciated.
Thanks,
Richard
(Below is my code in the Code Snippets plugin)
add_filter('frm_validate_field_entry', 'set_my_expiration_date', 10, 3);
function set_my_expiration_date($errors, $posted_field, $posted_value){
if ( $posted_field->id == 91 ) { //change 25 to the ID of the date field to change
// Get the first date in a UNIX timestamp
$first_date = date_create_from_format( 'd/m/Y', $_POST['item_meta'][90] ); //Change 20 to the ID of the first date field
$first_date = date_format( $first_date, 'Y-m-d' );
$first_date = strtotime( $first_date );
// Get the final date in Y-m-d format
$final_date = strtotime('+365 day', $first_date);
$final_date = date('Y-m-d', $final_date);
// Save the final date as the posted value
$_POST['item_meta'][$posted_field->id] = $final_date;
}
return $errors;
}
May 19, 2017 at 8:44 pm
I'm certainly no expert, but I've spent 20 minutes looking at your code and it looks like it's an exact match to the example posted on the Formidable site, so it's definitely not an obvious problem. All I can suggest are the standard diagnosis steps that I take in such cases: Are you certain you have the field IDs of 90 and 91 specified correctly? Is the code snippets plugin activated? (I know, that's a really basic thing to ask, but it's tripped me up before!) Are there javascript errors on the page that the form is on?
You could possibly try changing the first line from 10 to 5, per the note about the Pro validations here:
https://formidableforms.com/knowledgebase/frm_validate_field_entry/
add_filter('frm_validate_field_entry', 'set_my_expiration_date', 5, 3);
The other thing I would try is putting [date] in the 2nd date field too in the form editor; I've had situations where a PHP function wouldn't work if the field was null. Not sure if that's the case with this filter or not.
Finally, do you have any other validations or customizations running on this particular form? Try commenting those out while you test this one, to see if perhaps something is interfering somehow.
That's all I got. This one is a stumper based on what you posted.
May 19, 2017 at 9:57 pm
I just remembered a change that we had to make to one of our similar functions.
Try adding this immediately after the if(posted_field... line
global $wpdb;
$frmdb = new FrmDb();
May 21, 2017 at 6:24 pm
Thanks for your input. It's really stumping me too. The code I've taken is straight from the Formidable Pro page, I haven't altered it. I'm running the most recent Formidable Pro plugin with the current WP. Whether I'm on MAMP on a test site, or my live domain, I still can't get it working. Its really frustrating. Other hooks are the same. Although I tried the change_my_submit_button_label hook, and that worked. So its very puzzling. I must be doing something incorrectly.
Even if I create a blank form for with just 2 date fields, and use those IDs, it still won't work. My test MAMP site has no extra plugins activated to conflict, the Code Snippets plugin is activate and the snippet itself activated. There don't seem to be any Javascript errors on the page from what I can tell. Even if I put the hook in the child themes functions.php file it still won't do anything.
I'll try out your validation number change and see if that does anything.
esadmin, what does your code below do?
global $wpdb;
$frmdb = new FrmDb();
Thanks again for the help.
I'll keep you posted.
Richard
May 21, 2017 at 6:45 pm
Dropping the validation number from 10 to 5 or including esadmin's code didn't work. I guess I'm doing something incorrectly. I'm just not sure what.
May 22, 2017 at 5:06 pm
This is a really silly question but.... are you sure the frm_validate_field_entry hook isn't working? The reason I ask is that this hook operates behind the scenes, after the entry is updated. It won't push the new data to the front-end field. Are you looking in the backend (refreshing the Entries screen) to see if it worked after submitting an entry? Again, apologies if this is a very basic question, but I've been tripped up by smaller issues, so figured it was worth asking!
If you're saying that ALL of your hooks aren't working now, then I'd wonder if some other code on your system is interfering. Have you tried with all plugins deactivated except for Formidable and your Code Snippets? What about using a default theme?
July 7, 2017 at 3:52 am
What is the date format in your 'form settings' set too? Doesn't it have to match Y-m-d format? That's my guess.
Discussion closed.