<?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: Yet another data validation approach</title>
	<atom:link href="http://cakebaker.42dh.com/2006/02/06/yet-another-data-validation-approach/feed/" rel="self" type="application/rss+xml" />
	<link>http://cakebaker.42dh.com/2006/02/06/yet-another-data-validation-approach/</link>
	<description>baking cakes with CakePHP</description>
	<lastBuildDate>Sat, 13 Mar 2010 15:19:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: cakebaker</title>
		<link>http://cakebaker.42dh.com/2006/02/06/yet-another-data-validation-approach/comment-page-1/#comment-10707</link>
		<dc:creator>cakebaker</dc:creator>
		<pubDate>Thu, 16 Aug 2007 15:46:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=97#comment-10707</guid>
		<description>@austintx: Hm, usually the validation is done in the model, so I would try to validate your checkboxes in the model, too. 

I think it should be possible to validate it in the controller, but at the moment I don&#039;t see how (it is quite some time since I used this validation approach the last time).

HTH</description>
		<content:encoded><![CDATA[<p>@austintx: Hm, usually the validation is done in the model, so I would try to validate your checkboxes in the model, too. </p>
<p>I think it should be possible to validate it in the controller, but at the moment I don&#8217;t see how (it is quite some time since I used this validation approach the last time).</p>
<p>HTH</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: austintx</title>
		<link>http://cakebaker.42dh.com/2006/02/06/yet-another-data-validation-approach/comment-page-1/#comment-10658</link>
		<dc:creator>austintx</dc:creator>
		<pubDate>Mon, 13 Aug 2007 21:37:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=97#comment-10658</guid>
		<description>cakebaker, thanks so much for this.  very easy to use the model validation piece to this.

i can&#039;t figure out how to use this if i am trying to &quot;validate&quot; form data in my controller though.  it was mentioned above but it doesn&#039;t make sense to this newbie.

e.g.  i have 2 checkboxes where one has to be checked.  if both are checked or not checked i want to use showmessage to draw attention to it.  is that possible?  how can i do that in the controller?

thanks again for your work on this validation solution!</description>
		<content:encoded><![CDATA[<p>cakebaker, thanks so much for this.  very easy to use the model validation piece to this.</p>
<p>i can&#8217;t figure out how to use this if i am trying to &#8220;validate&#8221; form data in my controller though.  it was mentioned above but it doesn&#8217;t make sense to this newbie.</p>
<p>e.g.  i have 2 checkboxes where one has to be checked.  if both are checked or not checked i want to use showmessage to draw attention to it.  is that possible?  how can i do that in the controller?</p>
<p>thanks again for your work on this validation solution!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cakebaker</title>
		<link>http://cakebaker.42dh.com/2006/02/06/yet-another-data-validation-approach/comment-page-1/#comment-7733</link>
		<dc:creator>cakebaker</dc:creator>
		<pubDate>Sat, 21 Jul 2007 08:42:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=97#comment-7733</guid>
		<description>@Dia: Thanks for your addition! And yes, you are right, the example I showed is not that generic ;-)</description>
		<content:encoded><![CDATA[<p>@Dia: Thanks for your addition! And yes, you are right, the example I showed is not that generic ;-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dia</title>
		<link>http://cakebaker.42dh.com/2006/02/06/yet-another-data-validation-approach/comment-page-1/#comment-7718</link>
		<dc:creator>Dia</dc:creator>
		<pubDate>Fri, 20 Jul 2007 12:41:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=97#comment-7718</guid>
		<description>Hi,

First of all, thanks, Daniel, for this blog, which was and still is very usefull to me

I use the validation approach you described in this post with Cake 1.1

I know you posted it a long time ago now but I have a little complement to suggest

I needed in many models to valid fields with &quot;isMyFieldUnique&quot; functions

with your solution you have to put one method per field in every model

here is my solution to use a &quot;isUnique&quot; method in AppModel and how to pass it an array of arguments



in AppModel, function isUnique :

function isUnique($params) {
	$fieldName = $params[0];
	if ($this-&gt;{$this-&gt;primaryKey} == null) // add
        return (!$this-&gt;hasAny(array($this-&gt;name.&#039;.&#039;.$fieldName =&gt; $this-&gt;data[$this-&gt;name][$fieldName])));
    else // edit
        return (!$this-&gt;hasAny(array($this-&gt;name.&#039;.&#039;.$fieldName =&gt; $this-&gt;data[$this-&gt;name][$fieldName], $this-&gt;name.&#039;.&#039;.$this-&gt;primaryKey =&gt; &#039;!=&#039;.$this-&gt;data[$this-&gt;name][$this-&gt;primaryKey])));
}



in AppModel, lines to modify in your invalidFields function :

[...]
if (isset($validator[0])) {
if (is_array($validator[0])) {
	$function_name = $validator[0][0];
	$params = $validator[0][1];
	
	if (method_exists($this, $function_name)) {
		if (isset($data[$table][$field_name])
		and !call_user_func(array($this, $function_name), $params)) {
			if (!isset($errors[$field_name])) {
				$errors[$field_name] = isset ($validator[1]) ? $validator[1] : 1;
			}
		}
	}
} else {
	if (isset ($data[$table][$field_name]) &amp;&amp; !preg_match($validator[0], $data[$table][$field_name])) {
		if (!isset ($errors[$field_name])) {
			$errors[$field_name] = isset ($validator[1]) ? $validator[1] : 1;
		}
	}
}
}
[...]



using it in your model :

var $validate = array (
	&#039;my_field&#039; =&gt; array (
		array (VALID_NOT_EMPTY, &#039;Please enter something.&#039;),
		array (array(&#039;isUnique&#039;, array(&#039;my_field&#039;)), &#039;This name is already used.&#039;)
	)
);

$validator[0] has to be an array if you wanna call a callback function
first element : function name
second element : parameters array (can be empty or be of the size you need, dun care)


hope this comment could be usefull for even one person ^^</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>First of all, thanks, Daniel, for this blog, which was and still is very usefull to me</p>
<p>I use the validation approach you described in this post with Cake 1.1</p>
<p>I know you posted it a long time ago now but I have a little complement to suggest</p>
<p>I needed in many models to valid fields with &#8220;isMyFieldUnique&#8221; functions</p>
<p>with your solution you have to put one method per field in every model</p>
<p>here is my solution to use a &#8220;isUnique&#8221; method in AppModel and how to pass it an array of arguments</p>
<p>in AppModel, function isUnique :</p>
<p>function isUnique($params) {<br />
	$fieldName = $params[0];<br />
	if ($this-&gt;{$this-&gt;primaryKey} == null) // add<br />
        return (!$this-&gt;hasAny(array($this-&gt;name.&#8217;.&#8217;.$fieldName =&gt; $this-&gt;data[$this-&gt;name][$fieldName])));<br />
    else // edit<br />
        return (!$this-&gt;hasAny(array($this-&gt;name.&#8217;.&#8217;.$fieldName =&gt; $this-&gt;data[$this-&gt;name][$fieldName], $this-&gt;name.&#8217;.&#8217;.$this-&gt;primaryKey =&gt; &#8216;!=&#8217;.$this-&gt;data[$this-&gt;name][$this-&gt;primaryKey])));<br />
}</p>
<p>in AppModel, lines to modify in your invalidFields function :</p>
<p>[...]<br />
if (isset($validator[0])) {<br />
if (is_array($validator[0])) {<br />
	$function_name = $validator[0][0];<br />
	$params = $validator[0][1];</p>
<p>	if (method_exists($this, $function_name)) {<br />
		if (isset($data[$table][$field_name])<br />
		and !call_user_func(array($this, $function_name), $params)) {<br />
			if (!isset($errors[$field_name])) {<br />
				$errors[$field_name] = isset ($validator[1]) ? $validator[1] : 1;<br />
			}<br />
		}<br />
	}<br />
} else {<br />
	if (isset ($data[$table][$field_name]) &amp;&amp; !preg_match($validator[0], $data[$table][$field_name])) {<br />
		if (!isset ($errors[$field_name])) {<br />
			$errors[$field_name] = isset ($validator[1]) ? $validator[1] : 1;<br />
		}<br />
	}<br />
}<br />
}<br />
[...]</p>
<p>using it in your model :</p>
<p>var $validate = array (<br />
	&#8216;my_field&#8217; =&gt; array (<br />
		array (VALID_NOT_EMPTY, &#8216;Please enter something.&#8217;),<br />
		array (array(&#8216;isUnique&#8217;, array(&#8216;my_field&#8217;)), &#8216;This name is already used.&#8217;)<br />
	)<br />
);</p>
<p>$validator[0] has to be an array if you wanna call a callback function<br />
first element : function name<br />
second element : parameters array (can be empty or be of the size you need, dun care)</p>
<p>hope this comment could be usefull for even one person ^^</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: All About Validation in CakePHP 1.2 &#171; Another Cake Baker</title>
		<link>http://cakebaker.42dh.com/2006/02/06/yet-another-data-validation-approach/comment-page-1/#comment-7556</link>
		<dc:creator>All About Validation in CakePHP 1.2 &#171; Another Cake Baker</dc:creator>
		<pubDate>Tue, 03 Jul 2007 01:22:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=97#comment-7556</guid>
		<description>[...] and flexibility. This is evidenced by the number of alternatives that people have written such as Daniel Hofstetter, Evan Sagge and Adeel Khan&#8217;s ruby-esque [...]</description>
		<content:encoded><![CDATA[<p>[...] and flexibility. This is evidenced by the number of alternatives that people have written such as Daniel Hofstetter, Evan Sagge and Adeel Khan&#8217;s ruby-esque [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cakebaker</title>
		<link>http://cakebaker.42dh.com/2006/02/06/yet-another-data-validation-approach/comment-page-1/#comment-6429</link>
		<dc:creator>cakebaker</dc:creator>
		<pubDate>Sat, 05 May 2007 16:59:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=97#comment-6429</guid>
		<description>@chess64: How do you check it?</description>
		<content:encoded><![CDATA[<p>@chess64: How do you check it?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chess64</title>
		<link>http://cakebaker.42dh.com/2006/02/06/yet-another-data-validation-approach/comment-page-1/#comment-6425</link>
		<dc:creator>chess64</dc:creator>
		<pubDate>Fri, 04 May 2007 14:55:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=97#comment-6425</guid>
		<description>Checking for uniqueness doesn&#039;t seem to work if you are updating existing records...</description>
		<content:encoded><![CDATA[<p>Checking for uniqueness doesn&#8217;t seem to work if you are updating existing records&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim</title>
		<link>http://cakebaker.42dh.com/2006/02/06/yet-another-data-validation-approach/comment-page-1/#comment-6323</link>
		<dc:creator>Tim</dc:creator>
		<pubDate>Thu, 26 Apr 2007 09:02:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=97#comment-6323</guid>
		<description>Check here:

&lt;a href=&quot;http://php-coding-practices.com/cakephp-specific/the-validateerrors-issue/&quot; rel=&quot;nofollow&quot;&gt;The Validation-Errors issue&lt;/a&gt;.

It basically returns only the number of validation errors.</description>
		<content:encoded><![CDATA[<p>Check here:</p>
<p><a href="http://php-coding-practices.com/cakephp-specific/the-validateerrors-issue/" rel="nofollow">The Validation-Errors issue</a>.</p>
<p>It basically returns only the number of validation errors.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cakebaker</title>
		<link>http://cakebaker.42dh.com/2006/02/06/yet-another-data-validation-approach/comment-page-1/#comment-6077</link>
		<dc:creator>cakebaker</dc:creator>
		<pubDate>Sat, 31 Mar 2007 06:16:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=97#comment-6077</guid>
		<description>@Steve: Hm, what&#039;s in $this-&gt;validationErrors?</description>
		<content:encoded><![CDATA[<p>@Steve: Hm, what&#8217;s in $this-&gt;validationErrors?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://cakebaker.42dh.com/2006/02/06/yet-another-data-validation-approach/comment-page-1/#comment-6061</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Fri, 30 Mar 2007 19:55:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.cakebaker.42dh.com/?p=97#comment-6061</guid>
		<description>Great approach. 

I have a form where data from an associated model must also be validated. Let&#039;s say hypothetically I had a model called Post and another model called Comment. For the sake of this example I have one form that allows me to create a post and comment at the same time. 

So input fields would have something like Post/name and Comment/name ... etc. 

The Post and Comment models has it&#039;s respective validations.  When I print out the $errors variable in the App_Model class it will show errors for both if they don&#039;t validate. However, the errors helper doesn&#039;t print any errors for the associated model.</description>
		<content:encoded><![CDATA[<p>Great approach. </p>
<p>I have a form where data from an associated model must also be validated. Let&#8217;s say hypothetically I had a model called Post and another model called Comment. For the sake of this example I have one form that allows me to create a post and comment at the same time. </p>
<p>So input fields would have something like Post/name and Comment/name &#8230; etc. </p>
<p>The Post and Comment models has it&#8217;s respective validations.  When I print out the $errors variable in the App_Model class it will show errors for both if they don&#8217;t validate. However, the errors helper doesn&#8217;t print any errors for the associated model.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
