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.

Validate Input

  1. shadowedsoul
    Member

    On my form at: http://dev.shadowedsoul.net/recruitment-application/

    I'm looking to validate the two fields that the users can enter a web address into. I'm comfortable on the php side of things and regular expressions but I'm completely missing where I hook into this functionality and don't see a decent example.

    Any help available on this front?

    Posted 9 years ago on Monday February 21, 2011 | Permalink
  2. There is a validation hook you can use to write custom validation for a field (or fields). The hook is the gform_validation field.

    Here is an example of the gform_validation field in action. This doesn't do what you want to do, it's simply an example of using the gform_validation field to implement custom validation.

    <?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 9 years ago on Monday February 21, 2011 | Permalink
  3. shadowedsoul
    Member

    I was able to get it going pretty easily with the following code:

    add_filter('gform_validation', 'custom_validation');
    function custom_validation($validation_result){
    	$pattern = '|^http(s)?://us.battle.net/wow/en/character/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i';
    	// set the form validation to false
        $form = $validation_result["form"];
        print_r($form["fields"][21]);
    	if(preg_match($pattern, $_POST['input_21']) == false) {
    		$validation_result["is_valid"] = false;
    		$form["fields"][21]["failed_validation"] = true;
    		$form["fields"][21]["validation_message"] = "<strong>Armory Link: </strong>You must provide a valid URL, it should contain http:// at the beginning, and it should be a link to the armory!";
    	}
    	if(preg_match($pattern, $_POST['input_22']) == false && $_POST['input_22'] != "") {
    		$validation_result["is_valid"] = false;
    		$form["fields"][22]["failed_validation"] = true;
    		$form["fields"][22]["validation_message"] = "<strong>Raid Parse: </strong>You must provide a valid URL, it should contain http:// at the beginning.";
    	}
    
        // update the form in the validation result with the form object you modified
        $validation_result["form"] = $form;
    
        return $validation_result;
    
    }

    Thanks for the help!

    Posted 9 years ago on Monday February 21, 2011 | Permalink
  4. Good to hear! The hooks are really powerful and let you do all sorts of things with Gravity Forms. Full documentation on the hooks will be launching with the final release of Gravity Forms v1.5 which we are targeting for next week.

    Posted 9 years ago on Monday February 21, 2011 | Permalink