Gravity Forms custom validation filter
Asked Answered
W

2

9

I have a function that processes sales via a third-party service, processes the result and returns an array with the Status "Success" or "Invalid." This sales call is made using the gform_after_submission hook applied to the specific form.

What I need to do is store the "Success" or "Invalid" result in the array as a variable that I can later pass to a function to validate or invalidate the credit card field, using gform_validation hook.

I'm declaring the variable in a function, like so:

function foo { 
...code to sell product through API...

$status = $checkoutShoppingCartRequest['Result']['Status'];
}

When I print the variable $status within the function, it is showing either Success or Invalid like it should.

Here is other function where I need to use this variable, passed to gform_validation, which fails every time regardless of Success or Invalid result:

function MBvalidate( $validation_result ) {
$form = $validation_result['form'];
if ( $status !== "Success") {
    $validation_result['is_valid'] = false;
    foreach( $form['fields'] as &$field ) {
        if ( $field->id == '34' ) {
            $field->failed_validation = true;
            $field->validation_message = 'Your credit card could not be processed.';
            break;
        }
    }
}
//Assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;

}

add_filter( 'gform_validation_47', 'MBvalidate' );

I have tried passing the variable a number of different ways, via globals and sessions, etc.

I am new to GF development so I am sure I'm missing something. I'd appreciate any direction.

Walloping answered 20/5, 2015 at 14:45 Comment(2)
Where did you pass? $status variable in a callback function ? have you var_dump($validation_result) variable ?Beaulieu
I am assuming you made the variable global by adding global keyword inside MBvalidate() function i.e. global $status , am I correct? Also you will have to put global keyword inside foo() function as wellConner
B
1

The gform_after_submission action hook runs after gform_validation.

Anyway, assuming you can find a hook that runs earlier, what I would do is store a unique variable for each submitted form using the Transients API's set_transient() and get_transient() functions. For example you can create a hidden field in every form which you populate with a random ID. Use this random ID as a key to store and retrieve the Success/Invalid result.

Braggart answered 28/5, 2015 at 12:18 Comment(0)
C
0

$status here is a local variable which has never been defined before you try to use it in if-condition. So, it's always null. Maybe you missed

$status = $validation_result['Result']['Status'];

or something like this before checking the condition.

Cordle answered 27/5, 2015 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.