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.

Validating an array with gform_validation

  1. I am trying to use the gform_validation hook to check against an array of codes that we will be sending out to customers. I need to form to only accept the codes that we are using.

    I was hoping it was as simple as using the gform_validation to check against an array like so:

    add_filter('gform_validation', 'custom_validation');
    function custom_validation($validation_result){
    
        //supposing we don't want input 1 to be a value of 86
    
    	$code = array('aaa2', 'aaa3', 'aaa4');
        if($_POST['input_1'] == $code){
    
            // set the form validation to false
            $validation_result["is_valid"] = false;
    
            //finding Field with ID of 1 and marking it as failed validation
            foreach($validation_result["form"]["fields"] as &$field){
    
                //NOTE: replace 1 with the field you would like to validate
                if($field["id"] == "1"){
                    $field["failed_validation"] = true;
                    $field["validation_message"] = "Sorry, try again!";
                    break;
                }
            }
    
        }
    
        return $validation_result;
    }

    Ideally I would love to be able to check a table within our database, but I am not quite sure how that would be handled. Any help would be appreciated

    Posted 12 years ago on Thursday October 20, 2011 | Permalink
  2. What happens when you use that code?

    I am pulling codes from a database on one of my sites, and then I check the code entered into the form, to see if it matches a stored code. If so, continue. Sounds like you want to do the same?

    Posted 12 years ago on Friday October 21, 2011 | Permalink
  3. Yeah that is what I would prefer to do (check the database). I found out that I would be needing to check against about 1500 items, so the array wouldn't work anyway.

    Just not sure how to do the database check within that hook.

    When I use that code it only reads the first value in the array. I actually wrote that wrong too it should be != instead of == . So it has to be one of the items in the array. But the array doesn't seem to work.

    Posted 12 years ago on Friday October 21, 2011 | Permalink
  4. Are you sure the code is being processed? Can you deliberately return a "Good" or "Bad" message, without doing the actual check? I've done things like applied the function to the wrong form in the past, which drove me batty for a while.

    Also, are you certain that input_1 is correct and $field['id'] is actually one? I've been burned by that as well.

    I would first make sure your code is being executed, then work on making it actually validate something, then work on checking against the database. It's all doable. But we need to get the basics down first.

    Posted 12 years ago on Friday October 21, 2011 | Permalink
  5. Okay I was able to get it to obviously check the database table that I have. The odd thing is that it seems to ignore the table items I have and only allow other entries. So if I have "AAA1" in the database it won't let me enter that in the form.

    If I use this code it doesn't allow any entries, not even the ones that are in the database. But if I change the != to == it only allows entries that aren't in the db...

    add_filter('gform_validation', 'custom_validation');
    function custom_validation($validation_result){
    
        //supposing we don't want input 1 to be a value of 86
        global $wpdb;
        $codes = $wpdb->get_results("SELECT code FROM code_check");
        foreach ($codes as $code) {
        if($_POST['input_1'] != $code->code ){
    
            // set the form validation to false
            $validation_result["is_valid"] = false;
    
            //finding Field with ID of 1 and marking it as failed validation
            foreach($validation_result["form"]["fields"] as &$field){
    
                //NOTE: replace 1 with the field you would like to validate
                if($field["id"] == "1"){
                    $field["failed_validation"] = true;
                    $field["validation_message"] = "Sorry, try again!";
                    break;
                } //end if
            } //end foreach
    
        } //end if
        } // end foreach
        return $validation_result;
    
    } //End Function
    Posted 12 years ago on Friday October 21, 2011 | Permalink
  6. Not sure of the logic of it yet, but why not do a SELECT on the database to look for your entered code, and if the SELECT returns results, you have a valid code, so continue, otherwise, the code is invalid, so return a validation result. Pulling all the codes in, every time, then looping through them, seems inefficient.

    Also, you seem to be saying "if the entered code does not match the current code from the database, then set the validation to false. However, if you get a true match, you still loop through all the other codes, so unless the valid code was the last in the array, you'd always have a validation error (i.e. the valid result would be overridden by any invalid results.)

    It might just require a little more thought. I think you almost have it.

    Posted 12 years ago on Friday October 21, 2011 | Permalink
  7. Whew. This seems to be working great now. Thanks for talking me through that.

    add_filter('gform_validation', 'custom_validation');
    function custom_validation($validation_result){
    
        //supposing we don't want input 1 to be a value of 86
        global $wpdb;
    		$input = $_POST['input_1'];
    		$code_check = $wpdb->get_results("SELECT code FROM code_check WHERE code ="$input"");
    		foreach($code_check as $row){
    			$code = $row->code;
    		} //end foreach
    			if($code != $input){
    
    		        // set the form validation to false
    		        $validation_result["is_valid"] = false;
    
    		        //finding Field with ID of 1 and marking it as failed validation
    		        foreach($validation_result["form"]["fields"] as &$field){
    
    		            //NOTE: replace 1 with the field you would like to validate
    		            if($field["id"] == "1"){
    		                $field["failed_validation"] = true;
    		                $field["validation_message"] = "Sorry, try again!";
    		                break;
    		            } //end if
    		        } //end foreach
    
    		    } //end if
    		    return $validation_result;
    
    } //End Function

    If you notice anything wrong here let me know please. Otherwise it only allows the entries in the database table now.

    Thanks!

    Posted 12 years ago on Friday October 21, 2011 | Permalink
  8. Does line 7 actually work with the quotes like that? It looks a little incorrect to me, but if it works for you, it works.

    Also, line 8,9,10 is a very short loop, just one result (unless your SQL SELECT can return more than one result somehow.) You don't need a loop there at all, you just need to assign the results of your query to. You can remove lines 8 and 10. Line 9 will look like this (since you're not using $row any more:

    [php]
    $code = $code_check->code;

    There might be more, but with a little more testing and clean up I think you have it.

    Posted 12 years ago on Friday October 21, 2011 | Permalink
  9. If I remove those lines and only use this:

    $code = $code_check->code;

    It doesn't work. I am assuming it is because it is returning an array.

    Posted 12 years ago on Friday October 21, 2011 | Permalink
  10. Possibly. I didn't look too closely at it. If it works properly, it will be fine, even if inefficient. You can always fine tune it later.

    Posted 12 years ago on Friday October 21, 2011 | Permalink

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