<?xml version="1.0" encoding="UTF-8"?><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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Extending CodeIgniter&#8217;s Validation Library To Check For Unique Values</title>
	<atom:link href="http://www.scottnelle.com/41/extending-codeigniters-validation-library/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scottnelle.com/41/extending-codeigniters-validation-library/</link>
	<description>Scott Nell&#233;&#039;s Personal Site</description>
	<lastBuildDate>Thu, 29 Jul 2010 20:18:32 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Carson Shold</title>
		<link>http://www.scottnelle.com/41/extending-codeigniters-validation-library/comment-page-1/#comment-6349</link>
		<dc:creator>Carson Shold</dc:creator>
		<pubDate>Thu, 29 Jul 2010 20:18:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottnelle.com/?p=41#comment-6349</guid>
		<description>Well that didn&#039;t work at all. The important parts are below

&lt;code&gt;
class My_Validation extends CI_Form_validation {

	function My_Validation()
	{
	    parent::CI_Form_validation();
	}
&lt;/code
and
&lt;code&gt;
function unique($str, $field)
	{
		$CI =&amp; get_instance();
		list($table, $column) = preg_split(&quot;&#124;\.&#124;&quot;, $field, 2);

		$CI-&gt;validation-&gt;set_message(&#039;unique&#039;, &#039;The %s that you requested is unavailable.&#039;);

		$query = $CI-&gt;db-&gt;query(&quot;SELECT COUNT(*) dupe FROM $table WHERE $column = &#039;$str&#039;&quot;);
		$row = $query-&gt;row();
		return ($row-&gt;dupe &gt; 0) ? FALSE : TRUE;
	}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Well that didn&#8217;t work at all. The important parts are below</p>
<p><code><br />
class My_Validation extends CI_Form_validation {</p>
<p>	function My_Validation()<br />
	{<br />
	    parent::CI_Form_validation();<br />
	}<br />
&lt;/code<br />
and<br />
</code><code><br />
function unique($str, $field)<br />
	{<br />
		$CI =&amp; get_instance();<br />
		list($table, $column) = preg_split("|\.|", $field, 2);</p>
<p>		$CI-&gt;validation-&gt;set_message('unique', 'The %s that you requested is unavailable.');</p>
<p>		$query = $CI-&gt;db-&gt;query("SELECT COUNT(*) dupe FROM $table WHERE $column = '$str'");<br />
		$row = $query-&gt;row();<br />
		return ($row-&gt;dupe &gt; 0) ? FALSE : TRUE;<br />
	}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carson Shold</title>
		<link>http://www.scottnelle.com/41/extending-codeigniters-validation-library/comment-page-1/#comment-6348</link>
		<dc:creator>Carson Shold</dc:creator>
		<pubDate>Thu, 29 Jul 2010 20:15:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottnelle.com/?p=41#comment-6348</guid>
		<description>This seems so simple but I just can&#039;t get it to work. My code is below, can you see what I&#039;ve done wrong? Thanks

&lt;code&gt;
validation-&gt;set_message(&#039;unique&#039;, &#039;The %s that you requested is unavailable.&#039;);

		$query = $CI-&gt;db-&gt;query(&quot;SELECT COUNT(*) dupe FROM $table WHERE $column = &#039;$str&#039;&quot;);
		$row = $query-&gt;row();
		return ($row-&gt;dupe &gt; 0) ? FALSE : TRUE;
	}
}
?&gt;
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>This seems so simple but I just can&#8217;t get it to work. My code is below, can you see what I&#8217;ve done wrong? Thanks</p>
<p><code><br />
validation-&gt;set_message('unique', 'The %s that you requested is unavailable.');</p>
<p>		$query = $CI-&gt;db-&gt;query("SELECT COUNT(*) dupe FROM $table WHERE $column = '$str'");<br />
		$row = $query-&gt;row();<br />
		return ($row-&gt;dupe &gt; 0) ? FALSE : TRUE;<br />
	}<br />
}<br />
?&gt;<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Peers</title>
		<link>http://www.scottnelle.com/41/extending-codeigniters-validation-library/comment-page-1/#comment-5411</link>
		<dc:creator>David Peers</dc:creator>
		<pubDate>Sat, 15 May 2010 21:04:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottnelle.com/?p=41#comment-5411</guid>
		<description>This blog aided me  in narrowing down some problems with the latest release candidate, Why do they often seem to leave out vital information when they release a new version? It may be trivial to them but not to me. I&#039;m sure i&#039;m not alone either.</description>
		<content:encoded><![CDATA[<p>This blog aided me  in narrowing down some problems with the latest release candidate, Why do they often seem to leave out vital information when they release a new version? It may be trivial to them but not to me. I&#8217;m sure i&#8217;m not alone either.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://www.scottnelle.com/41/extending-codeigniters-validation-library/comment-page-1/#comment-5118</link>
		<dc:creator>Brian</dc:creator>
		<pubDate>Sun, 28 Mar 2010 02:06:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottnelle.com/?p=41#comment-5118</guid>
		<description>Looks like the HTML code tags cut out most of the code...
The only changes needed are (as noted above):
Replace &quot;Validation&quot; with &quot;Form_validation&quot; and replace the split() call with: preg_split(&quot;&#124;\.&#124;&quot;, $field, 2);</description>
		<content:encoded><![CDATA[<p>Looks like the HTML code tags cut out most of the code&#8230;<br />
The only changes needed are (as noted above):<br />
Replace &#8220;Validation&#8221; with &#8220;Form_validation&#8221; and replace the split() call with: preg_split(&#8220;|\.|&#8221;, $field, 2);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://www.scottnelle.com/41/extending-codeigniters-validation-library/comment-page-1/#comment-5117</link>
		<dc:creator>Brian</dc:creator>
		<pubDate>Sun, 28 Mar 2010 02:02:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottnelle.com/?p=41#comment-5117</guid>
		<description>Thanks for the extension! Here is updated code that works with current versions of CI (1.7.2) and PHP(5.3):

&lt;code&gt;

form_validation-&gt;set_message(&#039;unique&#039;, &#039;The %s that you requested is already in use.&#039;);
			
			$query = $CI-&gt;db-&gt;query(&quot;SELECT COUNT(*) dupe FROM $table WHERE $column = &#039;$str&#039;&quot;);
			$row = $query-&gt;row();
			return ($row-&gt;dupe &gt; 0) ? FALSE : TRUE;
		}
	}
?&gt;

&lt;/code&gt;

Changes are:
CI: Validation is now Form_validation
PHP: split() has been deprecated. Use preg_split() with &#124; around the regex as delimiters.</description>
		<content:encoded><![CDATA[<p>Thanks for the extension! Here is updated code that works with current versions of CI (1.7.2) and PHP(5.3):</p>
<p><code></p>
<p>form_validation-&gt;set_message('unique', 'The %s that you requested is already in use.');</p>
<p>			$query = $CI-&gt;db-&gt;query("SELECT COUNT(*) dupe FROM $table WHERE $column = '$str'");<br />
			$row = $query-&gt;row();<br />
			return ($row-&gt;dupe &gt; 0) ? FALSE : TRUE;<br />
		}<br />
	}<br />
?&gt;</p>
<p></code></p>
<p>Changes are:<br />
CI: Validation is now Form_validation<br />
PHP: split() has been deprecated. Use preg_split() with | around the regex as delimiters.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nabajit roy</title>
		<link>http://www.scottnelle.com/41/extending-codeigniters-validation-library/comment-page-1/#comment-4447</link>
		<dc:creator>nabajit roy</dc:creator>
		<pubDate>Tue, 19 Jan 2010 19:05:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottnelle.com/?p=41#comment-4447</guid>
		<description>yes Sir,you are right ,I haven&#039;t thought about this issue .thanx for your advice.you r really great.....</description>
		<content:encoded><![CDATA[<p>yes Sir,you are right ,I haven&#8217;t thought about this issue .thanx for your advice.you r really great&#8230;..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Nelle</title>
		<link>http://www.scottnelle.com/41/extending-codeigniters-validation-library/comment-page-1/#comment-4434</link>
		<dc:creator>Scott Nelle</dc:creator>
		<pubDate>Sun, 17 Jan 2010 16:37:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottnelle.com/?p=41#comment-4434</guid>
		<description>I prefer to extend classes rather than modifying the originals because when codeigniter is updated the originals may be modified. When you extend a class you don&#039;t have to keep track of which modifications you&#039;ve made.</description>
		<content:encoded><![CDATA[<p>I prefer to extend classes rather than modifying the originals because when codeigniter is updated the originals may be modified. When you extend a class you don&#8217;t have to keep track of which modifications you&#8217;ve made.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nabajit roy</title>
		<link>http://www.scottnelle.com/41/extending-codeigniters-validation-library/comment-page-1/#comment-4433</link>
		<dc:creator>nabajit roy</dc:creator>
		<pubDate>Sun, 17 Jan 2010 16:19:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottnelle.com/?p=41#comment-4433</guid>
		<description>good but the folling is much easier.you dont need to write the complete class but just add the following function in &quot;system/libraries/Form_validation.php&quot; file ........

function unique($str, $field)
		{
		     //$this-&gt;CI
			//$CI =&amp; get_instance();
			list($table, $column) = split(&quot;\.&quot;, $field, 2);
			//$this-&gt;CI-&gt;form_validation-&gt;set_message(&#039;unique&#039;, &#039;sorry! user &#039;.$str.&#039; doesnot exist.&#039;);
			$query = $this-&gt;CI-&gt;db-&gt;query(&quot;SELECT COUNT(*) dupe FROM $table WHERE $column = &#039;$str&#039;&quot;);
			$row = $query-&gt;row();
			return ($row-&gt;dupe == 0) ? FALSE : TRUE;
		}


and then call the validation function from you form as follows.......

 $this-&gt;form_validation-&gt;set_rules(&#039;user_name&#039;, &#039;Admin User Name&#039;, &#039;required&#124;unique[prism_users.user_loginname]&#039;);

hows that plz rply thank......</description>
		<content:encoded><![CDATA[<p>good but the folling is much easier.you dont need to write the complete class but just add the following function in &#8220;system/libraries/Form_validation.php&#8221; file &#8230;&#8230;..</p>
<p>function unique($str, $field)<br />
		{<br />
		     //$this-&gt;CI<br />
			//$CI =&amp; get_instance();<br />
			list($table, $column) = split(&#8220;\.&#8221;, $field, 2);<br />
			//$this-&gt;CI-&gt;form_validation-&gt;set_message(&#8216;unique&#8217;, &#8216;sorry! user &#8216;.$str.&#8217; doesnot exist.&#8217;);<br />
			$query = $this-&gt;CI-&gt;db-&gt;query(&#8220;SELECT COUNT(*) dupe FROM $table WHERE $column = &#8216;$str&#8217;&#8221;);<br />
			$row = $query-&gt;row();<br />
			return ($row-&gt;dupe == 0) ? FALSE : TRUE;<br />
		}</p>
<p>and then call the validation function from you form as follows&#8230;&#8230;.</p>
<p> $this-&gt;form_validation-&gt;set_rules(&#8216;user_name&#8217;, &#8216;Admin User Name&#8217;, &#8216;required|unique[prism_users.user_loginname]&#8216;);</p>
<p>hows that plz rply thank&#8230;&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott NellÃ©</title>
		<link>http://www.scottnelle.com/41/extending-codeigniters-validation-library/comment-page-1/#comment-2641</link>
		<dc:creator>Scott NellÃ©</dc:creator>
		<pubDate>Tue, 16 Dec 2008 15:33:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottnelle.com/?p=41#comment-2641</guid>
		<description>I&#039;m not sure this is the right extension for checking the ID of something while editing, because I imagine in that case you would &lt;em&gt;want&lt;/em&gt; the ID to match an existing record, and this extension will throw an error if it exists. Let me know if I&#039;m not following you correctly.

You could add another function to this extension called &quot;exists()&quot; that works something like this:

&lt;pre&gt;&lt;code&gt;
function exists($str, $field)
{
	$CI =&amp; get_instance();
	list($table, $column) = split(&quot;\.&quot;, $field, 2);

	$CI-&gt;validation-&gt;set_message(&#039;exists&#039;, &quot;The %s you&#039;re looking for doesn&#039;t exist.&quot;);

	$query = $CI-&gt;db-&gt;query(&quot;SELECT COUNT(*) dupe FROM $table WHERE $column = &#039;$str&#039;&quot;);
	$row = $query-&gt;row();
	return ($row-&gt;dupe &gt; 0) ? TRUE : FALSE;
}
&lt;/code&gt;&lt;/pre&gt;

I haven&#039;t tested it, but it should work. It does the opposite of the unique function, returning an error if the id &lt;em&gt;doesn&#039;t&lt;/em&gt; exist.</description>
		<content:encoded><![CDATA[<p>I&#8217;m not sure this is the right extension for checking the ID of something while editing, because I imagine in that case you would <em>want</em> the ID to match an existing record, and this extension will throw an error if it exists. Let me know if I&#8217;m not following you correctly.</p>
<p>You could add another function to this extension called &#8220;exists()&#8221; that works something like this:</p>
<pre><code>
function exists($str, $field)
{
	$CI =&#038; get_instance();
	list($table, $column) = split("\.", $field, 2);

	$CI->validation->set_message('exists', "The %s you're looking for doesn't exist.");

	$query = $CI->db->query("SELECT COUNT(*) dupe FROM $table WHERE $column = '$str'");
	$row = $query->row();
	return ($row->dupe > 0) ? TRUE : FALSE;
}
</code></pre>
<p>I haven&#8217;t tested it, but it should work. It does the opposite of the unique function, returning an error if the id <em>doesn&#8217;t</em> exist.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tresloukadu</title>
		<link>http://www.scottnelle.com/41/extending-codeigniters-validation-library/comment-page-1/#comment-2638</link>
		<dc:creator>tresloukadu</dc:creator>
		<pubDate>Tue, 16 Dec 2008 12:23:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.scottnelle.com/?p=41#comment-2638</guid>
		<description>Hello, how unique function would work for an edit form? Because I need to send the id to check too.</description>
		<content:encoded><![CDATA[<p>Hello, how unique function would work for an edit form? Because I need to send the id to check too.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
