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.

Validation & Confirmation messages

  1. BIGLIFE
    Member

    I have a form that is being used on a donation page. I am using the "gform_validation" hook to test the payment information with a third party processor and if the payment is declined then I want to display a custom validation message (via the "gform_validation_message" function). If the payment goes through and is accepted then I would like the text confirmation message that is attached to the form to display. I am having issues getting this to work the way I'd like. Here is the code I've got thus far:

    add_filter('gform_validation_9', 'validate_donation');
    function validate_donation($validation_result) {
    
    	// Make a call to process_payment function to process payment and check validity
    	$is_valid = process_payment();
    
    	// If the payment is valid don't do anything, return $validation_result
    	if($is_valid){
       		// Return the validation result
        	return $validation_result;
    		}
    	else{
    		// Fail the validation for the entire form
    		$validation_result['is_valid'] = false;
       		// Return the validation result
        	return $validation_result;
    		}
    }
    
    // This function runs the Donation script for third party processor
    function process_payment() {
        include("donate.php");
    }

    Currently I have the donate.php file processing of the donation and returning true if it goes through and false if there is a problem. The custom message is being called like this:

    add_filter("gform_validation_message_9", "change_message", 10, 2);
    function change_message($message, $form){
    global $donate_error;
    return '<div id="donate_error">' . "An error occurred while processing your submission.<br/>Please modify your information based on the error below and try again.<br/><br/>" .  '<span>' . "Error: {$donate_error}" . '</span>' . '</div>';
    }

    The $donate_error is a global variable that is being called/set from within the donate.php file and displays the error (if any) returned from the third party processor. The problem that I am experiencing is that if the payment is declined the custom validation message shows up as it should and the included error message ($donate_error) is displayed correctly but if the payment goes through and is accepted the custom validation message still shows up and the included error message ($donate_error) is displayed but as an empty string. What am I doing wrong here? It appears that the validation is getting stuck on false or something. Here is link to my form page: https://www.biglifeonlinetest.org/donate

    Posted 7 years ago on Tuesday September 13, 2011 | Permalink
  2. I'm not sure you want to do this purely with the gform_validation hook.

    I'm thinking you'll need to use the gform_pre_submission hook. If you use the gform_validation hook and some other error on the form is triggered (not your custom validation) then because of how you've implemented it the payment could be processed. Then when they correct the issue and submit the form again, the payment could be processed again.

    So you would either have to use the gform_pre_submission hook to check validity and trigger an error using the gform_validation hook if there is one OR have logic to know if the payments been processed yet or not. Since the entry data doesn't exist at that point, i'm not sure how you could do that.

    Look at the gform_pre_submission hook and do what you are trying to do using that instead. It is triggered AFTER validation which means any non-custom validation that occurs isn't going to interfere and cause duplicate payments.

    Posted 7 years ago on Tuesday September 13, 2011 | Permalink
  3. BIGLIFE
    Member

    To make sure I'm following you Carl......your saying use the gform_pre_submission hook to process the donation with my third-party and if it returns an error kick that out to the gform_validation hook which in turn will call the gform_validation_message hook?

    Posted 7 years ago on Wednesday September 28, 2011 | Permalink
  4. I have made some minor adjustments to your code above that I think will solve your problem. Let me know if not.
    http://pastie.org/2607053

    Posted 7 years ago on Wednesday September 28, 2011 | Permalink