PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Updating confirmation message from gform_post_submission hook

  1. Hi,

    I'm trying to dynamically control the confirmation message that is displayed based on the results of a 3rd party API call invoked in the gform_post_submission hook. The message returned is always the one defined in the form as the confirmation message. From looking at the Form Object documentation, it looks like this should be possible. Thoughts?

    add_action( 'gform_post_submission_1', 'doSubscribe', 10, 2 );
    
    function doSubscribe( $entry, $form )
    {
      ...
      $form["confirmation"]["type"] = "message";
        switch ( $api->resultCode )
        {
          case 10:
            $form["confirmation"]["message"] = "Already subscribed.";
            break;
          case 20:
            $form["confirmation"]["message"] = "Pending confirmation.";
            break;
          default:
            $form["confirmation"]["message"] = "General error";
            break;
        }
    }
    Posted 12 years ago on Tuesday November 1, 2011 | Permalink
  2. Do you return the $form object at all? I see you edited the code a little before posting, but if you are updating any of the $form fields, you will need to return the $form object before you can use them.

    I'm also wondering if gform_post_submission fires after the confirmation message is displayed. If so, this will be too late.

    UPDATE: looks like gform_post_submission fires BEFORE the confirmation is displayed, so this should work OK.

    Posted 12 years ago on Tuesday November 1, 2011 | Permalink
  3. I hadn't tried to return the value (I come from a Java-centric world, so may still be doing something wrong relative to PHP), but I did just try:

    a) return $form - this didn't work
    b) return array( $entry, $form ) - also didn't work
    c) change method signature to use &$form to pass by reference

    No luck with any of those. The docs also don't stipulate the objects can be returned, or what that might look like, so I wasn't sure it can be done.

    Posted 12 years ago on Wednesday November 2, 2011 | Permalink
  4. The problem is that the gform_post_submission is an action hook and not a filter. This means that you can't "return" things from it. Parameters are not passed by reference, so that won't work either.
    What I think you want to do is use the gform_validation filter. Inside this filter, you can change the $validation_result["form"] variable to change the confirmation message. The additional benefit to using this filter is that you can actually create a validation error and prevent the form from being submitted (if needed). The only possible problem with using this hook is that the entry hasn't been created yet, so you will need to read the field values from the $_POST variable. Following is the documentation page for the gform_validation filter.
    http://www.gravityhelp.com/documentation/page/Gform_validation

    Posted 12 years ago on Wednesday November 2, 2011 | Permalink
  5. Thanks for the update Alex. Sorry to have mislead you tstrike. Let us know if you need help getting this working with the gform_validation filter.

    Posted 12 years ago on Wednesday November 2, 2011 | Permalink
  6. No worries -- I'm sorted, thanks.

    The difference between add_filter (return values) and add_action (no return values) was enough to get me on the right track. I've used gform_pre_submission_filter instead (which is also after the validation) and I'm now able to override the confirmation message based on my api callout, and as an added bonus I can now store the api results into a hidden field so it appears in the entry viewer.

    Thanks again.

    For completeness (for anyone else looking):

    add_filter( 'gform_pre_submission_filter_1', 'doSubscribe' );
    
    function doSubscription( $form )
    {
    ...
    $form["confirmation"]["type"] = "message";
        switch ( $api->resultCode )
        {
          case 10:
            $form["confirmation"]["message"] = "Already subscribed.";
            break;
          case 20:
            $form["confirmation"]["message"] = "Pending confirmation.";
            break;
          default:
            $form["confirmation"]["message"] = "General error";
            break;
        }
       $_POST['input_4] = $api->resultCode;
       return $form;
    }
    Posted 12 years ago on Thursday November 3, 2011 | Permalink
  7. Thanks for posting your code. I'm certain it will help someone in the future.

    Posted 12 years ago on Thursday November 3, 2011 | Permalink

This topic has been resolved and has been closed to new replies.