<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: How to validate HABTM data&#8230;</title>
	<atom:link href="http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/feed/" rel="self" type="application/rss+xml" />
	<link>http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/</link>
	<description></description>
	<lastBuildDate>Wed, 11 Nov 2009 14:36:41 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: teknoid</title>
		<link>http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/#comment-1593</link>
		<dc:creator>teknoid</dc:creator>
		<pubDate>Thu, 16 Apr 2009 16:19:35 +0000</pubDate>
		<guid isPermaLink="false">http://teknoid.wordpress.com/?p=328#comment-1593</guid>
		<description>@Javier 

No you are right, the original approach lacked some elegance... and had some flaws. Too bad I didn&#039;t have much time to come back to it and test out some other things.</description>
		<content:encoded><![CDATA[<p>@Javier </p>
<p>No you are right, the original approach lacked some elegance&#8230; and had some flaws. Too bad I didn&#8217;t have much time to come back to it and test out some other things.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Javier</title>
		<link>http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/#comment-1590</link>
		<dc:creator>Javier</dc:creator>
		<pubDate>Thu, 16 Apr 2009 09:16:35 +0000</pubDate>
		<guid isPermaLink="false">http://teknoid.wordpress.com/?p=328#comment-1590</guid>
		<description>Interesting debate.

There&#039;s one thing I don&#039;t like in the original approach:

&lt;em&gt;We are about to save a new Post... let’s add a validation rule to our Tag model&lt;/em&gt;

So, we want the *Post* to have at least one tag. We should be validating the Post model. Maybe there are 3 others models with a HABTM relationship with Tag, and their validation rules are different.

Of course I don&#039;t have a better idea; I&#039;m just critizicing :-).

What I&#039;ve been using are manual validations in the controller or using the beforevalidate() method, but those have already been pointed out.</description>
		<content:encoded><![CDATA[<p>Interesting debate.</p>
<p>There&#8217;s one thing I don&#8217;t like in the original approach:</p>
<p><em>We are about to save a new Post&#8230; let’s add a validation rule to our Tag model</em></p>
<p>So, we want the *Post* to have at least one tag. We should be validating the Post model. Maybe there are 3 others models with a HABTM relationship with Tag, and their validation rules are different.</p>
<p>Of course I don&#8217;t have a better idea; I&#8217;m just critizicing :-).</p>
<p>What I&#8217;ve been using are manual validations in the controller or using the beforevalidate() method, but those have already been pointed out.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: teknoid</title>
		<link>http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/#comment-1403</link>
		<dc:creator>teknoid</dc:creator>
		<pubDate>Tue, 10 Mar 2009 18:22:15 +0000</pubDate>
		<guid isPermaLink="false">http://teknoid.wordpress.com/?p=328#comment-1403</guid>
		<description>@visskiss 

Secondary model knowing about primary model, is very much cake-like... the fact that it doesn&#039;t validate the whole form is definitely a problem. Well, it sparked some good conversation and solutions so it was worthwhile to write about it, I guess :)

And... thank you for sharing your solution as well.</description>
		<content:encoded><![CDATA[<p>@visskiss </p>
<p>Secondary model knowing about primary model, is very much cake-like&#8230; the fact that it doesn&#8217;t validate the whole form is definitely a problem. Well, it sparked some good conversation and solutions so it was worthwhile to write about it, I guess :)</p>
<p>And&#8230; thank you for sharing your solution as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: visskiss</title>
		<link>http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/#comment-1402</link>
		<dc:creator>visskiss</dc:creator>
		<pubDate>Tue, 10 Mar 2009 16:10:30 +0000</pubDate>
		<guid isPermaLink="false">http://teknoid.wordpress.com/?p=328#comment-1402</guid>
		<description>Hi Again,

Further to my previous comment, I think the best method for validating the data is simply to do this:

if(empty($this-&gt;data[&#039;Tag&#039;][&#039;Tag&#039;])) {  
	$this-&gt;Post-&gt;Tag-&gt;invalidate(&#039;Tag&#039;,&#039;min_tags&#039;);
}

Then proceed with a 

$this-&gt;Post-&gt;save($this-&gt;data); 

to validate the rest of the form.  

It&#039;s simpler, requires no hacks and is much shorter...</description>
		<content:encoded><![CDATA[<p>Hi Again,</p>
<p>Further to my previous comment, I think the best method for validating the data is simply to do this:</p>
<p>if(empty($this-&gt;data['Tag']['Tag'])) {<br />
	$this-&gt;Post-&gt;Tag-&gt;invalidate(&#8216;Tag&#8217;,'min_tags&#8217;);<br />
}</p>
<p>Then proceed with a </p>
<p>$this-&gt;Post-&gt;save($this-&gt;data); </p>
<p>to validate the rest of the form.  </p>
<p>It&#8217;s simpler, requires no hacks and is much shorter&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: visskiss</title>
		<link>http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/#comment-1401</link>
		<dc:creator>visskiss</dc:creator>
		<pubDate>Tue, 10 Mar 2009 15:58:26 +0000</pubDate>
		<guid isPermaLink="false">http://teknoid.wordpress.com/?p=328#comment-1401</guid>
		<description>Hi Teknoid,

This works, but it&#039;s true that it doesn&#039;t validate the whole form at once.  

It&#039;s also...un-cakelike ;-)  meaning the secondary model has to &#039;know&#039; about the primary model.  Which kind of defeats the point, no?

There MUST be a better way.  I tried fooling with multiple, but didn&#039;t get ANYWHERE.

PS your blog seems to pop up whenever I have a cake issue and do a search...</description>
		<content:encoded><![CDATA[<p>Hi Teknoid,</p>
<p>This works, but it&#8217;s true that it doesn&#8217;t validate the whole form at once.  </p>
<p>It&#8217;s also&#8230;un-cakelike ;-)  meaning the secondary model has to &#8216;know&#8217; about the primary model.  Which kind of defeats the point, no?</p>
<p>There MUST be a better way.  I tried fooling with multiple, but didn&#8217;t get ANYWHERE.</p>
<p>PS your blog seems to pop up whenever I have a cake issue and do a search&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yien Bin</title>
		<link>http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/#comment-1382</link>
		<dc:creator>Yien Bin</dc:creator>
		<pubDate>Thu, 05 Mar 2009 07:11:09 +0000</pubDate>
		<guid isPermaLink="false">http://teknoid.wordpress.com/?p=328#comment-1382</guid>
		<description>Hi, I would really like to see some examples about Validation::multiple(). It just simply doesn&#039;t work for me.</description>
		<content:encoded><![CDATA[<p>Hi, I would really like to see some examples about Validation::multiple(). It just simply doesn&#8217;t work for me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: teknoid</title>
		<link>http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/#comment-1379</link>
		<dc:creator>teknoid</dc:creator>
		<pubDate>Wed, 04 Mar 2009 20:36:17 +0000</pubDate>
		<guid isPermaLink="false">http://teknoid.wordpress.com/?p=328#comment-1379</guid>
		<description>@Joel Pearson

Excellent, thanks for sharing.</description>
		<content:encoded><![CDATA[<p>@Joel Pearson</p>
<p>Excellent, thanks for sharing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joel Pearson</title>
		<link>http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/#comment-1378</link>
		<dc:creator>Joel Pearson</dc:creator>
		<pubDate>Wed, 04 Mar 2009 07:30:22 +0000</pubDate>
		<guid isPermaLink="false">http://teknoid.wordpress.com/?p=328#comment-1378</guid>
		<description>I found a problem with my post above.

This line:
$validates = $validates &amp;&amp; $this-&gt;Post-&gt;validates();

Should be:
$validates = $this-&gt;Post-&gt;validates() &amp;&amp; $validates;

Otherwise the $this-&gt;Post-&gt;validates() function won&#039;t run, because PHP short-circuits the operation.</description>
		<content:encoded><![CDATA[<p>I found a problem with my post above.</p>
<p>This line:<br />
$validates = $validates &amp;&amp; $this-&gt;Post-&gt;validates();</p>
<p>Should be:<br />
$validates = $this-&gt;Post-&gt;validates() &amp;&amp; $validates;</p>
<p>Otherwise the $this-&gt;Post-&gt;validates() function won&#8217;t run, because PHP short-circuits the operation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joel Pearson</title>
		<link>http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/#comment-1377</link>
		<dc:creator>Joel Pearson</dc:creator>
		<pubDate>Wed, 04 Mar 2009 07:15:28 +0000</pubDate>
		<guid isPermaLink="false">http://teknoid.wordpress.com/?p=328#comment-1377</guid>
		<description>In regards to the question from @James some months ago about only doing partial validations when there are only validation errors in Tag, this is what I came up with.


function add() {

   if(!empty($this-&gt;data)) {
   
     $this-&gt;Post-&gt;Tag-&gt;set($this-&gt;data);
     $validates = $this-&gt;Post-&gt;Tag-&gt;validates();
     
     $this-&gt;Post-&gt;set($this-&gt;data);
     $validates = $validates &amp;&amp; $this-&gt;Post-&gt;validates();
     
     if($validates) {
         // Don&#039;t revalidate the Post model because we have already validated earlier
         $this-&gt;Post-&gt;save($this-&gt;data, false);
     }
   }

   $this-&gt;set(&#039;tags&#039;, $this-&gt;Post-&gt;Tag-&gt;find(&#039;list&#039;, array(&#039;fields&#039;=&gt;array(&#039;id&#039;, &#039;tag&#039;))));
}


This way everything is validated before trying to save, instead of only validating posts when the Tag model validates ok.</description>
		<content:encoded><![CDATA[<p>In regards to the question from @James some months ago about only doing partial validations when there are only validation errors in Tag, this is what I came up with.</p>
<p>function add() {</p>
<p>   if(!empty($this-&gt;data)) {</p>
<p>     $this-&gt;Post-&gt;Tag-&gt;set($this-&gt;data);<br />
     $validates = $this-&gt;Post-&gt;Tag-&gt;validates();</p>
<p>     $this-&gt;Post-&gt;set($this-&gt;data);<br />
     $validates = $validates &amp;&amp; $this-&gt;Post-&gt;validates();</p>
<p>     if($validates) {<br />
         // Don&#8217;t revalidate the Post model because we have already validated earlier<br />
         $this-&gt;Post-&gt;save($this-&gt;data, false);<br />
     }<br />
   }</p>
<p>   $this-&gt;set(&#8216;tags&#8217;, $this-&gt;Post-&gt;Tag-&gt;find(&#8216;list&#8217;, array(&#8216;fields&#8217;=&gt;array(&#8216;id&#8217;, &#8216;tag&#8217;))));<br />
}</p>
<p>This way everything is validated before trying to save, instead of only validating posts when the Tag model validates ok.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: brij bhushan</title>
		<link>http://teknoid.wordpress.com/2008/10/16/how-to-validate-habtm-data/#comment-873</link>
		<dc:creator>brij bhushan</dc:creator>
		<pubDate>Wed, 17 Dec 2008 11:20:59 +0000</pubDate>
		<guid isPermaLink="false">http://teknoid.wordpress.com/?p=328#comment-873</guid>
		<description>Yes,it works</description>
		<content:encoded><![CDATA[<p>Yes,it works</p>
]]></content:encoded>
	</item>
</channel>
</rss>
