• Resolved sistunix

    (@sistunix)


    Dear Forminator-Support-Team,

    first thanks a lot for this great form plugin with hundreds of customization options!

    I have setup a form and want to accomplish different bahaviour depending on the date of a field an the current date and time.
    Unfotunately my PHP-/JS-Skills are very little. I found some similar problems in the forums but couldn’t adapt a solution for me. Maybe you could help?

    I have a date field entered by the user – let’s call it “date-1“.
    When the form ist sent, it should check
    IF the value of date-1 is same as the current date (or value of hidden field “hidden-2“) AND current time (or value of hidden field “hidden-1” with send time) is before 7:00
    –> if so, it should enter the value “1” f.e. to a certain field, let’s call it “hidden-3”.

    Thanks in advance!

    • This topic was modified 10 months, 3 weeks ago by sistunix.
    • This topic was modified 10 months, 3 weeks ago by sistunix.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @sistunix

    I hope you are doing well today.

    Query seems to be simple, but it seems we are missing som data. What is the end goal? Does that value will be send to admin email or you still want to show that to user?

    By default you can cover both requests.

    HTML field scenario:
    HTML fields support Visibility rules, so you can set up 3xHTML fields with values 1,2,3.
    In the Visibility rules of each HTML field, you set up a condition to show that HTML depends on the date and hour.

    Email notification scenario:
    If you want to hide those values for the user you need to create 3xHIDDEN FIELD with values 1,2,3.
    At this point, you set up 3 different emails (they can be set up for the same admin). Same as in the previous scenario Visibility rules are supported in emails so for each email, you set up a condition to show 1,2, or 3 values based on the date and hour.

    You cannot show hidden field in email, so that is why you need 3 emails with conditions of date and hour, and body of those 3 emails will simply contain manully enter values like 1,2,3.

    Example:
    email 1 body: {email-1} / {date-1} / 1
    email 2 body: {email-1} / {date-1} / 2
    email 3 body: {email-1} / {date-1} / 3

    Kind Regards,
    Kris

    Thread Starter sistunix

    (@sistunix)

    Hey Kris (@wpmudevsupport13),

    thanks for your quick reply.

    The Endgoal ist:

    IF the form ist filled/sent after 7:00 AM AND the value of date-1 field is the same date as the current date the user will be forwarded to a specific page.

    Otherwise user will be forwarded to antoher page.

    hidden-1 and hidden-2 are just to get the current date and time. If they aren’t needed I don’t use them. Just thought date-format could be the same as in date-1 for easier comparisation.

    The processors to send data by mail won’t change at all and should send in both cases.

    I thought it would be sufficient to change the value of hidden-3 from “0” (default) to “1” to check this value in behaviour:
    – forward user to page 1 (when hidden-3 == “1”)
    – forward user to page 2 (when hidden-3 == “0”)

    Thanks
    Simon

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @sistunix

    Thanks for additional explanation!

    Since plugin doesn’t know if selected date is “today” or not, your idea with additional hidden fields is actually a good shot but then – we still need another additional field and custom code as plugin cannot compare two fields in conditions; it can only compare field against defined values.

    But all in all, with additional fields, conditions configurations and custom code, I believe it should be doable. Here is how:

    1. Add a “hidden-1” field to the form and set its “Default value (optional)” to “Date”. Make sure that the date format there is exactly the same as date format set in your datepicker field – this is critically important!

    2. Add a “hidden-2” field to the form and set its “Default value (optional)” to “Custom value”, then set that value to 0.

    3. Now you want to add following custom code to the site:

    <?php 
    
    add_filter( 'forminator_prepared_data', 'wpmudev_update_hidden_field_custom', 10, 2 );
    function wpmudev_update_hidden_field_custom( $prepared_data, $module_object ) {
        $form_ids = array(3010); // Please change the form ID.
    	if ( ! in_array( $module_object->id, $form_ids ) ) {
    		return $prepared_data;
    	}
    
        $date = $prepared_data['hidden-1'];
        $datediff   = $prepared_data['date-1'];
    	    
        if ( $date == $datediff  ) {
            $prepared_data['hidden-2'] = 1;
        }
        
    	return $prepared_data;
    }
    
    add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_custom', 10, 3 );
    function wpmudev_change_hidden_field_custom( $entry, $module_id, $field_data_array ) {
        $form_ids = array(3010); // Please change the form ID.
        if ( !in_array( $module_id, $form_ids ) ) {
    		return;
        }
        
        foreach ( $field_data_array as $key => $value ) {
            if ( strpos( $value['name'], 'hidden-2' ) !== false ) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( Forminator_CForm_Front_Action::$prepared_data['hidden-2'] );
            }
        }
    }

    a) create an empty file with a .php extension (e.g. “forminator-date-compare.php”) in the “/wp-content/mu-plugins” folder of your site’s WordPress install on server

    b) copy and paste code into it

    c) in both these lines in code (there are two of them!)

    $form_ids = array(3010); // Please change the form ID.

    replace number 3010 with the ID of your form; form ID is the number you see in the form shortcode

    d) in this line you may need to adjust your datepicker field ID if it’s different than “date-1”:

    $datediff = $prepared_data['date-1'];

    But I’m assuming that it’s “date-1” and both hidden fields are accordingly “hidden-1” and “hidden-2” and set as explained in points 1 and 2 above.

    e) save the file.

    At this point depending on if the date chosen is “today” or other date, the “hidden-2” field value saved will either be 0 (different date) or 1 (today) and we can use it for redirects.

    4. In form’s “behavior” settings in “Submission” behavior:

    a) add one “Redirect user to URL” behavior
    – set URL user should be taken to if the date is NOT today
    – set rule to “Process behavior if All of the conditions below match”
    – and add condition: “Hidden-2 is not 1”

    b) add another “redirect user to URL” behavior
    – set URL user should be taken to if the date IS today
    – set rule to “Process behavior if All of the conditions below match”
    – and add condition: “Hidden-2 is 1”

    Note: there should only be these two redirects/submission behaviors set. If you have more, you need to set other conditions for them so there would never be “clash” where two are processed at the same time. I hope that makes sense.

    At this point:
    – if user select “today” date from datepicker – they’d be redirected to one URL after submission
    – and if they select different date (than today) from datepicker – they’d be redirected to a different URL after submission.

    —-

    Now if you also want that “today” redirect to only happen if it’s before 7 AM you should be able to simply set additional condition for the redirect based on yet another hidden field; such hidden field should be set to include “submission time” – but there should be no need for additional custom code since time is predefined (you just set condition like “is less” or “does not contain”).

    Best regards,
    Adam

    Thread Starter sistunix

    (@sistunix)

    Dear Adam,

    thanks a lot! Works like a charm now.

    Great support team!

    Regards
    sistunix

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘date and time based processors/behaviour’ is closed to new replies.