Send notification if validation fails [RESOLVED]

Is there a way to send a notification despite the fact that validation fails.

I have written this custom validation which checks if a gform field is less than another one, if it is, it sets $result[‘is_valid’] = false; allowing the send process to stop and the custom validation message to show.

add_filter( 'gform_field_validation_1_5', function ( $result, $value, $form, $field ) {
    $min_offer = rgpost( 'input_9' );
    if ( $result['is_valid'] && $value <= $min_offer ) {
        $result['is_valid'] = false;
        $result['message']  = 'Unfortunately your offer is too low. Please feel free to try again.';
    }
    return $result;
}, 10, 4 );

However the client wants to be notified by email in this case, is there a way I can show my custom validation message but also send all entry fields to an email?

Hi Theo. Yes, you can do this. See this GFAPI documentation on how to send a notification programmatically:

You can call that right after this line of your code:

if ( $result['is_valid'] && $value <= $min_offer ) {

If you have any other questions, please let us know.

Thanks for this, I hope this doesn’t seem to silly to you but I can’t seem to get it to output the entry details in the email, when I try to add $entry as an argument to the function it just hangs on submission, have I written the syntax wrong?

//$entry added to arguments
add_filter( 'gform_field_validation_1_5', function ($result, $value, $form, $entry, $field) {
    $min_offer = rgpost( 'input_9' );

    if ( $result['is_valid'] && $value <= $min_offer ) {
        //new line added here
        GFAPI::send_notifications( $form, $entry );
        $result['is_valid'] = false;
        $result['message']  = 'Unfortunately your offer is too low. Please feel free to try again.';
    }
    return $result;
}, 10, 4 );

You went a little crazy with the parameters there. You have five listed in your function call:

add_filter( 'gform_field_validation_1_5', function ($result, $value, $form, $entry, $field) {

but the last line of your code (10, 4) only looks for 4 parameters.

If you check the documentation for gform_field_validation, you can see that only 4 parameters are accepted, and $entry is not one of them. The entry is not created at that point. You will need to use $_POST instead. You don’t need to pass it in, so the first line of your code should look like this:

add_filter( 'gform_field_validation_1_5', function ( $result, $value, $form, $field ) {

Then, you can use rgpost to read the $_POST global:

Let us know if you have any other questions.

Thanks for this chris, understood on the $entry object not being available at that stage.

I have had a look at rgpost().

I just don’t understand how I can gather all the entry fields using this and place it into the send_notifications method.

GFAPI::send_notifications( $form, $entry );

Have tried to grab just a single field to start off with but nothing seems to come out in the generated email.

Is there perhaps a way to output the all_fields merge tag into it?

I’ve had a rethink of this and don’t think I even need to force a send at the validation stage, as I could think of a way to do this with a simple conditional confirmation instead, thanks for your input in any case.

Thanks for the update Theo.