<?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>cakebaker &#187; validation</title>
	<atom:link href="http://cakebaker.42dh.com/tags/validation/feed/" rel="self" type="application/rss+xml" />
	<link>http://cakebaker.42dh.com</link>
	<description>baking cakes with CakePHP</description>
	<lastBuildDate>Tue, 20 Dec 2011 15:29:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Validation of local urls</title>
		<link>http://cakebaker.42dh.com/2009/01/28/validation-of-local-urls/</link>
		<comments>http://cakebaker.42dh.com/2009/01/28/validation-of-local-urls/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 16:43:55 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=1078</guid>
		<description><![CDATA[While working on NoseRub I noticed (once again) that the validation of urls in the form of &#8220;http://somename.localhost&#8221; fails when using CakePHP&#8217;s built-in url validation rule. To make such urls pass the validation we have to write a custom validation rule. As PHP (&#62;= 5.2.0) already provides a filter for urls we can use this [...]]]></description>
			<content:encoded><![CDATA[<p>While working on <a href="http://noserub.com">NoseRub</a> I noticed (once again) that the validation of urls in the form of &#8220;http://somename.localhost&#8221; fails when using CakePHP&#8217;s built-in url validation rule.</p>
<p>To make such urls pass the validation we have to write a custom validation rule. As PHP (&gt;= 5.2.0) already provides a <a href="http://www.php.net/manual/en/ref.filter.php">filter</a> for urls we can use this filter in our validation rule. And so the code in our model looks like:</p>
<pre>
<code>public $validate = array('url' =&gt; array('rule' =&gt; array('validateUrl')));

public function validateUrl($data) {
    return (filter_var($data['url'], FILTER_VALIDATE_URL)) ? true : false;
}</code>
</pre>
<p>I hope this workaround is useful for some of you.</p>
<p>PS: If you are a German-speaking baker, a new blog called <a href="http://cakery.de/">cakery</a> (started by <a href="https://twitter.com/mooose">Siegfried Hirsch</a>, the admin of the <a href="http://groups.google.com/group/cakephp-de">German CakePHP Google Group</a>) could be interesting for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2009/01/28/validation-of-local-urls/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The Model::validates() trap</title>
		<link>http://cakebaker.42dh.com/2008/11/05/the-modelvalidates-trap/</link>
		<comments>http://cakebaker.42dh.com/2008/11/05/the-modelvalidates-trap/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 09:56:29 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=928</guid>
		<description><![CDATA[One mistake I make from time to time is to try to validate my data in the following way: public function example() { $data = array('Project' =&#62; array('name' =&#62; 'Testproject')); if ($this-&#62;Project-&#62;validates($data)) { echo 'valid'; } else { echo 'invalid'; } } If you look at this snippet, it probably looks fine to you. And [...]]]></description>
			<content:encoded><![CDATA[<p>One mistake I make from time to time is to try to validate my data in the following way:</p>
<pre>
<code>public function example() {
    $data = array('Project' =&gt; array('name' =&gt; 'Testproject'));
    if ($this-&gt;Project-&gt;validates($data)) {
        echo 'valid';
    } else {
        echo 'invalid';
    }
}</code>
</pre>
<p>If you look at this snippet, it probably looks fine to you. And it is fine if you are using CakePHP 1.1. Unfortunately, in CakePHP 1.2 the behavior of this method has changed, and so the example above doesn&#8217;t work as you would expect (depending on the validation rules). For a while the parameter of Model::validates() was deprecated (see <a href="http://cakebaker.42dh.com/2007/01/06/parameter-for-modelvalidates-is-now-deprecated/">Parameter for Model::validates() is now deprecated</a>) and caused a warning if you used it. In the meantime the parameter is back with a new meaning&#8230;</p>
<p>Compare the API docs for the validates() methods. First the CakePHP 1.1 <a href="http://api.cakephp.org/1.1/class_model.html#16c08a6787a40c74393c28f048ae2f31">docs</a>:</p>
<pre>
<code>/**
 * Returns true if all fields pass validation, otherwise false.
 *
 * @param array $data POST data
 * @return boolean True if there are no errors
 * @access public
 */</code>
</pre>
<p>And now the <a href="http://api.cakephp.org/class_model.html#edac6ae5521be420df41894851ba1970">docs</a> for CakePHP 1.2:</p>
<pre>
<code>/**
 * Returns true if all fields pass validation.
 *
 * @param string $options An optional array of custom options to be made available in the beforeValidate callback
 * @return boolean True if there are no errors
 * @access public
 * @link http://book.cakephp.org/view/410/Validating-Data-from-the-Controller
 */</code>
</pre>
<p>The parameter of the method is no longer for the data you want to validate but for options for the beforeValidate() callback method. So the example from the beginning has to be changed in the following way to make it work as expected:</p>
<pre>
<code>public function example() {
    $data = array('Project' =&gt; array('name' =&gt; 'Testproject'));
    $this-&gt;Project-&gt;create($data);
    if ($this-&gt;Project-&gt;validates()) {
        echo 'valid';
    } else {
        echo 'invalid';
    }
}</code>
</pre>
<p>Happy baking :)</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/11/05/the-modelvalidates-trap/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>New validation rule: multiple</title>
		<link>http://cakebaker.42dh.com/2008/07/25/new-validation-rule-multiple/</link>
		<comments>http://cakebaker.42dh.com/2008/07/25/new-validation-rule-multiple/#comments</comments>
		<pubDate>Fri, 25 Jul 2008 15:52:53 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=637</guid>
		<description><![CDATA[With changeset 7353 a new validation rule (resp. its implementation) has been added to the core validation rules: multiple. It allows you to validate the data from SELECT elements. With the in parameter you can define the valid choices: public $validate = array('name' =&#62; array('rule' =&#62; array('multiple', array('in' =&#62; range(0, 10))))); And with using the [...]]]></description>
			<content:encoded><![CDATA[<p>With <a href="https://trac.cakephp.org/changeset/7353">changeset 7353</a> a new validation rule (resp. its implementation) has been added to the core validation rules: <strong>multiple</strong>. </p>
<p>It allows you to validate the data from SELECT elements. With the <strong>in</strong> parameter you can define the valid choices:</p>
<pre>
<code>public $validate = array('name' =&gt; array('rule' =&gt; array('multiple', array('in' =&gt; range(0, 10)))));</code>
</pre>
<p>And with using the <strong>min</strong> and <strong>max</strong> parameters you can specify the minimal resp. maximal number of non-zero choices the user has to select:</p>
<pre>
<code>public $validate = array('name' =&gt; array('rule' =&gt; array('multiple', array('min' =&gt; 1, 'max' =&gt; 2))));</code>
</pre>
<p>Happy baking :)</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/07/25/new-validation-rule-multiple/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Validation of optional fields with multiple validation rules</title>
		<link>http://cakebaker.42dh.com/2008/05/24/validation-of-optional-fields-with-multiple-validation-rules/</link>
		<comments>http://cakebaker.42dh.com/2008/05/24/validation-of-optional-fields-with-multiple-validation-rules/#comments</comments>
		<pubDate>Sat, 24 May 2008 14:33:29 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=606</guid>
		<description><![CDATA[Recently, I encountered an issue while trying to validate an optional field for which I defined multiple validation rules. It&#8217;s one of those issues I call a bug whereas for the core developers it is a feature&#8230; Let&#8217;s say we have an optional &#8220;nickname&#8221; field. Its value must either be empty or an alphanumeric string [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I encountered an issue while trying to validate an optional field for which I defined multiple validation rules. It&#8217;s one of those issues I call a bug whereas for the core developers it is a feature&#8230;</p>
<p>Let&#8217;s say we have an optional &#8220;nickname&#8221; field. Its value must either be empty or an alphanumeric string with a length between three and ten characters. We start with defining the validation rules:</p>
<pre>
public $validate = array('nickname' =&gt; array('length' =&gt; array('rule' =&gt; array('between', 3, 10)),
		                             'alpha' =&gt; array('rule' =&gt; array('alphaNumeric'))));
</pre>
<p>Those rules work fine. The only &#8220;problem&#8221; is that they reject an empty string as invalid. To allow an empty string we have to set the option &#8220;allowEmpty&#8221; to true. So the $validate array is now:</p>
<pre>
public $validate = array('nickname' =&gt; array('length' =&gt; array('rule' =&gt; array('between', 3, 10)),
		                             'alpha' =&gt; array('rule' =&gt; array('alphaNumeric')),
				             'allowEmpty' =&gt; true));
</pre>
<p>It looks correct, but as you might guess, it doesn&#8217;t work. An empty string still doesn&#8217;t pass the validation. </p>
<p>The problem is that &#8220;allowEmpty&#8221; has to be set on the first rule and not as a setting for the field. And so we have to change the $validate array to:</p>
<pre>
public $validate = array('nickname' =&gt; array('length' =&gt; array('rule' =&gt; array('between', 3, 10), 'allowEmpty' =&gt; true),
		                             'alpha' =&gt; array('rule' =&gt; array('alphaNumeric'))));
</pre>
<p>With this solution, an empty string is accepted as a valid value. It&#8217;s not really logical, but it works ;-)</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/05/24/validation-of-optional-fields-with-multiple-validation-rules/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Three new validation rules</title>
		<link>http://cakebaker.42dh.com/2008/05/21/three-new-validation-rules/</link>
		<comments>http://cakebaker.42dh.com/2008/05/21/three-new-validation-rules/#comments</comments>
		<pubDate>Wed, 21 May 2008 17:32:18 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[deprecated]]></category>
		<category><![CDATA[feature]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[validation]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=605</guid>
		<description><![CDATA[Recently, three new validation methods have been added to the core validation class and hence you can use three more validation rules in your models: boolean, inList, and time. boolean is self-explanatory: it validates whether a field contains a boolean value (i.e. 0, 1, &#8220;0&#8243;, &#8220;1&#8243;, false, or true). Example: var $validate = array('is_enabled' =&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, three new validation methods have been added to the core validation class and hence you can use three more validation rules in your models: boolean, inList, and time. </p>
<p><strong>boolean</strong> is self-explanatory: it validates whether a field contains a boolean value (i.e. 0, 1, &#8220;0&#8243;, &#8220;1&#8243;, false, or true). Example:</p>
<pre>
var $validate = array('is_enabled' =&gt; array('rule' =&gt; array('boolean')));
</pre>
<p><strong>inList</strong> ensures that the respective field only contains a value from the defined array. It is case-sensitive, i.e. if you use the rule from the example below, then the value &#8220;Red&#8221; would be invalid.</p>
<pre>
var $validate = array('color' =&gt; array('rule' =&gt; array('inList', array('red', 'green', 'blue'))));
</pre>
<p><strong>time</strong> determines whether a field contains a valid time value. It supports both the 24 hour format (e.g. 08:10:10) and the 12 hour format (e.g. 8:10:10am). The minutes and seconds are optional in both cases.</p>
<pre>
var $validate = array('starttime' =&gt; array('rule' =&gt; array('time')));
</pre>
<p>Another recent change affected View::renderElement(): it has been deprecated in favor of View::element():</p>
<pre>
$this-&gt;renderElement('my_element', array('param1' =&gt; 'a value'));

becomes

$this-&gt;element('my_element', array('param1' =&gt; 'a value'));
</pre>
<p>I don&#8217;t know why View::renderElement() was deprecated and not View::element(), as I think the method name &#8220;renderElement&#8221; is more expressive than &#8220;element&#8221;.</p>
<p>Anyway, happy baking :)</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/05/21/three-new-validation-rules/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Writing custom validation rules</title>
		<link>http://cakebaker.42dh.com/2008/05/04/writing-custom-validation-rules/</link>
		<comments>http://cakebaker.42dh.com/2008/05/04/writing-custom-validation-rules/#comments</comments>
		<pubDate>Sun, 04 May 2008 16:38:32 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=600</guid>
		<description><![CDATA[Sometimes the built-in validation rules are not flexible enough for your specific validation needs, and so you have to write your own validation rules. Fortunately, that&#8217;s quite easy. First, you have to write a method in your model which does the validation. The method must accept at least one parameter (for the value which should [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes the built-in validation rules are not flexible enough for your specific validation needs, and so you have to write your own validation rules. Fortunately, that&#8217;s quite easy.</p>
<p>First, you have to write a method in your model which does the validation. The method must accept at least one parameter (for the value which should be validated). If the data is valid, the method must return true. In the other case it has to return false. Such a method could look like:</p>
<pre>
public function validateNotEmpty($value) {
    if (trim($value[key($value)]) == '') {
        return false;
    } else {
        return true;
    }
}
</pre>
<p>One thing you have to notice is that $value is an array containing one key/value pair (in pre-beta releases it has been a single value). The key is the name of the field you validate, and the value contains what the user entered (and hence what you want to validate).</p>
<p>In the next, and last, step, we have to define that we want to use the method we just wrote to validate a certain field. This is done in the same way as with the built-in validation rules, the only difference is that we have to use the method name as rule name:</p>
<pre>
public $validate = array('name' =&gt; array('rule' =&gt; 'validateNotEmpty'));
</pre>
<p>You can also write validation rules with additional parameters. Let&#8217;s say you want to write a validation rule which validates whether a string contains a certain character. The validation method for this purpose may look like:</p>
<pre>
public function validateContainsChar($value, $char) {
    return (strpos($value[key($value)], $char) !== false);
}
</pre>
<p>Now, if we want to ensure that the value of the &#8220;name&#8221; field contains an &#8220;x&#8221;, then the $validate array is:</p>
<pre>
public $validate = array('name' =&gt; array('rule' =&gt; array('validateContainsChar', 'x')));
</pre>
<p>That&#8217;s it, happy validating!</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/05/04/writing-custom-validation-rules/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Parameter for Model::validates() is now deprecated</title>
		<link>http://cakebaker.42dh.com/2007/01/06/parameter-for-modelvalidates-is-now-deprecated/</link>
		<comments>http://cakebaker.42dh.com/2007/01/06/parameter-for-modelvalidates-is-now-deprecated/#comments</comments>
		<pubDate>Sat, 06 Jan 2007 09:55:29 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/2007/01/06/parameter-for-modelvalidates-is-now-deprecated/</guid>
		<description><![CDATA[Since changeset 4260 the following usage of Model::validates(), used to validate a model without saving it, is deprecated and causes a warning: if ($this-&#62;User-&#62;validates($this-&#62;data)) { // do something } It looks now: $this-&#62;User-&#62;data = $this-&#62;data; // or $this-&#62;data['User']; if ($this-&#62;User-&#62;validates()) { // do something } At the moment this change affects only those who use [...]]]></description>
			<content:encoded><![CDATA[<p>Since <a href="https://trac.cakephp.org/changeset/4260">changeset 4260</a> the following usage of Model::validates(), used to validate a model without saving it, is deprecated and causes a warning:</p>
<pre>
if ($this-&gt;User-&gt;validates($this-&gt;data)) { // do something }
</pre>
<p>It looks now:</p>
<pre>
$this-&gt;User-&gt;data = $this-&gt;data;  // or $this-&gt;data['User'];
if ($this-&gt;User-&gt;validates()) { // do something }
</pre>
<p>At the moment this change affects only those who use the Cake 1.2 branch from the repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2007/01/06/parameter-for-modelvalidates-is-now-deprecated/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Validation with CakePHP 1.2</title>
		<link>http://cakebaker.42dh.com/2007/01/03/validation-with-cakephp-12/</link>
		<comments>http://cakebaker.42dh.com/2007/01/03/validation-with-cakephp-12/#comments</comments>
		<pubDate>Wed, 03 Jan 2007 16:04:54 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/2007/01/03/validation-with-cakephp-12/</guid>
		<description><![CDATA[Up to now the validation support consisted of four constants (VALID_NOT_EMPTY, VALID_NUMBER, VALID_EMAIL, and VALID_YEAR), everything else you had to do yourself. The new validation class in CakePHP 1.2 is a bit more powerful ;-) The aforementioned constants still work in 1.2, but they are deprecated, so for new code you shouldn&#8217;t use them. The [...]]]></description>
			<content:encoded><![CDATA[<p>Up to now the validation support consisted of four constants (VALID_NOT_EMPTY, VALID_NUMBER, VALID_EMAIL, and VALID_YEAR), everything else you had to do yourself. The new validation class in CakePHP 1.2 is a bit more powerful ;-) </p>
<p>The aforementioned constants still work in 1.2, but they are deprecated, so for new code you shouldn&#8217;t use them. The usage of the new validation rules follows the following pattern (in your model):</p>
<pre>
var $validate = array('field' =&gt; array('rule' =&gt; array('validationmethod', 'param1', 'param2')));
</pre>
<p>Ok, let&#8217;s have a look at the validation methods.</p>
<p><strong>alphaNumeric</strong> should be self-explanatory, it allows only letters and strings. Useful for e.g. user names:</p>
<pre>
var $validate = array('username' =&gt; array('rule' =&gt; array('alphaNumeric')));
</pre>
<p><strong>between</strong> is a bit misleading, as I expected that it checks whether a number is in a certain range. But what it actually does is to check the string length. In the following example the length of the user name must be at least 3 characters, and maximal 10 characters:</p>
<pre>
var $validate = array('username' =&gt; array('rule' =&gt; array('between', 3, 10)));
</pre>
<p><strong>blank</strong> validates that a field is blank or contains only whitespace characters like tabs or spaces. It doesn&#8217;t look very useful to me. Its usage is:</p>
<pre>
var $validate = array('x' =&gt; array('rule' =&gt; array('blank')));
</pre>
<p><strong>cc</strong> doesn&#8217;t stand for &#8220;carbon copy&#8221;, it stands for &#8220;credit card&#8221;. It checks whether a credit card number is valid. If you want to check for Visa numbers, you would do the following:</p>
<pre>
var $validate = array('creditcard' =&gt; array('rule' =&gt; array('cc', array('visa'))));
</pre>
<p><strong>comparison</strong> is used to compare numeric values. It supports &#8220;is greater&#8221;, &#8220;is less&#8221;, &#8220;greater or equal&#8221;, &#8220;less or equal&#8221;, &#8220;is less&#8221;, &#8220;equal to&#8221;, and &#8220;not equal&#8221;. To check whether the provided age was higher than 18 you would do:</p>
<pre>
var $validate = array('age' =&gt; array('rule' =&gt; array('comparison', '&gt;=', 18)));
or
var $validate = array('age' =&gt; array('rule' =&gt; array('comparison', 'greater or equal', 18)));
</pre>
<p><strong>custom</strong> is also a bit misleading imho. It allows you to define a regular expression. </p>
<pre>
var $validate = array('username' =&gt; array('rule' =&gt; array('custom', '/^[0-9a-z]+$/i')));
</pre>
<p><strong>date</strong> checks for a valid date. It allows different formats, see the <a href="http://api.cakephp.org/1.2/classValidation.html#b83e69b014b61e2e42e967cdd7767d99">API</a> for details. To check whether the entered date is of the format yyyy-mm-dd resp. yy-mm-dd you would do:</p>
<pre>
var $validate = array('startdate' =&gt; array('rule' =&gt; array('date', 'ymd')));
</pre>
<p><strong>decimal</strong> checks for a valid decimal number. If you want to have decimal numbers with two digits after the dot you have to use:</p>
<pre>
var $validate = array('price' =&gt; array('rule' =&gt; array('decimal', 2)));
</pre>
<p>It is obvious, what <strong>email</strong> does: it checks for a valid email address:</p>
<pre>
var $validate = array('email' =&gt; array('rule' =&gt; array('email')));
</pre>
<p>I am not sure whether <strong>money</strong> is fully implemented yet, at least I was not able to use this validation rule.</p>
<p><strong>numeric</strong> is a simple wrapper for the PHP function <a href="http://php.net/is_numeric">is_numeric</a>:</p>
<pre>
var $validate = array('age' =&gt; array('rule' =&gt; array('numeric')));
</pre>
<p><strong>phone</strong> validates phone numbers. At the moment it supports only US phone numbers, if you want to validate other phone numbers you have to provide a regular expression.</p>
<pre>
var $validate = array('phone' =&gt; array('rule' =&gt; array('phone', null, 'us')));
</pre>
<p><strong>postal</strong> is used to validate zip codes from the U.S., Canada, and the U.K. For other zip codes you have to provide a regular expression.</p>
<pre>
var $validate = array('zipcode' =&gt; array('rule' =&gt; array('postal', null, 'us')));
</pre>
<p><strong>ssn</strong> validates social security numbers from the U.S., Denmark, and the Netherlands. For other social security numbers you have to provide a regular expression.</p>
<pre>
var $validate = array('ssn' =&gt; array('rule' =&gt; array('ssn', null, 'us')));
</pre>
<p><strong>url</strong> is used to validate urls.</p>
<pre>
var $validate = array('website' =&gt; array('rule' =&gt; array('url')));
</pre>
<p>If you look at the validation class you will notice several other public methods. <strong>equalTo</strong>, <strong>file</strong>, <strong>ip</strong>, <strong>minLength</strong>, <strong>maxLength</strong>, <strong>multiple</strong>, and <strong>number</strong> are validation rules which are not implemented yet, so I don&#8217;t have described them. The <strong>userDefined</strong> method looks like it allows you to create custom validation methods, but up to now I couldn&#8217;t figure out how to use it&#8230;</p>
<p>Thanks to Nate for his initial <a href="http://groups.google.com/group/cake-php/browse_thread/thread/786da77a8cdfb124/716b2f6e2e4f0c7d#716b2f6e2e4f0c7d">explanations</a> in the Google Groups.</p>
<p>Update 2007-01-06: <strong>minLength</strong> and <strong>maxLength</strong> have been implemented in <a href="https://trac.cakephp.org/changeset/4264">changeset 4264</a>. They can be used to validate the length of a string. If a string must consist of at least three characters you would do:</p>
<pre>
var $validate = array('username' =&gt; array('rule' =&gt; array('minLength', 3)));
</pre>
<p>And to validate that a string can consist of maximal 20 characters you do:</p>
<pre>
var $validate = array('username' =&gt; array('rule' =&gt; array('maxLength', 20)));
</pre>
<p>Update 2007-01-07: Also <strong>ip</strong> has been implemented, which is used to validate IP addresses. </p>
<pre>
var $validate = array('ip' =&gt; array('rule' =&gt; 'ip'));
</pre>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2007/01/03/validation-with-cakephp-12/feed/</wfw:commentRss>
		<slash:comments>141</slash:comments>
		</item>
		<item>
		<title>Automatic XHTML validation</title>
		<link>http://cakebaker.42dh.com/2006/07/05/automatic-xhtml-validation/</link>
		<comments>http://cakebaker.42dh.com/2006/07/05/automatic-xhtml-validation/#comments</comments>
		<pubDate>Wed, 05 Jul 2006 16:02:32 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=221</guid>
		<description><![CDATA[To ensure I write valid XHTML code I wrote a simple helper which does automatically validate the generated code. The helper uses the XHTML validator from the Akelos framework. This validator is labelled as &#8220;experimental&#8221;, so it is possible that some errors are not found. Maybe there exists a better XHTML validator somewhere else? The [...]]]></description>
			<content:encoded><![CDATA[<p>To ensure I write valid XHTML code I wrote a simple helper which does automatically validate the generated code. The helper uses the <a href="http://www.bermi.org/xhtml_validator">XHTML validator</a> from the <a href="http://www.bermi.org/projects/akelos_framework">Akelos framework</a>. This validator is labelled as &#8220;experimental&#8221;, so it is possible that some errors are not found. Maybe there exists a better XHTML validator somewhere else?</p>
<p>The usage is simple:</p>
<ol>
<li>Download the XHTML validator from <a href="http://www.bermi.org/xhtml_validator">http://www.bermi.org/xhtml_validator</a> and place it in the app/vendors directory</li>
<li>Copy the code below to app/views/helpers/xhtml_validator.php</li>
<li>Add the helper to the helpers array of your controller(s): var $helpers = array(&#8216;XhtmlValidator&#8217;);</li>
</ol>
<pre>
// app/views/helpers/xhtml_validator.php
vendor('XhtmlValidator');

class XhtmlValidatorHelper extends Helper
{
    function afterRender()
    {
        if (DEBUG &gt; 0)
        {
            $html = @ob_get_clean();
            $originalHtml = $html;
            ob_start();

            $XhtmlValidator = new XhtmlValidator();

            if($XhtmlValidator-&gt;validate($html) === false)
            {
                echo 'Ooops! There are some errors on the XHTML page';
                $XhtmlValidator-&gt;showErrors();
            }

            echo $originalHtml;
        }
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2006/07/05/automatic-xhtml-validation/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Once again an update of the advanced validation approach</title>
		<link>http://cakebaker.42dh.com/2006/05/11/once-again-an-update-of-the-advanced-validation-approach/</link>
		<comments>http://cakebaker.42dh.com/2006/05/11/once-again-an-update-of-the-advanced-validation-approach/#comments</comments>
		<pubDate>Thu, 11 May 2006 15:43:30 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=187</guid>
		<description><![CDATA[This is hopefully the last post about the advanced validation approach for a long time ;-) As the previous update this update is only for users using CakePHP with a version number higher than 1.0.1.2708 (you find the version number in VERSION.txt). And that is the update: replace the following snippet in the invalidFields() function [...]]]></description>
			<content:encoded><![CDATA[<p>This is hopefully the last post about the <a href="http://cakebaker.wordpress.com/2006/02/06/yet-another-data-validation-approach/">advanced validation approach</a> for a long time ;-)</p>
<p>As the <a href="http://cakebaker.wordpress.com/2006/05/09/a-small-update-of-the-advanced-validation-approach/">previous update</a> this update is only for users using CakePHP with a version number higher than 1.0.1.2708 (you find the version number in VERSION.txt). And that is the update: replace the following snippet in the invalidFields() function in your AppModel:</p>
<pre>
if ($data == null)
{
    if (isset($this-&gt;data))
    {
        $data = $this-&gt;data;
    }
    else
    {
        $data = array();
    }
}
</pre>
<p>with</p>
<pre>
if (isset($this-&gt;data))
{
    $data = array_merge($data, $this-&gt;data);
}
</pre>
<p>The reason for that update is that if you modify data in a possible beforeValidate() function, these changes are ignored by the invalidFields() function without this update.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2006/05/11/once-again-an-update-of-the-advanced-validation-approach/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

