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.

Gform_validation example

  1. http://www.gravityhelp.com/documentation/page/Gform_validation

    the example code you offer here is a little confusing

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

    your example would override all other validations and only perform the one validation example you give. the following would be a better example

    <?php
    add_filter('gform_validation', 'custom_validation');
    function custom_validation($validation_result){
    
        if($_POST['input_1'] == 86){ //supposing we don't want input 1 to be a value of 86
    	// set the form validation to false
        $validation_result["is_valid"] = false;
        $validation_result["form"]["fields"][0]["failed_validation"] = true;
        $validation_result["form"]["fields"][0]["validation_message"] = "This field is invalid!";
    	}
        return $validation_result;
    
    }
    ?>
    Posted 12 years ago on Friday June 24, 2011 | Permalink
  2. one other thought. it would be nice if you could override the text

    "There was a problem with your submission. Errors have been highlighted below."

    and replace / append with your own custom message. some validation will not be related to a specific field...

    Posted 12 years ago on Friday June 24, 2011 | Permalink
  3. Thanks for the better example! I have replaced our existing example with yours (with a minor change to make it even more useful).
    As far as setting the main message, you can use the gform_validation_message filter to change it. http://www.gravityhelp.com/documentation/page/Gform_validation_message.

    I agree it would be nicer to set it right from the gform_validation hook. I will keep that in mind for the next versions.

    Posted 12 years ago on Friday June 24, 2011 | Permalink
  4. vlinkdave
    Member

    I'm trying to follow the example but can't seem to validate my form field correctly.

    On the very simple form at http://www.selyestresssolutions.com/e-learning-resources/registration/ I'm checking the form fields based on a cssClass to find the "ARAMCO" field that needs validation and then checking if the value entered is "MCSS5". Unfortunately the form continues to give me the error message that the value inserted is not correct.

    Can you point me to my logic or coding error? I think I'm close :-)

    Thx... dave

    // Registration form MCSS validation
    add_filter('gform_validation', 'custom_validation');
    function custom_validation($validation_result) {
    
        $form = $validation_result["form"];
    
        foreach($form['fields'] as &$field){
    
            if(strpos($field['cssClass'], 'validate-mcss') === false)
                continue;
    
            $field_value = rgpost("input_{$field['id']}");
            $is_valid = validate_mcss($field_value);
    
            if($is_valid)
                continue;
    
            $validation_result['is_valid'] = false;
            $field['failed_validation'] = true;
            $field['validation_message'] = 'The MCSS you have entered is not valid.';
        }
    // update the form in the validation result with the form object you modified
    $validation_result["form"] = $form;
        return $validation_result;
    }
    
    function validate_mcss($mcss) {
        // check mcss value
        if ($mcss != "MCSS5") {
            // MCSS is not valid - must match MCSS5
            return false;
        }
    
        }
    Posted 12 years ago on Friday July 15, 2011 | Permalink
  5. vlinkdave
    Member

    Perhaps I need to trim the passed value before I do the comparison in the function??

    Didn't seem to make a difference when I tried it :-(

    Any thoughts on what I should try next??

    ... dave

    function validate_mcss($mcss) {
        $mcss = trim($mcss);
    // check mcss value
        if ($mcss != "MCSS5") {
            // MCSS is not valid - must match MCSS5
            return false;
        }
    
        }
    Posted 12 years ago on Friday July 15, 2011 | Permalink
  6. You were close... you forgot to "return true;" from your validate_mcss() function.

    function validate_mcss($mcss) {
        $mcss = trim($mcss);
    // check mcss value
        if ($mcss != "MCSS5") {
            // MCSS is not valid - must match MCSS5
            return false;
        }
        return true;
    }
    Posted 12 years ago on Friday July 15, 2011 | Permalink

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