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.

Custom validation issue

  1. Hello all,

    I'm having a head-banging issue. I'm using the following docs on the site:

    Using the Gform validation hook
    Gform Validation

    and I'm running into an issue. Maybe I need another set of eyes or something - because the issue seems like it should be *so simple* to fix, but for the life of me, I can't get it to work. My filter looks like so:

    add_filter('gform_validation_1', 'validate_min_group');
    function validate_min_group($validation_result) {
    
    	$form = $validation_result["form"];
    	foreach($form['fields'] as &$field) {
    		if(strpos($field['cssClass'], 'validate-min') === false) continue;
    		$field_value = rgpost("input_{$field['id']}"); // retrieve the value
    
        	if($field_value < '15') {   //if the value is less than 15
        		$validation_result['is_valid'] = false;
        		$field['failed_validation'] = true;
        		$field['validation_message'] = 'A minimum of 15 tickets is required for group pricing.';
        	}
    	}
    
        $validation_result['form'] = $form;
        return $validation_result;
    
    }

    However, no matter what I do, it's absolute: either it ALWAYS returns false, or it ALWAYS returns true.

    It would help if I could print_r() or var_dump the values as they pass, but when I do that, it returns nothing. (for example, print_r($field_value); = doesn't come back at all.) But it just makes no sense at all to me.

    I simply want to check if the field value is less than 15. if it is, i want it to return my error message. but like I said, for the life of me, it's absolute. It wither ALWAYS returns an error, or it NEVER does.

    Can anyone see what I'm doing wrong here?

    Posted 11 years ago on Monday August 20, 2012 | Permalink
  2. David Peralty

    Hi, is there any reason why using the following hook wouldn't work?
    http://www.gravityhelp.com/documentation/page/Gform_field_validation

    <?php
    add_filter("gform_field_validation_175_1", "custom_validation", 10, 4);
    function custom_validation($result, $value, $form, $field){
    
        if($result["is_valid"] && intval($value) < 15){
            $result["is_valid"] = false;
            $result["message"] = "A minimum of 15 tickets is required for group pricing.";
        }
        return $result;
    }
    ?>
    Posted 11 years ago on Tuesday August 21, 2012 | Permalink
  3. Thanks David :)

    I just tried your code above, and I'm having the same issue - it's either ALWAYS false, or ALWAYS true depending on which way I put the carat (less than or greater than).

    I'm still messing with the code, but I'm just not seeing why it's not seemingly recognizing the "<" value. That *seems* to be where the issue lies - in that area somewhere.

    I swear, if I could figure out how to *see* the values returned by $_POST, it would help a lot, but I simply can't get this filter to return the $_POST values to see what, exactly, is being passed (which is usually how I troubleshoot stuff).

    BTW, here's the code I'm using:

    add_filter("gform_field_validation_1_16", "custom_validation", 10, 4); // field 16, form 1
    function custom_validation($result, $value, $form, $field){
        	if($result["is_valid"] && intval($value) < 15){
            	$result["is_valid"] = false;
            	$result["message"] = "A minimum of 15 tickets is required for group pricing.";
        	}
        return $result;
    }

    pretty much exactly what you wrote. I just don't get why it's not doing what it *should* be doing.

    EDIT: Okay - I think it has to do with the Paypal add-on. (This field is a Paypal pricing field.) When I put in a regular text field and run this function, it works perfectly fine. But the one I want to use it on is a Paypal quantity field. Guess I have more digging to do.

    Posted 11 years ago on Tuesday August 21, 2012 | Permalink
  4. Heh. Docs to the rescue again - I'm not looking for "gform_field_validation", I'm looking for "gform_paypal_config_validation". Looking for the exact code I need to use, but I think I'm starting on the right path here.

    Posted 11 years ago on Tuesday August 21, 2012 | Permalink
  5. David Peralty

    Yeah the issue you are experiencing is that the value of money is a string. You would have to convert it to a number and remove the currency symbol and whatnot to do a direct comparison using a greater than or less than operator. I think you can echo a variable and immediately afterwards put exit; and be able to see what is going on.

    Posted 11 years ago on Tuesday August 21, 2012 | Permalink
  6. Well, I found a better workaround XD My idiocy is exposed!

    Instead of trying to add a filter to the "product" pricing field, I simply disabled the "quantity" field in the pricing field option, then added a "quantity" field that goes along with the "product". So 2 fields instead of 1, and I can set a minimum quantity on the "quantity" field.

    Well THAT was a whole lot easier than the past 24 hours ;)

    Thanks anyway! (Maybe that can be a future feature request: to have the ability to set a min/max value on the "product" field when the quantity is displayed, instead of breaking it up into 2 fields?)

    Posted 11 years ago on Tuesday August 21, 2012 | Permalink
  7. David Peralty

    Glad you were able to find a solution. Let us know if anything else comes up. Sometimes, the best thing you can do is take a break from looking at code and tackle it again after a short break. It happens to me all the time when I get too close to a project.

    Posted 11 years ago on Tuesday August 21, 2012 | Permalink

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