Skip Navigation

[Resolved] Create a form with dates conditions + send notification at those dates

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to trigger an automatic notification from a Form using custom date fields.

Solution: See Nigel's example code in the following ticket.

Relevant Documentation:
https://toolset.com/forums/topic/cred-form-notification-email-conditional-on-generic-field/#post-1158823

This support ticket is created 3 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by fredr 3 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#2028685

Tell us what you are trying to do?
On a website where producers declare their films, I have a form with three dates :
- Date de mise en production (production start date)
- Date de fin de tournage (end of shooting)
- Date de fin de post-production (end of post-production)

I need to :
1 - make sure that the dates are in the right order
2 - send alerts to specific emails (present in the form too) a few day before the dates.

Is there any documentation that you are following?

1 - Making sure that the dates are in the right order :
I have done that adapting a code found on your support forums (https://toolset.com/forums/topic/select-a-date-that-is-not-before-the-first-date-field/) :
-> I have managed to control the datepickers. When the start date is entered, users cannot select a previous date for other fields.
-> But, then, the form validation says that "You must enter a date"... Impossible to register my dates in the database and to validate the form.

jQuery(document).ready(function() {
var fromId = jQuery('input[name="wpcf-date-de-mise-en-production[display-only]"').attr('id');
var intId = jQuery('input[name="wpcf-date-de-fin-de-tournage[display-only]"').attr('id');
var toId = jQuery('input[name="wpcf-date-de-fin-de-post-production[display-only]"').attr('id');
var $from = jQuery( '#' + fromId );
var $int = jQuery( '#' + intId );
var $to = jQuery( '#' + toId );

$from.datepicker('option', 'dateFormat', 'd MM yy' );
$from.datepicker('option', 'minDate',0);
$from.datepicker('option', 'onSelect', function( selectedDate, obj ) {
$int.datepicker( "option", "minDate", selectedDate );
});

$int.datepicker('option', 'dateFormat', 'd MM yy' );
$int.datepicker('option', 'onSelect', function( selectedDate, obj ) {
$from.datepicker( "option", "maxDate", selectedDate );
$to.datepicker( "option", "minDate", selectedDate );
});

$to.datepicker('option', 'dateFormat', 'd MM yy' );
$to.datepicker('option', 'onSelect', function( selectedDate, obj ) {
$int.datepicker( "option", "maxDate", selectedDate );
});
});

2 - send alerts to specific emails (present in the form too) a few day before the dates.
My reference is also a thread in the support forum : https://toolset.com/forums/topic/send-e-mail-notifications-at-individual-dates/
The instructions are clear, I can create a notification and a condition.
But I'm not sure where and how to add the recipients and the text of the email alert.

What is the link to your site?
hidden link
(password : reaumur)

#2028913

-> I have managed to control the datepickers. When the start date is entered, users cannot select a previous date for other fields.
-> But, then, the form validation says that "You must enter a date"... Impossible to register my dates in the database and to validate the form.

Hello, Forms date fields currently support the following datepicker formats:
F j, Y = April 21, 2021
Y/m/d = 21/04/21
m/d/Y = 04/21/2021
d/m/Y = 21/04/2021
j F Y = 21 April 2021
I think if you try to use an unsupported date format like 'd MM yy' you might experience problems like you've described, where the format isn't accepted when submitting the form.

The instructions are clear, I can create a notification and a condition.
But I'm not sure where and how to add the recipients and the text of the email alert.

Normally you would set the recipient and the email contents in the notification editor inside the Form builder screen. You can create multiple notifications with different recipient email addresses and contents for each notification. Are you unable to find the notification editor in the Form editor, or are you looking for something more custom?

#2029779

Thanks for your answer !
If I understand well it means that :

1 - I have to find a way to convert my date back to datetime format after it has been managed by the datepicker, in order to be able to save it.... I'll explore that (unless you have a snippet somewhere to do just that ?). (May I suggest that you update or modify the code in the support thread I used, since it apparently does not work ?) https://toolset.com/forums/topic/send-e-mail-notifications-at-individual-dates/

2 - If I create a notification and its condition, then they will appear in the form for me to write the message and select the recipients.

Am I right ?

#2029813

1 - I have to find a way to convert my date back to datetime format after it has been managed by the datepicker, in order to be able to save it....
Types date fields store dates in Unix timestamp format in the database. If you choose one of the supported date formats, there is nothing else you need to do. Forms will convert the datepicker selection to Unix timestamp automatically and save it in the database in Unix timestamp format. If you choose a date format that is unsupported, the field may not save correctly. If you choose to implement something custom to work around the problem where Toolset's datepickers do not support a specific date format, any custom field date you store in the database should use Unix timestamp format to be compatible with Types date fields.

2 - If I create a notification and its condition, then they will appear in the form for me to write the message and select the recipients.
You must create notifications inside the Form editor screen using the Email Notification GUI - you cannot create notifications programmatically as far as I know. When you create a notification inside the Form editor screen, you will see a GUI where you can select the recipients and add the email contents. You can create any number of notifications for each Form within the Form editor screen.

#2030925

Thanks for your answers.

1 -
For the datepickers : I have found a code that might be the solution here : https://toolset.com/forums/topic/this-field-is-required-error-but-field-is-filled-out/
I'll try it...

2 - For creating a notification : If I create the notification inside the form, how can I link it to the dates in the form in order to have them sent on the right days ? This choice does not appear.

Creating notifications programmatically seems very possible : the info is in the links below. I just wondered how to put this info into practice, and specifically to add the recipients (from emails entered by the form) and create the message, but it seems that you do not know neither.

https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_event_type
(hook used to create a custom event type for notifications)

https://toolset.com/documentation/programmer-reference/cred-api/#cred_custom_notification_event_type_condition
This hook is used during evaluation of a custom notification condition, when the event type is custom.

#2032379

Creating notifications programmatically seems very possible : the info is in the links below. just wondered how to put this info into practice, and specifically to add the recipients (from emails entered by the form) and create the message, but it seems that you do not know neither.
I am aware of these two API hooks, but neither of these API hooks creates a notification programmatically. These hooks are intended for use with existing notifications created in the GUI. The cred_notification_event_type hook registers a custom event type when the Form is submitted, and informs the notification system that an existing notification should be triggered at some time in the future based on a custom condition(s) you define in a cred_custom_notification_event_type_condition hook.

See Nigel's thorough explanation and detailed example in this ticket:
https://toolset.com/forums/topic/cred-form-notification-email-conditional-on-generic-field/#post-1158823

2 - For creating a notification : If I create the notification inside the form, how can I link it to the dates in the form in order to have them sent on the right days ? This choice does not appear.
Right, you cannot select this trigger/event combination in the Form builder, it must be established programmatically. You should choose the notification email trigger "When submitting the form" in the notification editor, then the cred_notification_event_type hook will inform the notification system that an existing notification should be triggered at some date in the future, based on a custom conditional you define in the cred_custom_notification_event_type_condition hook. Date conditions or any other custom field conditions must be enforced in the cred_custom_notification_event_type_condition hook. The conditional code there must return true (send the notification now) or false (do not send the notification now) depending on whatever criteria you choose, like the current date (the date when the conditional is evaluated), or the value of some custom field in some post. Your custom conditional code can compare the current date to a date stored in a custom field, or compare the current date to a date in a generic date field in your Form, for example.

The example here shows how you might use a Types date field to trigger an existing notification on an arbitrary date by comparing the current date to the date stored in a Types date field in the post created by the Form containing this existing notification:
https://toolset.com/forums/topic/send-email-notification-at-future-date/#post-377653

#2032445

Thanks for your explanations. Thanks to you I FINALLY understood how to use these hooks ! I think I can manage to code my functions and get what I want now.

May I suggest that you add this information/an example to the API documentation ? I've read it many times without understanding what I could do with it.