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.

Cancel a form submit using gform_pre_submission

  1. CollegeDegrees
    Member

    So I have some additional validation that I am doing in the hook gform_pre_submission and would like to know how to make the form cancel. For instance, I tried wiping out the value of a post field and that did not work. Is there some easy way to do this?

    Thank you!

    Posted 13 years ago on Wednesday October 27, 2010 | Permalink
  2. CollegeDegrees
    Member

    I should say that the value I tried wiping is that of a required field.

    Posted 13 years ago on Wednesday October 27, 2010 | Permalink
  3. CollegeDegrees
    Member

    Oh, never mind I got it... I need to use gform_validation... Okay, will do.

    Posted 13 years ago on Wednesday October 27, 2010 | Permalink
  4. CollegeDegrees
    Member

    On again, off again... I have messed around with gform_validation and that doesn't seem to be what I want either... Basically I want to validate an email against an internal scrub list and if the email is there then I want to block the form submit and display a message to the user letting them know I need a different email address. I can easily grab the field and do the check using gform_pre_submission, but I am not sure of exactly how to do that if it is right at all with gform_validation.

    So in gform_pre_submission I just do the following:

    $email = $_POST["input_7"];

    and I have the email address to lookup against the scrub list. I basically tried just wiping $_POST["input-7"] but it still goes through. I am guessing that is based upon the sequence that the hooks are called (pre_submission is after validation maybe...).

    Anyway, this seems like it would be a fairly common problem where people would want to validate some piece of information and stop the form submission so I am assuming that as usual I am making this harder than it needs to be.

    Has anyone else tried anything remotely similar in the past? Any help would be greatly appreciated.

    Posted 13 years ago on Thursday October 28, 2010 | Permalink
  5. You do want do use gform_validation. That is the only hook that can stop a form submission.
    You should be able to read the submitted email the same way you do on gform_pre_submission (i.e. $_POST["input_7"] ).
    The key is to set the "failed_validation" and "validation_message" property of the field to mark it as failed.
    Following is a code snippet to point you in the right direction.
    Good Luck.

    <?php
    add_filter('gform_validation', 'custom_validation');
    function custom_validation($validation_result){
    
        // set the form validation to false
        $validation_result["is_valid"] = false;
        $form = $validation_result["form"];
    
        // specify the first field to be invalid and provide a custom validation message
        $form["fields"][0]["failed_validation"] = true;
        $form["fields"][0]["validation_message"] = "This field is invalid!";
    
        // update the form in the validation result with the form object you modified
        $validation_result["form"] = $form;
    
        return $validation_result;
    
    }
    ?>
    Posted 13 years ago on Thursday October 28, 2010 | Permalink

This topic has been resolved and has been closed to new replies.