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.

Require just the zip code?

  1. ananda
    Member

    We'd like to able to require just the zip code field of an address. Is this possible, using the pre-built address field?

    This is a form it would be good to do on:
    http://www.anandala.org/free-book-chapters/

    Thanks!

    Posted 13 years ago on Friday November 12, 2010 | Permalink
  2. Ananda,
    This is possible through a hook and a few lines of PHP, but it will require some PHP experience.

    Posted 13 years ago on Friday November 12, 2010 | Permalink
  3. ananda
    Member

    Hi Alex, thanks for your answer. That's great to know. I see now that the hooks are documented.

    Looks like I would use gform_pre_submission to run a validation on the zip code and -- somehow?? -- stop the form?

    Posted 13 years ago on Friday November 12, 2010 | Permalink
  4. You would actually need to use the gform_validation filter. Following is a simple example on how to fail a validation for the first field in the form. It should point you in the right direction

    <?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 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 Sunday November 14, 2010 | Permalink
  5. ananda
    Member

    Superb. Thanks, Alex.

    Is there a complete list of filters anywhere?

    Posted 13 years ago on Tuesday November 16, 2010 | Permalink
  6. A complete list of filters will be available with the new documentation that will launch along with the 1.5 final release once it is out of beta testing.

    Posted 13 years ago on Tuesday November 16, 2010 | Permalink
  7. shmooth
    Member

    Hi all,

    Thought I'd add a note for other n00bs like me:

    1) when I first copied this example, I somehow got rid of the following line -- that will break things:

    $form = $validation_result["form"];

    2) to get access to the submitted data, you have to access it via the regular HTTP $_POST["input_name"] PHP server variable (where 'input_name' is probably 'input_1', etc.), b/c at this stage of processing, the $form does not seem to have the submitted values set.

    so, my full, working test example is:

    add_filter('gform_validation', 'custom_validation');
    
    function custom_validation($validation_result){
    
        $form = $validation_result["form"];
    
        // if value of first field name != 'peter'
        if( strcmp($_POST["input_1"],"peter")!=0 ){  
    
            // then mark this submission invalid
            $validation_result["is_valid"] = false;
    
            // mark the errant field [the first (0th) field]
            $form["fields"][0]["failed_validation"] = true;
    
            // and put in an explanatory message to the user on how to fix
            $form["fields"][0]["validation_message"] = "This first field must have the value 'peter'.";
        }
    
        // 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 Monday December 20, 2010 | Permalink