<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Confessions of a Guru &#187; CodeIgniter</title>
	<atom:link href="http://www.guru.net.nz/blog/category/codeigniter/feed" rel="self" type="application/rss+xml" />
	<link>http://www.guru.net.nz/blog</link>
	<description>Random stuff from a Dunedin (NZ) based web developer, beer drinker and dad</description>
	<lastBuildDate>Wed, 14 Dec 2011 09:18:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>CodeIgniter &#8211; Form Validation and Optional Fields</title>
		<link>http://www.guru.net.nz/blog/2009/09/codeigniter-form-validation-and-optional-fields.html</link>
		<comments>http://www.guru.net.nz/blog/2009/09/codeigniter-form-validation-and-optional-fields.html#comments</comments>
		<pubDate>Tue, 22 Sep 2009 12:18:41 +0000</pubDate>
		<dc:creator>GuruBob</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.guru.net.nz/blog/?p=290</guid>
		<description><![CDATA[I&#8217;ve been doing a bit of work lately with CodeIgniter.  It&#8217;s been OK but the framework seems quite wrong in most places.  My main gripe is that the &#8220;helpers&#8221; define themselves as functions in the global namespace (rather than as objects that hook into the CodeIgniter object) which results in some stupidity like a function [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a bit of work lately with CodeIgniter.  It&#8217;s been OK but the framework seems quite wrong in most places.  My main gripe is that the &#8220;helpers&#8221; define themselves as functions in the global namespace (rather than as objects that hook into the CodeIgniter object) which results in some stupidity like a function called &#8220;set_value&#8221; which just happens to retrieve the value as validated by the form validation &#8220;helper&#8221;.  Anyway, that&#8217;s a lesson learnt.</p>
<p>I&#8217;ve been trying to get the form validation to allow me to use set_value for fields that aren&#8217;t required, but unless your rules specify the field and a validation rule for it the value won&#8217;t be available when set_value is called.  The trick here is to use &#8220;echo&#8221; as the rule which means that the validation passes and you get back what you put in.  This is not documented in the CodeIgniter manual (it&#8217;s implied by the fact that you can use any PHP function as a &#8220;rule&#8221;).</p>
<p>Example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$rules</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'field'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Name'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'rules'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'required'</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'field'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'nickname'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Nickname'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'rules'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'echo'</span>
  <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form_validation</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rules</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$rules</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this case any calls to set_value(&#8216;nickname&#8217;) will return the correct value, rather than an empty string.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guru.net.nz/blog/2009/09/codeigniter-form-validation-and-optional-fields.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

