<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Gravity Support Forums Topic: Validating an array with gform_validation</title>
		<link>https://legacy.forums.gravityhelp.com/topic/validating-an-array-with-gform_validation</link>
		<description>Gravity Support Forums Topic: Validating an array with gform_validation</description>
		<language>en-US</language>
		<pubDate>Sat, 04 Apr 2026 09:04:37 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.1</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>https://legacy.forums.gravityhelp.com/search.php</link>
		</textInput>
		<atom:link href="https://legacy.forums.gravityhelp.com/rss/topic/validating-an-array-with-gform_validation" rel="self" type="application/rss+xml" />

		<item>
			<title>Chris Hajer on "Validating an array with gform_validation"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/validating-an-array-with-gform_validation#post-38620</link>
			<pubDate>Fri, 21 Oct 2011 19:41:35 +0000</pubDate>
			<dc:creator>Chris Hajer</dc:creator>
			<guid isPermaLink="false">38620@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;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.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iamdangavin on "Validating an array with gform_validation"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/validating-an-array-with-gform_validation#post-38617</link>
			<pubDate>Fri, 21 Oct 2011 18:29:51 +0000</pubDate>
			<dc:creator>iamdangavin</dc:creator>
			<guid isPermaLink="false">38617@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;If I remove those lines and only use this:&#60;/p&#62;
&#60;p&#62;&#60;code&#62;$code = $code_check-&#38;gt;code;&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;It doesn't work. I am assuming it is because it is returning an array.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Chris Hajer on "Validating an array with gform_validation"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/validating-an-array-with-gform_validation#post-38604</link>
			<pubDate>Fri, 21 Oct 2011 17:07:54 +0000</pubDate>
			<dc:creator>Chris Hajer</dc:creator>
			<guid isPermaLink="false">38604@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;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.&#60;/p&#62;
&#60;p&#62;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:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;[php]
$code = $code_check-&#38;gt;code;&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;There might be more, but with a little more testing and clean up I think you have it.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iamdangavin on "Validating an array with gform_validation"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/validating-an-array-with-gform_validation#post-38601</link>
			<pubDate>Fri, 21 Oct 2011 16:38:03 +0000</pubDate>
			<dc:creator>iamdangavin</dc:creator>
			<guid isPermaLink="false">38601@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;Whew. This seems to be working great now. Thanks for talking me through that.&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;add_filter(&#38;#39;gform_validation&#38;#39;, &#38;#39;custom_validation&#38;#39;);
function custom_validation($validation_result){

    //supposing we don&#38;#39;t want input 1 to be a value of 86
    global $wpdb;
		$input = $_POST[&#38;#39;input_1&#38;#39;];
		$code_check = $wpdb-&#38;gt;get_results(&#38;quot;SELECT code FROM code_check WHERE code =&#38;quot;$input&#38;quot;&#38;quot;);
		foreach($code_check as $row){
			$code = $row-&#38;gt;code;
		} //end foreach
			if($code != $input){

		        // set the form validation to false
		        $validation_result[&#38;quot;is_valid&#38;quot;] = false;

		        //finding Field with ID of 1 and marking it as failed validation
		        foreach($validation_result[&#38;quot;form&#38;quot;][&#38;quot;fields&#38;quot;] as &#38;amp;$field){

		            //NOTE: replace 1 with the field you would like to validate
		            if($field[&#38;quot;id&#38;quot;] == &#38;quot;1&#38;quot;){
		                $field[&#38;quot;failed_validation&#38;quot;] = true;
		                $field[&#38;quot;validation_message&#38;quot;] = &#38;quot;Sorry, try again!&#38;quot;;
		                break;
		            } //end if
		        } //end foreach

		    } //end if
		    return $validation_result;

} //End Function&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;If you notice anything wrong here let me know please. Otherwise it only allows the entries in the database table now. &#60;/p&#62;
&#60;p&#62;Thanks!
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Chris Hajer on "Validating an array with gform_validation"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/validating-an-array-with-gform_validation#post-38564</link>
			<pubDate>Fri, 21 Oct 2011 13:38:40 +0000</pubDate>
			<dc:creator>Chris Hajer</dc:creator>
			<guid isPermaLink="false">38564@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;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.&#60;/p&#62;
&#60;p&#62;Also, you seem to be saying &#34;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.)&#60;/p&#62;
&#60;p&#62;It might just require a little more thought.  I think you almost have it.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iamdangavin on "Validating an array with gform_validation"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/validating-an-array-with-gform_validation#post-38562</link>
			<pubDate>Fri, 21 Oct 2011 13:25:12 +0000</pubDate>
			<dc:creator>iamdangavin</dc:creator>
			<guid isPermaLink="false">38562@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;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 &#34;AAA1&#34; in the database it won't let me enter that in the form. &#60;/p&#62;
&#60;p&#62;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...&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;add_filter(&#38;#39;gform_validation&#38;#39;, &#38;#39;custom_validation&#38;#39;);
function custom_validation($validation_result){

    //supposing we don&#38;#39;t want input 1 to be a value of 86
    global $wpdb;
    $codes = $wpdb-&#38;gt;get_results(&#38;quot;SELECT code FROM code_check&#38;quot;);
    foreach ($codes as $code) {
    if($_POST[&#38;#39;input_1&#38;#39;] != $code-&#38;gt;code ){

        // set the form validation to false
        $validation_result[&#38;quot;is_valid&#38;quot;] = false;

        //finding Field with ID of 1 and marking it as failed validation
        foreach($validation_result[&#38;quot;form&#38;quot;][&#38;quot;fields&#38;quot;] as &#38;amp;$field){

            //NOTE: replace 1 with the field you would like to validate
            if($field[&#38;quot;id&#38;quot;] == &#38;quot;1&#38;quot;){
                $field[&#38;quot;failed_validation&#38;quot;] = true;
                $field[&#38;quot;validation_message&#38;quot;] = &#38;quot;Sorry, try again!&#38;quot;;
                break;
            } //end if
        } //end foreach

    } //end if
    } // end foreach
    return $validation_result;

} //End Function&#60;/code&#62;&#60;/pre&#62;</description>
		</item>
		<item>
			<title>Chris Hajer on "Validating an array with gform_validation"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/validating-an-array-with-gform_validation#post-38556</link>
			<pubDate>Fri, 21 Oct 2011 12:52:37 +0000</pubDate>
			<dc:creator>Chris Hajer</dc:creator>
			<guid isPermaLink="false">38556@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;Are you sure the code is being processed?  Can you deliberately return a &#34;Good&#34; or &#34;Bad&#34; 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.&#60;/p&#62;
&#60;p&#62;Also, are you certain that input_1 is correct and $field['id'] is actually one?  I've been burned by that as well.&#60;/p&#62;
&#60;p&#62;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.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iamdangavin on "Validating an array with gform_validation"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/validating-an-array-with-gform_validation#post-38544</link>
			<pubDate>Fri, 21 Oct 2011 12:09:12 +0000</pubDate>
			<dc:creator>iamdangavin</dc:creator>
			<guid isPermaLink="false">38544@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;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. &#60;/p&#62;
&#60;p&#62;Just not sure how to do the database check within that hook.&#60;/p&#62;
&#60;p&#62;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.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Chris Hajer on "Validating an array with gform_validation"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/validating-an-array-with-gform_validation#post-38490</link>
			<pubDate>Fri, 21 Oct 2011 00:37:15 +0000</pubDate>
			<dc:creator>Chris Hajer</dc:creator>
			<guid isPermaLink="false">38490@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;What happens when you use that code?&#60;/p&#62;
&#60;p&#62;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?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iamdangavin on "Validating an array with gform_validation"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/validating-an-array-with-gform_validation#post-38454</link>
			<pubDate>Thu, 20 Oct 2011 16:13:35 +0000</pubDate>
			<dc:creator>iamdangavin</dc:creator>
			<guid isPermaLink="false">38454@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;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.&#60;/p&#62;
&#60;p&#62;I was hoping it was as simple as using the gform_validation to check against an array like so: &#60;/p&#62;
&#60;pre&#62;&#60;code&#62;add_filter(&#38;#39;gform_validation&#38;#39;, &#38;#39;custom_validation&#38;#39;);
function custom_validation($validation_result){

    //supposing we don&#38;#39;t want input 1 to be a value of 86

	$code = array(&#38;#39;aaa2&#38;#39;, &#38;#39;aaa3&#38;#39;, &#38;#39;aaa4&#38;#39;);
    if($_POST[&#38;#39;input_1&#38;#39;] == $code){

        // set the form validation to false
        $validation_result[&#38;quot;is_valid&#38;quot;] = false;

        //finding Field with ID of 1 and marking it as failed validation
        foreach($validation_result[&#38;quot;form&#38;quot;][&#38;quot;fields&#38;quot;] as &#38;amp;$field){

            //NOTE: replace 1 with the field you would like to validate
            if($field[&#38;quot;id&#38;quot;] == &#38;quot;1&#38;quot;){
                $field[&#38;quot;failed_validation&#38;quot;] = true;
                $field[&#38;quot;validation_message&#38;quot;] = &#38;quot;Sorry, try again!&#38;quot;;
                break;
            }
        }

    }

    return $validation_result;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;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
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
