If you are running your code with the gform_after_submission, it’s too late to prevent submission. The entry is already created, and the confirmation was already shown.
You will have to tie your code to something like the gform_pre_submission filter or gform_validation filter so you can actually do something with the response (i.e. not let the submission continue):
add_filter( ‘gform_validation_20’, ‘post_to_third_party_new’ );
function post_to_third_party_new( $validation_result ) {
// validation failed
$validation_result['is_valid'] = false;
//finding Field with ID of 1 and marking it as failed validation
foreach ( $form['fields'] as &$field ) {
//NOTE: replace 1 with the field you would like to validate
if ( $field->id == '1' ) {
$field->failed_validation = true;
$field->validation_message = 'This field is invalid!';
break;
}
}
}
//Assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
else {
Generate PDF file
}
}
But its not working just shows a message that can not locate your form message.
Now the code is something like this. It throws the validation error even when the address is valid and response is not empty for some reason. Can you please take a look?
add_filter( 'gform_validation_20', 'post_to_third_party_new' );
function post_to_third_party_new( $validation_result ) {
// validation failed
$validation_result['is_valid'] = false;
//finding Field with ID of 1 and marking it as failed validation
foreach ( $form['fields'] as &$field ) {
//NOTE: replace 1 with the field you would like to validate
if ( $field->id == '1' ) {
$field->failed_validation = true;
$field->validation_message = 'This field is invalid!';
break;
}
}
}
//Assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
else {
Generate PDF file
}
}