<?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>Derilicious&#187; Posts tagged with Validation &#8211; Page  &#8211; Derilicious</title> <atom:link href="http://derickng.com/posts/tag/validation/feed" rel="self" type="application/rss+xml" /><link>http://derickng.com</link> <description>A blog on web development technicalities by Derick Ng</description> <lastBuildDate>Sun, 20 Dec 2009 12:18:03 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Multiple Validation Sets in CakePHP</title><link>http://derickng.com/posts/9-multiple-validation-sets-in-cakephp</link> <comments>http://derickng.com/posts/9-multiple-validation-sets-in-cakephp#comments</comments> <pubDate>Wed, 23 Jul 2008 15:19:26 +0000</pubDate> <dc:creator>Derick Ng</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[CakePHP]]></category> <category><![CDATA[Validation]]></category><guid
isPermaLink="false">http://derick.lyniq.com/?p=9</guid> <description><![CDATA[Jonathon Snook wrote about multiple validation sets in CakePHP and it reminded me how often we require rather different sets of validation rules for a single model. I have always enjoyed learning how others approached similar problems so I think I shall share mine too.
A very relevant example is the User model where we usually [...]]]></description> <content:encoded><![CDATA[<p>Jonathon Snook wrote about <a
href="http://snook.ca/archives/cakephp/multiple_validation_sets_cakephp/">multiple validation sets in CakePHP</a> and it reminded me how often we require rather different sets of validation rules for a single model. I have always enjoyed learning how others approached similar problems so I think I shall share mine too.</p><p>A very relevant example is the User model where we usually have multiple forms associated. E.g. sign up, profile updates, password reset, etc. Each form have a different set of fields while some share the same fields. My approach is to select a different set of fields for each &#8220;action&#8221; and modify the rules if required. This means I&#8217;ll keep a master set of rules for all the User model fields and call <code>setValidate() </code> for each &#8220;action&#8221;.</p><pre class="brush: php">
/**
 * Set the default validation rules here.
 *
 * @var array
 */
	protected $_validate = array(
		&#039;username&#039; =&gt; array(
			&#039;empty&#039; =&gt; array(
				&#039;required&#039; =&gt; true,
				&#039;rule&#039; =&gt; VALID_NOT_EMPTY,
				&#039;last&#039; =&gt; true
			),
			&#039;invalid&#039; =&gt; array(
				&#039;rule&#039; =&gt; &#039;/^[\w]{3,30}$/&#039;,
				&#039;last&#039; =&gt; true,
				&#039;on&#039; =&gt; &#039;create&#039;
			),
			&#039;exists&#039; =&gt; array(
				&#039;rule&#039; =&gt; &#039;validateUniqueUsername&#039;,
				&#039;last&#039; =&gt; true,
				&#039;on&#039; =&gt; &#039;create&#039;
			)
		),
		&#039;password&#039; =&gt; array(
			&#039;empty&#039; =&gt; array(
				&#039;required&#039; =&gt; true,
				&#039;rule&#039; =&gt; VALID_NOT_EMPTY,
				&#039;last&#039; =&gt; true
			),
			&#039;invalid&#039; =&gt; array(
				&#039;required&#039; =&gt; true,
				&#039;rule&#039; =&gt; &#039;/^.{6,30}$/&#039;,
				&#039;last&#039; =&gt; true
			)
		),
		...
	);
/**
 * Sets just enough fields for validation.
 *
 * @param array $fields List of field names to validate against. If no param
 *                      passed, all fields will be included.
 * @author Derick
 */
	function setValidate($fields = null) {
		if ($fields === null) {
			$this-&gt;validate = $this-&gt;_validate;
		} else {
			$this-&gt;validate = array();
			foreach ($fields as $f) {
				if (isset($this-&gt;_validate[$f])) {
					$this-&gt;validate[$f] = $this-&gt;_validate[$f];
				}
			}
		}
	}
</pre><p>This is a simplified example in my User model where I have the <code>register()</code> function to validate only the fields required for sign up and the <code>updateProfile()</code> function which uses another set of fields with slight different requirement.</p><pre class="brush: php">
function register($data) {
	$fields = array(&#039;username&#039;, &#039;password&#039;, ...);
	$this-&gt;setValidate($fields);
	$this-&gt;save($data, true, $fields);
}

function updateProfile($data) {
	$fields = array(&#039;password&#039;, ...);
	$this-&gt;setValidate($fields);

	// derick: allow user to left password field empty
	unset($this-&gt;validate[&#039;password&#039;][&#039;empty&#039;]);
	$this-&gt;validate[&#039;password&#039;][&#039;invalid&#039;][&#039;allowEmpty&#039;] = true;
	$this-&gt;save($data, true, $fields);

	// derick: you can also call this again to &quot;reset&quot; to the default set of
	// rules but usually we don&#039;t need this
	$this-&gt;setValidate();
}
</pre><p>In other words, I only update the <code>$validate</code> array in the model when required. Some might think it is dangerous since there is a chance of saving data without validation but this works for me. What do the rest of you do?</p> ]]></content:encoded> <wfw:commentRss>http://derickng.com/posts/9-multiple-validation-sets-in-cakephp/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching 7/14 queries in 0.003 seconds using disk

Served from: derickng.com @ 2012-02-05 04:17:12 -->
