Stop form submission and make user enter a field if response is zero [RESOLVED]

Hi Team,

I am sending the form data to external url.

I am trying to do something like this :

If Reponse == 0
don’t submit form
Make user renter the value of a form field saying something like “Please enter a valid address”

else

Submit form

I am not sure what filters and functions to use to make it work.

Regards,
Pallavi

What hook or filter are you using to send data to your external destination? Can you share your code?

Hi Chris,

Thank you for your help.

Here is the code below:

 add_action( 'gform_after_submission_9', 'post_to_third_party_internal', 10, 2 );
function post_to_third_party_internal( $entry, $form ) {
	
    $post_url = ''http://thirdparty.com/report';
    $body = array(
        'apiKey' => 'xxxxxxxxxxxxxxx',
        'reportCode' => 'RENT-REPORT',
		'address' => rgar( $entry, '4.1' ),
		'city' => rgar( $entry, '4.3' ),
		'state' => rgar( $entry, '4.4' ),
		'zip' => rgar( $entry, '4.5' ),
		'propertyType' => rgar( $entry, '10'),
		'bedrooms' => rgar( $entry, '15' ),
        'bathrooms' => rgar( $entry, '14' ),
		'squareFeet' => rgar( $entry, '25' ),
        );
	
	
     $request = new WP_Http();
     $response = $request->post( $post_url, array( 	'timeout' => 45,'redirection' => 5,'httpversion' => '1.0','blocking' => true,'headers' => array(),
	 'body' => $body )); 
	 
	 if ( is_wp_error( $response ) ) {
      $error_message = $response->get_error_message();
      echo "Something went wrong: $error_message";
    } else {
     	
	 $post_report_url = ''http://thirdparty.com/pdf';
   	 $reportid = $response['body'];
     if (empty($reportid)) {
		// Don't submit the form and ask user to enter a valid the address
	 }	
	 else {	 
	 Generate PDF Report
  }		 
}

Regards,
Pallavi

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):


or

Hi Chris,

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.

Regards,
Pallavi

You have to return the validation result in all cases; you’re not, in your else case.

//Assign modified $form object back to the validation result
    $validation_result['form'] = $form;
    return $validation_result;

Hi Chris,

Thank you so much for your help.

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
   }	 
}

Regards,
Pallavi

Hi Chris,

I figured it out. It works now.

Regards,
Pallavi

1 Like

Thanks for the update.