<?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>Jon Lebensold &#187; demo</title>
	<atom:link href="http://lebensold.net/category/demo/feed" rel="self" type="application/rss+xml" />
	<link>http://lebensold.net</link>
	<description>thoughts on web development, technology and media</description>
	<lastBuildDate>Tue, 22 Jun 2010 16:04:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Zend with Flickr: random photos from users or groups</title>
		<link>http://lebensold.net/development/zend-with-flickr-random-photos-from-users-or-groups</link>
		<comments>http://lebensold.net/development/zend-with-flickr-random-photos-from-users-or-groups#comments</comments>
		<pubDate>Mon, 07 Jul 2008 23:15:25 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[free code]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/?p=94</guid>
		<description><![CDATA[I'm currently working on a project that involves pulling photos randomly. While my initial programmer instinct tells me I should write something that would enable my client to upload photos locally. However, there are some missed social networking opportunities when you decide to host your own images.
By putting the photos on Flickr, my client can [...]]]></description>
			<content:encoded><![CDATA[<p>I'm currently working on a project that involves pulling photos randomly. While my initial programmer instinct tells me I should write something that would enable my client to upload photos locally. However, there are some missed social networking opportunities when you decide to host your own images.</p>
<p>By putting the photos on Flickr, my client can keep posting photos and build a pool of images that other people can contribute to. From a development perspective, all I needed to do was leverage the Zend_Service_Flickr classes and write a simple wrapper.</p>
<p><iframe src="http://jon.lebensold.ca/demo/ZendFlickrRandomPhotos/" width="600" height="420"></iframe></p>
<p>The frontend page looks like this:</p>
<pre class="php">&nbsp;
<span style="color: #0000ff;">$randomPhotos</span> = <span style="color: #000000; font-weight: bold;">new</span> RandomPhotosFromFlickr<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'YOUR_API_KEY'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$numberOfPhotos</span> = <span style="color: #cc66cc;">3</span>;
&nbsp;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;
&lt;h2&gt;Group Photos&lt;/h2&gt;
&nbsp;
&quot;</span>;
<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$randomPhotos</span>-&gt;<span style="color: #006600;">FromGroup</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span>, <span style="color: #ff0000;">&quot;GROUP@ID&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$p</span><span style="color: #66cc66;">&#41;</span>
	<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;img src='&quot;</span>.<span style="color: #0000ff;">$p</span>-&gt;<span style="color: #006600;">Small</span>-&gt;<span style="color: #006600;">uri</span>.<span style="color: #ff0000;">&quot;' /&gt;&quot;</span>;
&nbsp;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;
&lt;h2&gt;User's Photos&lt;/h2&gt;
&nbsp;
&quot;</span>;
<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$randomPhotos</span>-&gt;<span style="color: #006600;">FromUser</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span>, <span style="color: #ff0000;">&quot;user@yahoo.com&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$p</span><span style="color: #66cc66;">&#41;</span>
	<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;img src='&quot;</span>.<span style="color: #0000ff;">$p</span>-&gt;<span style="color: #006600;">Small</span>-&gt;<span style="color: #006600;">uri</span>.<span style="color: #ff0000;">&quot;' /&gt;&quot;</span>;
&nbsp;</pre>
<p>In this example, $p is a <a href="http://framework.zend.com/manual/en/zend.service.flickr.html#zend.service.flickr.classes.image">Zend_Service_Flickr object</a>, so you can pull any kind of data while letting the simple class I wrote handle the selection of random photos:</p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> RandomPhotosFromFlickr
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/**
	 * API key for Flickr
	 *
	 * @var string
	 */</span>
	protected <span style="color: #0000ff;">$apiKey</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * photoset used for retrieving random photos
	 *
	 * @var Zend_Service_Flickr_ResultSet
	 */</span>
	protected <span style="color: #0000ff;">$currentPhotoset</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * number of photos in the current photoset
	 *
	 * @var integer
	 */</span>
	protected <span style="color: #0000ff;">$totalPhotoCount</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Class constructor
	 * We need your Flickr API Key
	 * Go to http://www.flickr.com/services/api/keys/apply/ to get one
	 * @param string $apiKey
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$apiKey</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">apiKey</span> = <span style="color: #0000ff;">$apiKey</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Supply a username (a user's email address) and the number of photos you'd like returned
	 *
	 * @param integer $numberOfPhotos
	 * @param string $username
	 * @return Zend_Service_Flickr_ResultSet
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FromUser<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span>, <span style="color: #0000ff;">$username</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$flickr</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Flickr<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">apiKey</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span> = <span style="color: #0000ff;">$flickr</span>-&gt;<span style="color: #006600;">userSearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$username</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span>-&gt;<span style="color: #006600;">totalResultsAvailable</span>;
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span> = <span style="color: #0000ff;">$flickr</span>-&gt;<span style="color: #006600;">userSearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$username</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'per_page'</span> =&gt; <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getPhotosFromCurrentPhotoset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Supply a group ID and the number of photos you'd like returned
	 *
	 * @param integer $numberOfPhotos
	 * @param string $groupId
	 * @return Zend_Service_Flickr_ResultSet
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FromGroup<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span>, <span style="color: #0000ff;">$groupId</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0000ff;">$flickr</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Flickr<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">apiKey</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span> = <span style="color: #0000ff;">$flickr</span>-&gt;<span style="color: #006600;">groupPoolGetPhotos</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$groupId</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span>-&gt;<span style="color: #006600;">totalResultsAvailable</span>;
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span> = <span style="color: #0000ff;">$flickr</span>-&gt;<span style="color: #006600;">groupPoolGetPhotos</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$groupId</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'per_page'</span> =&gt; <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getPhotosFromCurrentPhotoset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Return an integer within the photo index range
	 *
	 * @return integer
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getRandomPhotoIndex<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/rand"><span style="color: #000066;">rand</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span> , <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">totalPhotoCount</span> - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Get a photo object from the Flickr Web Service
	 * Provide an indice from the current photo set
	 *
	 * @param integer $index
	 * @return Zend_Service_Flickr_Image
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getPhoto<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$index</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span>-&gt;<span style="color: #006600;">seek</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$index</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">currentPhotoset</span>-&gt;<span style="color: #006600;">current</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * using $this-&gt;currentPhotoset, grab a collection of random photos
	 *
	 * @param integer $numberOfPhotos
	 * @return Zend_Service_Flickr_ResultSet
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getPhotosFromCurrentPhotoset<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$numberOfPhotos</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// first collect up a list of IDs (making sure there are no duplicates</span>
		<span style="color: #0000ff;">$photoIds</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$photoIds</span><span style="color: #66cc66;">&#41;</span> &lt; <span style="color: #0000ff;">$numberOfPhotos</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0000ff;">$id</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getRandomPhotoIndex</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>! <a href="http://www.php.net/in_array"><span style="color: #000066;">in_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span>, <span style="color: #0000ff;">$photoIds</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #0000ff;">$photoIds</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$id</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">$randomPhotos</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// query the web service to grab the photo objects</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$photoIds</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #0000ff;">$randomPhotos</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getPhoto</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$randomPhotos</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>you can <a href="http://jon.lebensold.ca/demo/ZendFlickrRandomPhotos/RandomPhotosFromFlickr.zip">download the full source</a> if you'd like to try it yourself. Please note that I've omitted bundling my code with a copy of the Zend Framework.</p>
<p>Using Zend Framework's Zend_Service_Flickr </p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/development/zend-with-flickr-random-photos-from-users-or-groups/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Decoupled validation with Zend_Form, Zend_Validate and Zend MVC</title>
		<link>http://lebensold.net/development/decoupled-validation-with-zend_form-zend_validate-and-zend-mvc</link>
		<comments>http://lebensold.net/development/decoupled-validation-with-zend_form-zend_validate-and-zend-mvc#comments</comments>
		<pubDate>Wed, 04 Jun 2008 19:56:32 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[free code]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/development/decoupled-validation-with-zend_form-zend_validate-and-zend-mvc</guid>
		<description><![CDATA[Lately I've been experimenting a lot with trying to serialize a form and send it over the wire as a means of doing asynchronous postback of partially validated forms. 
I've come up with a little prototype that's a PHP version of an ASP.NET implementation I did a couple months ago for a project I was [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I've been experimenting a lot with trying to serialize a form and send it over the wire as a means of doing asynchronous postback of partially validated forms. </p>
<p>I've come up with a little prototype that's a PHP version of an ASP.NET implementation I did a couple months ago for a project I was working on. </p>
<p>The PHP version was surprisingly easier to implement, due to the simplicity of Zend_Form when compared to the bulldozer we-control-everything-and-rewrite-your-user-control-id's approach of ASP.NET forms.</p>
<p>Essentially, I've created a class with a simple form (1 lookup list (one-to-many for the data modelers out there) and a couple of simple textboxes).</p>
<p>While I think that it's crucial to decouple business rules from the actual form implementation, I think that Zend_Validate validators, as well as custom validators, are an excellent way of creating testable, declaritive validation rules that can be applied to form elements as you see fit. The other decoupling you'll find in this example is from the postback model, essentially allowing you to drop the same form into any part of a Zend Framework application and posting to an Ajax Controller service for validation purposes. What's nice about this is that no business rules are left on the client. I've used prototype for a little styling.</p>
<p>The finishing touch in all of this is that I can leverage the server-side validation routine for both the asynchronous and synchronous calls, so that postback and validation run through the same validation code, essentially providing a second interface with to the same form object.</p>
<p>I'm going to look into extending this example to provide not only one-to-one mapping and one-to-many mapping (lookup lists), but also many-to-one mapping (generating sub-forms dynamically and performing partial validation). Not sure exactly how to do that, so comments would be greatly appreciated!</p>
<p>Here's a quick demonstration.<br />
<iframe src="http://jon.lebensold.ca/demo/decoupled-validation/html/" width="550" height="450" border="0"></iframe></p>
<p>and now for the code. The webservice bit is all contained in the IndexController for the sake of brevity. In a full blown application, you would likely have a Controller (or several controllers) that would offer different asynchronous entry points into your MVC framework.</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> IndexController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">f</span> = <span style="color: #000000; font-weight: bold;">new</span> RegistrationForm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">setMode</span> = <span style="color: #ff0000;">&quot;setMode($('wsTab').childNodes[0]);&quot;</span>;
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">f</span>-&gt;<span style="color: #006600;">isValid</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_POST</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">render</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;FormSuccess&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_request-&gt;<span style="color: #006600;">isPost</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">errorElements</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">f</span>-&gt;<span style="color: #006600;">getMessages</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">view</span>-&gt;<span style="color: #006600;">setMode</span> = <span style="color: #ff0000;">&quot;setMode($('pbTab').childNodes[0]);&quot;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * Ajax JSON call is made through this controller
	 *
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> asyncAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//prepare a JSON response</span>
		<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">viewRenderer</span>-&gt;<span style="color: #006600;">setNoRender</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">getHelper</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'layout'</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">disableLayout</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
     	<span style="color: #808080; font-style: italic;">//Map the form from the client-side call</span>
     	<span style="color: #0000ff;">$myFormData</span> = Zend_Json::<span style="color: #006600;">decode</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">getParam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fs&quot;</span><span style="color: #66cc66;">&#41;</span> , Zend_Json::<span style="color: #006600;">TYPE_ARRAY</span><span style="color: #66cc66;">&#41;</span>;
     	<span style="color: #0000ff;">$form</span> = <span style="color: #000000; font-weight: bold;">new</span> RegistrationForm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
     	<span style="color: #0000ff;">$form</span>-&gt;<span style="color: #006600;">isValid</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$myFormData</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
     	<span style="color: #808080; font-style: italic;">//return the result</span>
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getResponse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Content-Type'</span>, <span style="color: #ff0000;">'application/json'</span><span style="color: #66cc66;">&#41;</span>
							   -&gt;<span style="color: #006600;">setBody</span><span style="color: #66cc66;">&#40;</span>Zend_Json::<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$form</span>-&gt;<span style="color: #006600;">getMessages</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>You'll notice that I've pulled out the form into its own class (RegistrationForm): </p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> RegistrationForm <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
    	<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setAttrib</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id&quot;</span> , <span style="color: #ff0000;">&quot;frm&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    	<span style="color: #0000ff;">$onetomany</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Select<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'sad'</span><span style="color: #66cc66;">&#41;</span>;
    	<span style="color: #0000ff;">$onetomany</span>-&gt;<span style="color: #006600;">setLabel</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'One to many:'</span><span style="color: #66cc66;">&#41;</span>
    			  -&gt;<span style="color: #006600;">setMultiOptions</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getOneToMany</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    			  -&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Onetomany<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$usernameRequiredValidator</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_NotEmpty<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$usernameRequiredValidator</span>-&gt;<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Please enter a username.&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$usernameStringLengthValidator</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_StringLength<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span>,<span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$usernameStringLengthValidator</span>-&gt;<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Username is too short! (8 character minimum)&quot;</span>, Zend_Validate_StringLength::<span style="color: #006600;">TOO_SHORT</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$usernameStringLengthValidator</span>-&gt;<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Username is too long! (less than 20 characters)&quot;</span>, Zend_Validate_StringLength::<span style="color: #006600;">TOO_LONG</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    	<span style="color: #0000ff;">$username</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'username'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$username</span>-&gt;<span style="color: #006600;">setLabel</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Username:'</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$usernameRequiredValidator</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$usernameStringLengthValidator</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">setRequired</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    	<span style="color: #0000ff;">$name</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$name</span> -&gt;<span style="color: #006600;">setLabel</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Name:'</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'StringLength'</span>, <span style="color: #000000; font-weight: bold;">true</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        		 -&gt;<span style="color: #006600;">setRequired</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;    	
&nbsp;
        <span style="color: #0000ff;">$password</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Password<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'password'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$password</span>-&gt;<span style="color: #006600;">setLabel</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'password:'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">addElements</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        	<span style="color: #0000ff;">$onetomany</span>,
            <span style="color: #0000ff;">$username</span>,
            <span style="color: #0000ff;">$name</span>,
            <span style="color: #0000ff;">$password</span>
&nbsp;
        <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setDecorators</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        	<span style="color: #ff0000;">'FormElements'</span>,
            <span style="color: #ff0000;">'Fieldset'</span>,
            <span style="color: #ff0000;">'Form'</span>
&nbsp;
        <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getElements</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$element</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
        	<span style="color: #0000ff;">$element</span>-&gt;<span style="color: #006600;">setDecorators</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
                     <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ViewHelper'</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'class'</span> =&gt; <span style="color: #ff0000;">'formText'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
                     <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Label'</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'class'</span> =&gt; <span style="color: #ff0000;">'label'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
            		 <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'HtmlTag'</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'tag'</span> =&gt; <span style="color: #ff0000;">'dd'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #0000ff;">$element</span>-&gt;<span style="color: #006600;">class</span> = <span style="color: #ff0000;">'formText'</span>;
&nbsp;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getOneToMany<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$ar</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;please select&quot;</span>;
		<span style="color: #0000ff;">$ar</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;car&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;car&quot;</span>;
		<span style="color: #0000ff;">$ar</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;rollerBlade&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;roller blades&quot;</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$ar</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>In my post about enterprise validation, I described a scenario where you would create custom validators that could be applied declaratively in C# using the Enterprise Library. The same thinking was applied here with the One To Many by creating a custom validator. This allows you to test the validation (and your business rules) without thinking about the form, the UI or even session:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> Onetomany <span style="color: #000000; font-weight: bold;">extends</span> Zend_Validate_Abstract
<span style="color: #66cc66;">&#123;</span>
    const NOT_VALID  = <span style="color: #ff0000;">'stringLengthTooLong'</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @var array
     */</span>
    protected <span style="color: #0000ff;">$_messageTemplates</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        self::<span style="color: #006600;">NOT_VALID</span> =&gt; <span style="color: #ff0000;">&quot;one to many was not valid choice&quot;</span>,
&nbsp;
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * @var array
     */</span>
    protected <span style="color: #0000ff;">$_messageVariables</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <span style="color: #ff0000;">'not_valid'</span> =&gt; <span style="color: #ff0000;">'_not_valid'</span>,
&nbsp;
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    protected <span style="color: #0000ff;">$_not_valid</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * Returns the min option
     *
     * @return integer
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getNotValid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;_not_valid;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * Defined by Zend_Validate_Interface
     * @param  string $value
     * @return boolean
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isValid<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// more business rules would go somewhere here...</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$value</span> == <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #0000ff;">$this</span>-&gt;_error<span style="color: #66cc66;">&#40;</span>self::<span style="color: #006600;">NOT_VALID</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_messages<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
        <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The last bit of magic is the Javascript. My weapon of choice (as mentioned above) is prototype, however I'm sure you could do the same thing with any other javascript framework:</p>
<pre class="javascript">Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span>window , <span style="color: #3366CC;">&quot;load&quot;</span> , <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
		&lt;?= $this-&gt;setMode ?&gt;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">function</span> attachWebServiceObserver<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> url = <span style="color: #3366CC;">&quot;/index/async/&quot;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> f = JSON.<span style="color: #006600;">stringify</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'frm'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">serialize</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #006600;">Request</span><span style="color: #66cc66;">&#40;</span>url , <span style="color: #66cc66;">&#123;</span>
			method: <span style="color: #3366CC;">'post'</span>,
			parameters: <span style="color: #3366CC;">'fs='</span> + f,
			onComplete : doAjaxCall.<span style="color: #006600;">bindAsEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> attachPostBackObserver<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'frm'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">submit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//this ID comes from the RegistrationForm class</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> setMode<span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.tab'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>emt<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> emt.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span>background : <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	e.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>background :<span style="color: #3366CC;">&quot;#FFFF99&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!FIRST_LOAD<span style="color: #66cc66;">&#41;</span>
		clearErrors<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>e.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">id</span> ==<span style="color: #3366CC;">&quot;wsTab&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span>, <span style="color: #3366CC;">'click'</span>, attachWebServiceObserver<span style="color: #66cc66;">&#41;</span>;
		Event.<span style="color: #006600;">stopObserving</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span>, <span style="color: #3366CC;">'click'</span>, attachPostBackObserver<span style="color: #66cc66;">&#41;</span>;
		$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span> = <span style="color: #3366CC;">&quot;Submit Asynchronously&quot;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>e.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">id</span> ==<span style="color: #3366CC;">&quot;pbTab&quot;</span> <span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		Event.<span style="color: #006600;">stopObserving</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span>, <span style="color: #3366CC;">'click'</span>, attachWebServiceObserver<span style="color: #66cc66;">&#41;</span>;
		Event.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span>,<span style="color: #3366CC;">'click'</span>, attachPostBackObserver<span style="color: #66cc66;">&#41;</span>;
		$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bt'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span> = <span style="color: #3366CC;">&quot;Submit With Postback&quot;</span>;
	<span style="color: #66cc66;">&#125;</span>
	FIRST_LOAD = <span style="color: #003366; font-weight: bold;">false</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> doAjaxCall<span style="color: #66cc66;">&#40;</span>tr<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	clearErrors<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>formIsValid<span style="color: #66cc66;">&#40;</span>tr.<span style="color: #006600;">responseJSON</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'frm'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">submit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	displayErrorMessages<span style="color: #66cc66;">&#40;</span>tr.<span style="color: #006600;">responseJSON</span><span style="color: #66cc66;">&#41;</span>;	
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> clearErrors<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'dvPostbackErrors'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">update</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'dvPostbackErrors'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;.errormessage&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		Element.<span style="color: #006600;">remove</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;input.formText &quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		e.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>border : <span style="color: #3366CC;">&quot;1px solid #CCC&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;select.formText &quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		e.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>border : <span style="color: #3366CC;">&quot;1px solid #CCC&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> formIsValid<span style="color: #66cc66;">&#40;</span>errorJson<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>isNaN<span style="color: #66cc66;">&#40;</span>errorJson<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> displayErrorMessages<span style="color: #66cc66;">&#40;</span>errorJson<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span>formElement <span style="color: #000066; font-weight: bold;">in</span> errorJson<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> elementObj = <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;errorJson.&quot;</span>+formElement<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span>error <span style="color: #000066; font-weight: bold;">in</span> elementObj<span style="color: #66cc66;">&#41;</span>
			displayError<span style="color: #66cc66;">&#40;</span>formElement , <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;errorJson.&quot;</span>+formElement+<span style="color: #3366CC;">&quot;.&quot;</span>+error<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> displayError<span style="color: #66cc66;">&#40;</span>emt , msg<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> tpl = <span style="color: #3366CC;">&quot;&lt;span class='errormessage' &gt;&quot;</span>+msg+<span style="color: #3366CC;">&quot;&lt;/span&gt;&quot;</span>;
&nbsp;
	markInvalidTextbox<span style="color: #66cc66;">&#40;</span>emt<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	Element.<span style="color: #006600;">insert</span><span style="color: #66cc66;">&#40;</span>emt , <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;after&quot;</span> : tpl <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> markInvalidTextbox<span style="color: #66cc66;">&#40;</span>emt<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	$<span style="color: #66cc66;">&#40;</span>emt<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>border: <span style="color: #3366CC;">&quot;2px solid red&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>In the version that you can download, I include the <a href="http://www.json.org/js.html">JSON.stringify class</a> (courtesy of Crawford) to serialize the form and send it the AsyncAction in my IndexController.</p>
<p>This architectural approach allows you to keep all the validation rules server-side, while allowing you to pick and choose when and how you postback. Because this example works with both postback and asynchronous calls, it's a little verbose. </p>
<p>Regardless, you can <a href="http://jon.lebensold.ca/wp-content/uploads/2008/AsyncValidationManager.zip">download the whole project and try it out for yourself</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/development/decoupled-validation-with-zend_form-zend_validate-and-zend-mvc/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Screencast: Simple AJAX with the Prototype Javascript Framework</title>
		<link>http://lebensold.net/development/screencast-simple-ajax-with-the-prototype-javascript-framework</link>
		<comments>http://lebensold.net/development/screencast-simple-ajax-with-the-prototype-javascript-framework#comments</comments>
		<pubDate>Wed, 30 Apr 2008 06:46:04 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Screencasts]]></category>
		<category><![CDATA[demo]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/development/screencast-simple-ajax-with-the-prototype-javascript-framework</guid>
		<description><![CDATA[This is part two of my introductory Javascript videos. I cover how more about using Event Listeners in with the prototype javascript framework. I also try and answer the question: "What is an Event Listener?" and try and provide a simple example of doing an asynchronous call.
Unlike most AJAX samples, this example doesn't require any [...]]]></description>
			<content:encoded><![CDATA[<p>This is part two of my introductory Javascript videos. I cover how more about using Event Listeners in with the prototype javascript framework. I also try and answer the question: "What is an Event Listener?" and try and provide a simple example of doing an asynchronous call.</p>
<p>Unlike most AJAX samples, this example <strong>doesn't require any server-side scripting or even a webserver</strong>.</p>
<p>Two notes about the video;<br />
- It's not necessary to capture elements in Event.observe using $()<br />
e.g. Event.observe('myHelloLink' ...) instead of Event.observe($('myHelloLink') ... );</p>
<p>- Instead of using innerHTML, you can also use .update("text changed on Load");</p>
<p>Enjoy! </p>
<p><center><br />
<object type="application/x-shockwave-flash" align="center" width="480" height="270"  data="http://www.idea22.com/public/swf/i22_swf.swf?vv=videos:200804226044250710:480:270:e"><param name="allowScriptAccess" value="always" /><param name="movie" value="http://www.idea22.com/public/swf/i22_flv.swf?vv=pv:200804226044250710:480:270:e" /><param name="allowFullScreen" value="true" /><param name="flashVars" value="allowfullscreen=true&logo=http://www.idea22.com/public/swf/i22.png&autostart=false&file=http://www.idea22.com/public/pv/200804226044250710.flv&image=http://www.idea22.com/public/pv/200804226044250710.jpg" /><embed src="http://www.idea22.com//public/swf/i22_flv.swf?vv=pv:200804226044250710:480:270:e" quality="high" width="480" width="270" id="idea22" align="center"  allowfullscreen="true" type="application/x-shockwave-flash"<br />
		pluginspage="http://www.macromedia.com/go/getflashplayer" flashVars="allowfullscreen=true&logo=http://www.idea22.com/public/swf/i22.png&autostart=false&file=http://www.idea22.com/public/pv/200804226044250710.flv&image=http://www.idea22.com/public/pv/200804226044250710.jpg" /></object></center></p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/development/screencast-simple-ajax-with-the-prototype-javascript-framework/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Demo: AJAX Autocompleter with Script.aculo.us and a Zend JSON web service</title>
		<link>http://lebensold.net/creative-stuff/demo-ajax-autocompleter-with-scriptaculous-and-a-zend-json-web-service</link>
		<comments>http://lebensold.net/creative-stuff/demo-ajax-autocompleter-with-scriptaculous-and-a-zend-json-web-service#comments</comments>
		<pubDate>Mon, 24 Mar 2008 09:16:52 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Creative stuff]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[free code]]></category>

		<guid isPermaLink="false">http://jon.lebensold.ca/creative-stuff/demo-ajax-autocompleter-with-scriptaculous-and-a-zend-json-web-service</guid>
		<description><![CDATA[Today I'm posting a sample project of a modification of a Script.aculo.us auto-completer script that will work with a Zend JSON web service. This example could easily be expanded by providing a richer data collection to the client (currently it's just a string array).
Here's a demo:





    

Implementing the code is really only [...]]]></description>
			<content:encoded><![CDATA[<p>Today I'm posting a sample project of a <a href="http://wiki.script.aculo.us/scriptaculous/show/Autocompleter.Local">modification of a Script.aculo.us auto-completer script</a> that will work with a Zend JSON web service. This example could easily be expanded by providing a richer data collection to the client (currently it's just a string array).</p>
<p>Here's a demo:</p>
<link rel="stylesheet" type="text/css" href="/demo/html/public/css/autocomplete.css" >
<p><script type="text/javascript" src="/demo/html/public/js/prototype.js"></script></p>
<p><script type="text/javascript" src="/demo/html/public/js/scriptaculous-1.8/scriptaculous.js"></script></p>
<p><script type="text/javascript" src="/demo/html/public/js/autocompleter.js"></script></p>
<input id="tbAutoCompleter" name="tbAutoCompleter" />
<div id="dvAutoCompleter" class="auto_complete" >    </div>
<p><script type="text/javascript" src="/demo/wp_ac.js"></script></p>
<p>Implementing the code is really only one line once you have the back end web service configured:</p>
<pre class="javascript"> <span style="color: #003366; font-weight: bold;">var</span> demoAutocompleter = <span style="color: #003366; font-weight: bold;">new</span> MyAjaxAutocompleter<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'tbAutoCompleter'</span>, <span style="color: #3366CC;">'dvAutoCompleter'</span>, <span style="color: #3366CC;">'/demo/html/Ajaxservice/index'</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>where 'tbAutoCompleter' corresponds to the textbox you wish to bind, 'dvAutoCompleter' is the control in question and '/demo/html/Ajaxservice/index' is the actual Web service giving you JSON data.</p>
<p>I'm still having a hard time POSTing to a Zend controller in a manner as elegant as you would find in ASP.NET's web services, however the code isn't really that bad. My AjaxServiceController looks like this:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> AjaxserviceController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">contextSwitch</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
             -&gt;<span style="color: #006600;">addActionContext</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'index'</span>, <span style="color: #ff0000;">'json'</span><span style="color: #66cc66;">&#41;</span>
             -&gt;<span style="color: #006600;">initContext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
       	<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">viewRenderer</span>-&gt;<span style="color: #006600;">setNoRender</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
       	<span style="color: #0000ff;">$this</span>-&gt;_helper-&gt;<span style="color: #006600;">getHelper</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'layout'</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">disableLayout</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
       <span style="color: #808080; font-style: italic;">/* [Json response] */</span>
		 <span style="color: #0000ff;">$data</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;2008&quot;</span>,<span style="color: #ff0000;">&quot;3d&quot;</span>,<span style="color: #ff0000;">&quot;social&quot;</span>,<span style="color: #ff0000;">&quot;software&quot;</span>,<span style="color: #ff0000;">&quot;technology&quot;</span>,<span style="color: #ff0000;">&quot;tips&quot;</span>,
<span style="color: #ff0000;">&quot;tools&quot;</span>,<span style="color: #ff0000;">&quot;toread&quot;</span>,<span style="color: #ff0000;">&quot;travel&quot;</span>,<span style="color: #ff0000;">&quot;tutorial&quot;</span>,
<span style="color: #ff0000;">&quot;tutorials&quot;</span>,<span style="color: #ff0000;">&quot;tv&quot;</span>,<span style="color: #ff0000;">&quot;typography&quot;</span>, <span style="color: #ff0000;">&quot;wiki&quot;</span>,<span style="color: #ff0000;">&quot;windows&quot;</span>,<span style="color: #ff0000;">&quot;wishlist&quot;</span>,<span style="color: #ff0000;">&quot;wordpress&quot;</span>
,<span style="color: #ff0000;">&quot;work&quot;</span>,<span style="color: #ff0000;">&quot;writing&quot;</span>,<span style="color: #ff0000;">&quot;youtube&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		 <span style="color: #0000ff;">$responseData</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">array_isearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">getParam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'search'</span><span style="color: #66cc66;">&#41;</span> , <span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
       try <span style="color: #66cc66;">&#123;</span>
&nbsp;
           <span style="color: #0000ff;">$responseDataJsonEncoded</span> = Zend_Json::<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$responseData</span><span style="color: #66cc66;">&#41;</span>;
           <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getResponse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Content-Type'</span>, <span style="color: #ff0000;">'application/json'</span><span style="color: #66cc66;">&#41;</span>
							   -&gt;<span style="color: #006600;">setBody</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$responseDataJsonEncoded</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
       <span style="color: #66cc66;">&#125;</span> catch<span style="color: #66cc66;">&#40;</span>Zend_Json_Exception <span style="color: #0000ff;">$e</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
           <span style="color: #808080; font-style: italic;">// handle and generate HTTP error code response, see below</span>
       <span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> array_isearch<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$str</span>, <span style="color: #0000ff;">$array</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    	<span style="color: #0000ff;">$returnArray</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color: #000066;">preg_match</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/$str/i'</span>,<span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            	<span style="color: #0000ff;">$returnArray</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$v</span>;
&nbsp;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$returnArray</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>There's not much here, mostly just disabling the Zend Layout, specifying the content and doing the search. The $data array could easily be replaced with a data source (like a collection from Zend_Db).</p>
<p><center><a style="border: 0px none #FFF;" href="/demo/SampleAutoCompletionZend15.zip"><img  style="border: 0px none #FFF;"src="/wp-content/uploads/2008/downloadSearchIcon.png" /><br />
<br />
Download Ajax Auto Completer Sample Zend Framework 1.5 project<br />
</a></center></p>
<p>The nice thing about web service AJAX controls is that they live independently of the application calling them, so you can essentially separate out your data layer in the same way that you would separate Zend_Db resources from a Zend_Controller.</p>
]]></content:encoded>
			<wfw:commentRss>http://lebensold.net/creative-stuff/demo-ajax-autocompleter-with-scriptaculous-and-a-zend-json-web-service/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
