<?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>Rob Allen&#039;s DevNotes &#187; Tutorial</title>
	<atom:link href="http://akrabat.com/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://akrabat.com</link>
	<description>Developing PHP software in the Real World, by Rob Allen</description>
	<lastBuildDate>Fri, 12 Mar 2010 16:38:23 +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>Tutorial Q&amp;A PDF</title>
		<link>http://akrabat.com/tutorial/tutorial-qa-pdf/</link>
		<comments>http://akrabat.com/tutorial/tutorial-qa-pdf/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 17:41:35 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=769</guid>
		<description><![CDATA[Chris Kirk has kindly provided a Q&#038;A PDF which summarises a number of problems that have been raised and answered in the comments on the tutorial page. If you are having problems, download it and see if it helps.
Thanks Chris!
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.ncoded.com">Chris Kirk</a> has kindly provided a <a href='http://akrabat.com/wp-content/uploads/chris-kirk-zf-tutorial-problems-solutions.pdf'>Q&#038;A PDF</a> which summarises a number of problems that have been raised and answered in the comments on the <a href="http://akrabat.com/zend-framework-tutorial">tutorial page</a>. If you are having problems, download it and see if it helps.</p>
<p>Thanks Chris!</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/tutorial/tutorial-qa-pdf/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Three years of my Zend Framework Tutorial</title>
		<link>http://akrabat.com/site/three-years-of-my-zend-framework-tutorial/</link>
		<comments>http://akrabat.com/site/three-years-of-my-zend-framework-tutorial/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 06:30:42 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Me]]></category>
		<category><![CDATA[Site]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=714</guid>
		<description><![CDATA[Three years ago today, I published my first Getting Started with Zend Framework tutorial. This was the announcement. Back then, Zend Framework was at version 0.1.5 and a considerably smaller download than now :) 
Three years later and I haven't lost my enthusiasm for Zend Framework as you can tell since the latest version of [...]]]></description>
			<content:encoded><![CDATA[<p>Three years ago today, I published my first <em>Getting Started with Zend Framework</em> tutorial. This was the <a href="/2006/08/16/my-take-on-a-zend-framework-tutorial/">announcement</a>. Back then, Zend Framework was at version 0.1.5 and a considerably smaller download than now :) </p>
<p>Three years later and I haven't lost my enthusiasm for Zend Framework as you can tell since the <a href="http://akrabat.com/zend-framework-tutorial/">latest version of the tutorial</a> supports ZF 1.8 and 1.9 and uses the new features like Zend_Application and the command line Zend_Tool scripts. And I wrote a <a href="http://www.zendframeworkinaction.com">Zend Framework book</a>!</p>
<p>I wonder what will happen in the next three years?!</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/site/three-years-of-my-zend-framework-tutorial/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>My tutorial is compatible with Zend Framework 1.9</title>
		<link>http://akrabat.com/zend-framework/my-tutorial-is-compatible-with-zend-framework-1-9/</link>
		<comments>http://akrabat.com/zend-framework/my-tutorial-is-compatible-with-zend-framework-1-9/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 20:17:20 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=726</guid>
		<description><![CDATA[I've just updated my tutorial to version 1.6.3 after checking that it is still compatible with version 1.9 of Zend Framework.
The only changes I had to make were:

ZF 1.9 comes with its own BaseUrl view helper, so there's no need to write our own.
ZF 1.9.0's command line tool doesn't work on Windows. I've created patches [...]]]></description>
			<content:encoded><![CDATA[<p>I've just updated my <a href="/zend-framework-tutorial/">tutorial</a> to version 1.6.3 after checking that it is still compatible with version 1.9 of Zend Framework.</p>
<p>The only changes I had to make were:</p>
<ul>
<li>ZF 1.9 comes with its own <tt>BaseUrl</tt> view helper, so there's no need to write our own.</li>
<li>ZF 1.9.0's command line tool doesn't work on Windows. I've created patches on issues <a href="http://framework.zend.com/issues/browse/ZF-7464">ZF-7464</a> and <a href="http://framework.zend.com/issues/browse/ZF-7465">ZF-7465</a>. I'm sure this will be sorted with 1.9.1 though.</li>
</ul>
<p>ZF 1.9 looks like a really solid release too.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/my-tutorial-is-compatible-with-zend-framework-1-9/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zend Framework Tutorial for ZF 1.8</title>
		<link>http://akrabat.com/zend-framework/zend-framework-tutorial-for-zf-18/</link>
		<comments>http://akrabat.com/zend-framework/zend-framework-tutorial-for-zf-18/#comments</comments>
		<pubDate>Fri, 01 May 2009 05:30:53 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=600</guid>
		<description><![CDATA[Zend Framework 1.8, has been released!
To celebrate, I have completely revised and updated my Zend Framework tutorial to support the new Zend_Tool command line tool and Zend_Application for bootstrapping.

Let me know what you think :)
]]></description>
			<content:encoded><![CDATA[<p><a href="http://framework.zend.com/">Zend Framework 1.8</a>, has been released!</p>
<p>To celebrate, I have completely revised and updated my <a href="http://akrabat.com/zend-framework-tutorial">Zend Framework tutorial</a> to support the new <tt>Zend_Tool</tt> command line tool and <tt>Zend_Application</tt> for bootstrapping.</p>
<p><img src="http://akrabat.com/wp-content/uploads/zf-tutorial_16-300x207.png" alt="Screen short of Zend Framework tutorial" title="Listing albums in Zend Framework tutorial" width="300" height="207" class="size-medium wp-image-540" /></p>
<p>Let me know what you think :)</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/zend-framework-tutorial-for-zf-18/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>File uploads with Zend_Form_Element_File</title>
		<link>http://akrabat.com/php/file-uploads-with-zend_form_element_file/</link>
		<comments>http://akrabat.com/php/file-uploads-with-zend_form_element_file/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 18:41:30 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=318</guid>
		<description><![CDATA[Now that Zend Framework 1.7 has been released, I thought I'd take a look at the built in file upload element, Zend_Form_Element_File, and see how it can be used. This is how to use it in its most basic form.
I decided to use the same set of form elements as before in order to make [...]]]></description>
			<content:encoded><![CDATA[<p>Now that Zend Framework 1.7 has been released, I thought I'd take a look at the built in file upload element, Zend_Form_Element_File, and see how it can be used. This is how to use it in its most basic form.</p>
<p>I decided to use the same set of form elements as <a href="/2008/04/07/simple-zend_form-file-upload-example/">before</a> in order to make things easy.</p>
<p><img src="http://akrabat.com/wp-content/uploads/zend-form-element-file-example.png" alt="Zend_Form_Element_File_Example.png" border="0" width="641" height="404" /></p>
<p>Let's start with the form:</p>
<h3>The form</h3>
<p>We extend <tt>Zend_Form</tt> and store it in the <tt>application/forms</tt> folder and so the class name is <tt> forms_UploadForm</tt>:</p>
<pre class="phpcode">
<span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">forms_UploadForm&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Form
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setName</span><span style="color: #007700">(</span><span style="color: #DD0000">'upload'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setAttrib</span><span style="color: #007700">(</span><span style="color: #DD0000">'enctype'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'multipart/form-data'</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$description&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Form_Element_Text</span><span style="color: #007700">(</span><span style="color: #DD0000">'description'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$description</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #DD0000">'Description'</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">setRequired</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">addValidator</span><span style="color: #007700">(</span><span style="color: #DD0000">'NotEmpty'</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Form_Element_File</span><span style="color: #007700">(</span><span style="color: #DD0000">'file'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #DD0000">'File'</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">setDestination</span><span style="color: #007700">(</span><span style="color: #0000BB">BASE_PATH&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'/data/uploads'</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">setRequired</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$submit&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Form_Element_Submit</span><span style="color: #007700">(</span><span style="color: #DD0000">'submit'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$submit</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #DD0000">'Upload'</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addElements</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$description</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$submit</span><span style="color: #007700">));

&nbsp;&nbsp;&nbsp;&nbsp;}
}
</span>
</span></code></pre>
<p>As before, we set the name and enctype attribute of the form to allow for files to be uploaded. The form itself has two fields: a text field called 'description' and the file upload field called 'file', along with a submit button. Nothing especially complicated here. </p>
<p>The <tt>Zend_Form_Element_File</tt> element has a setDestination() method which is used to tell the underlying <tt>Zend_File_Transfer_Adapter_Http</tt> where we want the file that is uploaded to be stored. In this case we choose <tt>data/uploads</tt>.</p>
<h3>The controller &amp; view</h3>
<p>The controller is also very standard:</p>
<pre class="phpcode">
<span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">IndexController&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action&nbsp;
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">indexAction</span><span style="color: #007700">()&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">headTitle</span><span style="color: #007700">(</span><span style="color: #DD0000">'Home'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">title&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Zend_Form_Element_File&nbsp;Example'</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bodyCopy&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;Please&nbsp;fill&nbsp;out&nbsp;this&nbsp;form.&lt;/p&gt;"</span><span style="color: #007700">;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$form&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">forms_UploadForm</span><span style="color: #007700">();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">isPost</span><span style="color: #007700">())&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$formData&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getPost</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$form</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">isValid</span><span style="color: #007700">(</span><span style="color: #0000BB">$formData</span><span style="color: #007700">))&nbsp;{

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;success&nbsp;-&nbsp;do&nbsp;something&nbsp;with&nbsp;the&nbsp;uploaded&nbsp;file
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$uploadedData&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getValues</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fullFilePath&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">file</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getFileName</span><span style="color: #007700">();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Zend_Debug</span><span style="color: #007700">::</span><span style="color: #0000BB">dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$uploadedData</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'$uploadedData'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Zend_Debug</span><span style="color: #007700">::</span><span style="color: #0000BB">dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$fullFilePath</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'$fullFilePath'</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"done"</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">populate</span><span style="color: #007700">(</span><span style="color: #0000BB">$formData</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">form&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">;

&nbsp;&nbsp;&nbsp;&nbsp;}
}
</span>
</span></code></pre>
<p>The view, <tt>views/scripts/index.phtml</tt>, is trivial:</p>
<pre class="phpcode"><span style="color: #0000BB">
</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">h1</span><span style="color: #007700">&gt;&lt;?=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">title</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">?&gt;</span>&lt;/h1&gt;
<span style="color: #0000BB">&lt;?=&nbsp;$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bodyCopy</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">?&gt;
&lt;?=&nbsp;$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">form</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">?&gt;
</span>
</span></code></pre>
<p>If the form validates correctly, the <tt>$uploadedData</tt> array will contain the values of the form fields along with the filename of the file that was uploaded. Note that this filename is not fully qualified, so if you need the entire path to the file, then use the <tt>getFileName()</tt> method on the file element.</p>
<h3>Conclusion</h3>
<p>That's all there is to it for simple uploading of forms. There are still a few fairly important bugs in the component that we'll have to wait for 1.7.2 for. Specifically the <tt>Count</tt> validator doesn't always work as you'd expect and don't use <tt>getValues()</tt> and <tt>receive()</tt> as it isn't yet clever enough to know not to call <tt>move_uploaded_file()</tt> more than once. </p>
<p>As usual, here's a zip file of the project I created to test this: <a href="/wp-content/uploads/2008/Zend_Form_Element_File_Example.zip">Zend_Form_Element_File_Example.zip</a> (including Zend Framework (a snapshot of the release-1.7 branch) which is why it's 3.9MB large).</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/php/file-uploads-with-zend_form_element_file/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Using Action Helpers in Zend Framework</title>
		<link>http://akrabat.com/zend-framework/using-action-helpers-in-zend-framework/</link>
		<comments>http://akrabat.com/zend-framework/using-action-helpers-in-zend-framework/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 11:30:15 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=271</guid>
		<description><![CDATA[When you have some functionality that needs to be shared across multiple controllers, one method is to use action helpers. Action helpers are very powerful and contain hooks to automatically run when you need them too, but you can ignore all that if you don't need it.
The first thing you need to do is decide [...]]]></description>
			<content:encoded><![CDATA[<p>When you have some functionality that needs to be shared across multiple controllers, one method is to use action helpers. Action helpers are very powerful and contain hooks to automatically run when you need them too, but you can ignore all that if you don't need it.</p>
<p>The first thing you need to do is decide where to put them. The latest default project structure document recommends using a sub folder from your controllers directory. That is: <tt>application/controllers/helpers</tt>, and it's as good a place as any. </p>
<p>Firstly, you need to tell the helper broker where your action helpers are. I usually do this in the bootstrap, but it could equally be done in a front controller plug-in:</p>
<pre class="phpcode"><span style="color: #0000BB">
&nbsp;&nbsp;&nbsp;&nbsp;Zend_Controller_Action_HelperBroker</span><span style="color: #007700">::</span><span style="color: #0000BB">addPath</span><span style="color: #007700">(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">APPLICATION_PATH&nbsp;</span><span style="color: #007700">.</span><span style="color: #DD0000">'/controllers/helpers'</span><span style="color: #007700">);
</span>
</span></code></pre>
<p>You then need to create your action helper, we'll call it <i>Multiples</i> in this example and so<br />
the filename is <tt>application/controllers/helpers/Multiples.php</tt>:</p>
<pre class="phpcode">
<span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action_Helper_Multiples&nbsp;</span><span style="color: #007700">extends
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action_Helper_Abstract
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">direct</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</span>
</span></code></pre>
<p>Note that there is a prefix to the action helper name of <tt>Zend_Controller_Action_Helper</tt>. You can change this by passing a different prefix as the second parameter to the <tt>Zend_Controller_Action_HelperBroker::addPath()</tt> call in your bootstrap.</p>
<p>Finally, usage within a controller action:</p>
<pre class="phpcode">
<span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">IndexController&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action&nbsp;
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">indexAction</span><span style="color: #007700">()&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">headTitle</span><span style="color: #007700">(</span><span style="color: #DD0000">'Home'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">title&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Test&nbsp;of&nbsp;the&nbsp;Multiples&nbsp;action&nbsp;helper'</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">30</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$twice&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_helper</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">multiples</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-</span><span style="color: #0000BB">twice&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$twice</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</span>
</span></code></pre>
<p>Note that we call the action helper's name as a function of the <tt>_helper</tt> class. This maps to the <tt>direct()</tt> call within the helper.</p>
<p>You can put also multiple functions within the action helper:</p>
<pre class="phpcode"><span style="color: #0000BB">
</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action_Helper_Multiples&nbsp;</span><span style="color: #007700">extends
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action_Helper_Abstract
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">direct</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">thrice</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;
}
</span>
</span></code></pre>
<p>To call the <tt>thrice()</tt> function within a controller action you use:</p>
<pre class="phpcode"><span style="color: #0000BB">
&nbsp;&nbsp;&nbsp;&nbsp;$thrice&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_helper</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">multiples</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">thrice</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">);
</span>
</span></code></pre>
<p>As you can see if you use the action helper's name as a property of <tt>_helper</tt>, then you can call any function within the helper.</p>
<p>This is just a summary to get you going. Full details are in the <a href="http://framework.zend.com/manual/en/zend.controller.actionhelpers.html">manual</a>.  </p>
<p>Have fun and avoid copy and paste with your common functionality!</p>
<p><b>Zip Files:</b><br />
I've created an example project so you can see it in context.</p>
<ul>
<li><a href="/wp-content/uploads/2008/ZF_Action_Helper_example.zip">ZF_Action_Helper_example.zip</a> - 9.7kB</li>
<li><a href="/wp-content/uploads/2008/ZF_Action_Helper_example.zip">ZF_Action_Helper_example_with_zf.zip</a> (including ZF 1.6) - 3.5MB</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/using-action-helpers-in-zend-framework/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Simple Zend_Form File Upload Example</title>
		<link>http://akrabat.com/php/simple-zend_form-file-upload-example/</link>
		<comments>http://akrabat.com/php/simple-zend_form-file-upload-example/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 07:04:19 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[zendframework]]></category>
		<category><![CDATA[zend_form]]></category>

		<guid isPermaLink="false">http://akrabat.com/?p=242</guid>
		<description><![CDATA[Zend Framework 1.5's Zend_Form component is missing support for the file input element as it is waiting on a file upload component to build upon. We're busy people, so we'll fake it...
This is a super simple example showing how to do file uploads with Zend_Form in Zend Framework 1.5.
I'm building on the Simple Zend_Form Example, [...]]]></description>
			<content:encoded><![CDATA[<p>Zend Framework 1.5's Zend_Form component is missing support for the <tt>file</tt> input element as it is waiting on a file upload component to build upon. We're busy people, so we'll fake it...</p>
<p>This is a super simple example showing how to do file uploads with Zend_Form in Zend Framework 1.5.</p>
<p>I'm building on the <a href="/2008/02/21/simple-zend_form-example/">Simple Zend_Form Example</a>, so make sure you have read that and that it works before you start this one.</p>
<p>This is what the form looks like:</p>
<p><img src="http://akrabat.com/wp-content/uploads/simplefileuploadscreenshot.jpg" alt="SimpleFileUploadScreenshot.jpg" border="0" width="580" height="369" /></p>
<p>(Unstyled, as usual!)</p>
<p>This is what we need to do:</p>
<h3>The form</h3>
<p>The form is an extension of <tt>Zend_Form</tt> and is stored <tt>in app/forms/UploadForm.php</tt> and so the class name is <tt>forms_UploadForm</tt>:</p>
<pre class="phpcode">
<span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">forms_UploadForm&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Form&nbsp;
</span><span style="color: #007700">{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addElementPrefixPath</span><span style="color: #007700">(</span><span style="color: #DD0000">'App'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'App/'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setName</span><span style="color: #007700">(</span><span style="color: #DD0000">'upload'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setAttrib</span><span style="color: #007700">(</span><span style="color: #DD0000">'enctype'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'multipart/form-data'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$description&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Form_Element_Text</span><span style="color: #007700">(</span><span style="color: #DD0000">'description'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$description</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #DD0000">'Description'</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">setRequired</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">addValidator</span><span style="color: #007700">(</span><span style="color: #DD0000">'NotEmpty'</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">App_Form_Element_File</span><span style="color: #007700">(</span><span style="color: #DD0000">'file'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #DD0000">'File'</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">setRequired</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">addValidator</span><span style="color: #007700">(</span><span style="color: #DD0000">'NotEmpty'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$submit&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Form_Element_Submit</span><span style="color: #007700">(</span><span style="color: #DD0000">'submit'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$submit</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #DD0000">'Upload'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addElements</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$description</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$submit</span><span style="color: #007700">));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;
}&nbsp;
</span>
</span></code></pre>
<p>We are going to create our own validator class within our own library namespace (lib/App) and so we need to tell the form about this using <tt>addElementPath()</tt>. We then set the name and enctype attribute of the form to allow for files to be uploaded.</p>
<p>The form has two fields: a text field called 'description' and the file upload field called 'file', along with a submit button. As Zend Framework doesn't have it's own file element, we will create our own called <tt>App_Form_Element_File</tt>.</p>
<h3>The file form element</h3>
<p>The file element, <tt>App_Form_Element_File</tt>, is stored in <tt>lib/App/Form/Element/File.php</tt> and looks like this:</p>
<pre class="phpcode">
<span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">require_once&nbsp;</span><span style="color: #DD0000">'Zend/Form/Element/Xhtml.php'</span><span style="color: #007700">;

class&nbsp;</span><span style="color: #0000BB">App_Form_Element_File&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Form_Element_Xhtml
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Flag&nbsp;indicating&nbsp;whether&nbsp;or&nbsp;not&nbsp;to&nbsp;insert&nbsp;ValidFile&nbsp;validator&nbsp;when&nbsp;element&nbsp;is&nbsp;required
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@var&nbsp;bool
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">protected&nbsp;</span><span style="color: #0000BB">$_autoInsertValidFileValidator&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;

&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Default&nbsp;view&nbsp;helper&nbsp;to&nbsp;use
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@var&nbsp;string
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;</span><span style="color: #0000BB">$helper&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'formFile'</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Set&nbsp;flag&nbsp;indicating&nbsp;whether&nbsp;a&nbsp;ValidFile&nbsp;validator&nbsp;should&nbsp;be&nbsp;inserted&nbsp;when&nbsp;element&nbsp;is&nbsp;required
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;&nbsp;bool&nbsp;$flag&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;Zend_Form_Element
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">setAutoInsertValidFileValidator</span><span style="color: #007700">(</span><span style="color: #0000BB">$flag</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_autoInsertValidFileValidator&nbsp;</span><span style="color: #007700">=&nbsp;(bool)&nbsp;</span><span style="color: #0000BB">$flag</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Get&nbsp;flag&nbsp;indicating&nbsp;whether&nbsp;a&nbsp;ValidFile&nbsp;validator&nbsp;should&nbsp;be&nbsp;inserted&nbsp;when&nbsp;element&nbsp;is&nbsp;required
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;bool
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">autoInsertValidFileValidator</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_autoInsertValidFileValidator</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">isValid</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;for&nbsp;a&nbsp;file&nbsp;upload,&nbsp;the&nbsp;value&nbsp;is&nbsp;not&nbsp;in&nbsp;the&nbsp;POST&nbsp;array,&nbsp;it's&nbsp;in&nbsp;$_FILES
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">null&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(isset(</span><span style="color: #0000BB">$_FILES</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">]))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_FILES</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;auto&nbsp;insert&nbsp;ValidFile&nbsp;validator
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">isRequired</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">autoInsertValidFileValidator</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;!</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getValidator</span><span style="color: #007700">(</span><span style="color: #DD0000">'ValidFile'</span><span style="color: #007700">))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$validators&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getValidators</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$validFile&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'validator'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'ValidFile'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'breakChainOnFailure'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">array_unshift</span><span style="color: #007700">(</span><span style="color: #0000BB">$validators</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$validFile</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setValidators</span><span style="color: #007700">(</span><span style="color: #0000BB">$validators</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;do&nbsp;not&nbsp;use&nbsp;the&nbsp;automatic&nbsp;NotEmpty&nbsp;Validator&nbsp;as&nbsp;ValidFile&nbsp;replaces&nbsp;it&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setAutoInsertNotEmptyValidator</span><span style="color: #007700">(</span><span style="color: #0000BB">false</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">isValid</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;}

}
</span>
</span></code></pre>
<p>Zend Framework already provides a view helper for displaying a file element, <tt>formFile</tt>, so we set the <tt>$helper</tt> member variable to 'formFile' so that the correct element is rendered when the form is displayed. We then need to ensure that validation is handled correctly. For all other form elements the value of the field is returned in the POST array. For a file, this is not true as the data is with the <tt>$_FILES</tt> global array. We could handle this in the controller, but by putting it in the element class, we never have to think about it again.  The <tt>isValid()</tt> member function is used to set the value for an element and also run the validator chain to determine if the value is valid or not. </p>
<p>We override <tt>isValid()</tt> for the file element to provide two functionalities:</p>
<p>* Set the value to the contents of correct sub-array of the $_FILES array .<br />
* Automatically turn on a custom validator called ValidFile which will check if the upload succeeded.</p>
<p>The file element also has some helper functions (<tt>setAutoInsertValidFileValidator</tt> and <tt>getAutoInsertValidFileValidator</tt>) to control the auto-insertion of the <tt>ValidFile</tt> validator. Note that if we do automatically insert the <tt>ValidFile</tt> validator, then we turn off <tt>Zend_Form_Element</tt>'s automatic <tt>NotEmpty</tt> validator as it is redundant. </p>
<p>Once we have set the <tt>$value</tt> variable correctly and inserted our validator, we call up to the parent's <tt>isValid()</tt> function which will run the validation chain for us.</p>
<h3>The <tt>ValidFile</tt> validator</h3>
<p>The <tt>ValidFile</tt> validator's full class name is <tt>App_Validate_ValidFile</tt> and so it's filename is lib/App/Validate/ValidFile.php. This class handles validating the 'error' field within the $value array (which as you'll recall came from $_FILES). This is the code:</p>
<pre class="phpcode">
<span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">App_Validate_ValidFile&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Validate_Abstract
</span><span style="color: #007700">{

&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;</span><span style="color: #0000BB">INI_SIZE&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'iniSize'</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Value:&nbsp;1;&nbsp;The&nbsp;uploaded&nbsp;file&nbsp;exceeds&nbsp;the&nbsp;upload_max_filesize&nbsp;directive&nbsp;in&nbsp;php.ini
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">const&nbsp;</span><span style="color: #0000BB">FORM_SIZE&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'formSize'</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Value:&nbsp;2;&nbsp;The&nbsp;uploaded&nbsp;file&nbsp;exceeds&nbsp;the&nbsp;MAX_FILE_SIZE&nbsp;directive&nbsp;that&nbsp;was&nbsp;specified&nbsp;in&nbsp;the&nbsp;HTML&nbsp;form.&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">const&nbsp;</span><span style="color: #0000BB">PARTIAL&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'partial'</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Value:&nbsp;3;&nbsp;The&nbsp;uploaded&nbsp;file&nbsp;was&nbsp;only&nbsp;partially&nbsp;uploaded.&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">const&nbsp;</span><span style="color: #0000BB">NO_FILE&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'noFile'</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Value:&nbsp;4;&nbsp;No&nbsp;file&nbsp;was&nbsp;uploaded.&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">const&nbsp;</span><span style="color: #0000BB">NO_TMP_DIR&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'noTmpDir'</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Value:&nbsp;6;&nbsp;Missing&nbsp;a&nbsp;temporary&nbsp;folder.
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">const&nbsp;</span><span style="color: #0000BB">CANT_WRITE&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'cantWrite'</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;Value:&nbsp;7;&nbsp;Failed&nbsp;to&nbsp;write&nbsp;file&nbsp;to&nbsp;disk.
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">const&nbsp;</span><span style="color: #0000BB">EXTENSION&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'extension'</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Value:&nbsp;8;&nbsp;File&nbsp;upload&nbsp;stopped&nbsp;by&nbsp;extension.&nbsp;Introduced&nbsp;in&nbsp;PHP&nbsp;5.2.0.&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">const&nbsp;</span><span style="color: #0000BB">ERROR&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'error'</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;General&nbsp;error&nbsp;for&nbsp;future&nbsp;proofing&nbsp;against&nbsp;new&nbsp;PHP&nbsp;versions

&nbsp;&nbsp;&nbsp;&nbsp;/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@var&nbsp;array
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">protected&nbsp;</span><span style="color: #0000BB">$_messageTemplates&nbsp;</span><span style="color: #007700">=&nbsp;array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">INI_SIZE&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"The&nbsp;uploaded&nbsp;file&nbsp;exceeds&nbsp;the&nbsp;upload_max_filesize&nbsp;directive&nbsp;in&nbsp;php.ini"</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">FORM_SIZE&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"The&nbsp;uploaded&nbsp;file&nbsp;exceeds&nbsp;the&nbsp;MAX_FILE_SIZE&nbsp;directive&nbsp;that&nbsp;was&nbsp;specified&nbsp;in&nbsp;the&nbsp;HTML&nbsp;form"</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">PARTIAL&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"The&nbsp;uploaded&nbsp;file&nbsp;was&nbsp;only&nbsp;partially&nbsp;uploaded"</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">NO_FILE&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"No&nbsp;file&nbsp;was&nbsp;uploaded"</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">NO_TMP_DIR&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Missing&nbsp;a&nbsp;temporary&nbsp;folder"</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">CANT_WRITE&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Failed&nbsp;to&nbsp;write&nbsp;file&nbsp;to&nbsp;disk"</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">EXTENSION&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"File&nbsp;upload&nbsp;stopped&nbsp;by&nbsp;extension"</span><span style="color: #007700">,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">ERROR&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Unknown&nbsp;upload&nbsp;error"
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Defined&nbsp;by&nbsp;Zend_Validate_Interface
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Returns&nbsp;true&nbsp;if&nbsp;and&nbsp;only&nbsp;if&nbsp;$value[error]&nbsp;is&nbsp;equal&nbsp;to&nbsp;UPLOAD_ERR_OK.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@note:&nbsp;This&nbsp;validator&nbsp;expects&nbsp;$value&nbsp;to&nbsp;be&nbsp;the&nbsp;array&nbsp;from&nbsp;$_FILES
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;array&nbsp;$value
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;boolean
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">isValid</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;default&nbsp;value&nbsp;and&nbsp;error&nbsp;is&nbsp;"no&nbsp;file&nbsp;uploaded"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$valueString&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$error&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">UPLOAD_ERR_NO_FILE</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'error'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;set&nbsp;the&nbsp;error&nbsp;to&nbsp;the&nbsp;correct&nbsp;value
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$error&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">[</span><span style="color: #DD0000">'error'</span><span style="color: #007700">];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;set&nbsp;the&nbsp;%value%&nbsp;placeholder&nbsp;to&nbsp;the&nbsp;uplaoded&nbsp;filename
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$valueString&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_setValue</span><span style="color: #007700">(</span><span style="color: #0000BB">$valueString</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch(</span><span style="color: #0000BB">$error</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">UPLOAD_ERR_OK</span><span style="color: #007700">:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">UPLOAD_ERR_INI_SIZE</span><span style="color: #007700">:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_error</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">INI_SIZE</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">UPLOAD_ERR_FORM_SIZE</span><span style="color: #007700">:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_error</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">FORM_SIZE</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">UPLOAD_ERR_PARTIAL</span><span style="color: #007700">:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_error</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">PARTIAL</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">UPLOAD_ERR_NO_FILE</span><span style="color: #007700">:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_error</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">NO_FILE</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">UPLOAD_ERR_NO_TMP_DIR</span><span style="color: #007700">:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_error</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">NO_TMP_DIR</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">UPLOAD_ERR_CANT_WRITE</span><span style="color: #007700">:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_error</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">CANT_WRITE</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">:&nbsp;</span><span style="color: #FF8000">//&nbsp;UPLOAD_ERR_EXTENSION&nbsp;isn't&nbsp;defined&nbsp;in&nbsp;PHP&nbsp;5.1.4,&nbsp;so&nbsp;use&nbsp;the&nbsp;value
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_error</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">EXTENSION</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_error</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">ERROR</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}

}
</span>
</span></code></pre>
<p>Although this class is quite long, not much happens! All the work happens in <tt>isValid()</tt> where we set $error to the 'error' value originally from the $_FILES array and then set our error message based on which error PHP has reported to us. If PHP reported no error (UPLOAD_ERR_OK), then we return true, otherwise we return false after having set the error value.</p>
<h3>The controller</h3>
<p>Lastly, the controller is essentially identical to what we did in the <a href="/2008/02/21/simple-zend_form-example/">Simple Zend_Form Example</a>:</p>
<pre class="phpcode">
<span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">IndexController&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">indexAction</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pageTitle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Zend_Form&nbsp;File&nbsp;Upload&nbsp;Example"</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bodyCopy&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;Please&nbsp;fill&nbsp;out&nbsp;this&nbsp;form.&lt;/p&gt;"</span><span style="color: #007700">;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$form&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">forms_UploadForm</span><span style="color: #007700">();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">isPost</span><span style="color: #007700">())&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$formData&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getPost</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$form</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">isValid</span><span style="color: #007700">(</span><span style="color: #0000BB">$formData</span><span style="color: #007700">))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;success&nbsp;-&nbsp;do&nbsp;something&nbsp;with&nbsp;the&nbsp;uploaded&nbsp;file
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$uploadedData&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getValues</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Zend_Debug</span><span style="color: #007700">::</span><span style="color: #0000BB">dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$uploadedData</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'$uploadedData'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">populate</span><span style="color: #007700">(</span><span style="color: #0000BB">$formData</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">form&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</span>
</span></code></pre>
<p>If it all validates correctly, then you need to process the uploaded data. For this example we just display it using <tt>Zend_Debug::dump()</tt>, so you can see what it looks like:</p>
<p><img src="http://akrabat.com/wp-content/uploads/simplefileuploadscreenshot21.jpg" alt="SimpleFileUploadScreenshot2.jpg" border="0" width="594" height="411" /></p>
<h3>Conclusion</h3>
<p>As you can see, although Zend Framework 1.5 doesn't have file upload element built in, adding one is not especially hard. In fact, we have more code used to validate that and provide an error message on failure than we do for the upload itself!</p>
<p>As usual, here's a zip file of this project: <a href="http://akrabat.com/wp-content/uploads/Zend_Form_FileUpload_Example.zip">Zend_Form_FileUpload_Example.zip</a> (It includes Zend Framework 1.5.1 which is why it's 3.9MB big).</p>
<p>Test it out and maybe use it as the basis of your file uploading needs with Zend_Form.</p>
<p>Update: There's a follow up to this post here: <a href="http://akrabat.com/2008/05/16/simple-zend_form-file-upload-example-revisited/">Simple Zend_Form File Upload Example Revisited</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/php/simple-zend_form-file-upload-example/feed/</wfw:commentRss>
		<slash:comments>60</slash:comments>
		</item>
		<item>
		<title>Updated Tutorial for Zend Framework 1.5</title>
		<link>http://akrabat.com/zend-framework/updated-tutorial-for-zend-framework-15/</link>
		<comments>http://akrabat.com/zend-framework/updated-tutorial-for-zend-framework-15/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 13:50:25 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://akrabat.com/2008/03/17/updated-tutorial-for-zend-framework-15/</guid>
		<description><![CDATA[Zend Framework 1.5 has now been released to mark the occasion I have significantly updated my Zend Framework Tutorial! The tutorial was first released on 16th August 2006 and was written against version 0.1 of Zend Framework and had one major revision to bring in support for the ViewRenderer component. 

The new tutorial produces exactly [...]]]></description>
			<content:encoded><![CDATA[<p>Zend Framework 1.5 has now been released to mark the occasion I have significantly updated my <a href="/zend-framework-tutorial">Zend Framework Tutorial</a>! The tutorial was first released on <a href="http://akrabat.com/2006/08/16/my-take-on-a-zend-framework-tutorial/">16th August 2006</a> and was written against version 0.1 of Zend Framework and had one major revision to bring in support for the ViewRenderer component. </p>
<p><img src="http://akrabat.com/wp-content/uploads/tutorial-screenshot.jpg" alt="Tutorial-Screenshot" /></p>
<p>The new tutorial produces exactly the same application as before, but now uses the new 1.5 goodies of Zend_Form and Zend_Layout, so you can see how these key components fit into a Zend Framework MVC application.</p>
<p>As always, when you find bugs and typos, please let me know and I'll fix them!</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/zend-framework/updated-tutorial-for-zend-framework-15/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Simple Zend_Form Example</title>
		<link>http://akrabat.com/php/simple-zend_form-example/</link>
		<comments>http://akrabat.com/php/simple-zend_form-example/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 21:01:21 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/2008/02/21/simple-zend_form-example/</guid>
		<description><![CDATA[Following on from the Simple Zend_Layout Example, Zend_Form is now in the trunk, so here's a super simple, complete example that shows it in action:

(clearly the form is unstyled :)
You can construct a Zend_Form directly from a config file or can build it in code. This example builds it in code.
Setting up
This example uses the [...]]]></description>
			<content:encoded><![CDATA[<p>Following on from the <a href="http://akrabat.com/2007/12/11/simple-zend_layout-example/">Simple Zend_Layout Example</a>, Zend_Form is now in the trunk, so here's a super simple, complete example that shows it in action:</p>
<p><img src="http://akrabat.com/wp-content/uploads/zend-form-screenshot1.png" alt="Zend_Form screenshot1.png" border="0" width="469" height="412" /></p>
<p>(clearly the form is unstyled :)</p>
<p>You can construct a Zend_Form directly from a config file or can build it in code. This example builds it in code.</p>
<h3>Setting up</h3>
<p>This example uses the same basic skeleton as the Zend_Layout code with the addition of a forms directory:</p>
<p><img src="http://akrabat.com/wp-content/uploads/zend-form-directory-layout.png" alt="Zend_Form Directory Layout.png" border="0" width="282" height="382" /></p>
<p>As you can see, I've added a forms directory within the application subdirectory with our form file, ContactForm.php, in it. </p>
<h3>The contact form</h3>
<p>To keep the form contained, I've put it in its own class which extends from <tt>Zend_Form</tt>. The file is application/forms/ContactForm.php and so the class is <tt>forms_ContactForm</tt> so that <tt>Zend_Loader</tt> can easily load it:</p>
<pre class="phpcode"><span style="color: #0000BB">
</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">forms_ContactForm&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Form&nbsp;
</span><span style="color: #007700">{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setName</span><span style="color: #007700">(</span><span style="color: #DD0000">'contact_us'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$title&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Form_Element_Select</span><span style="color: #007700">(</span><span style="color: #DD0000">'title'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$title</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #DD0000">'Title'</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">setMultiOptions</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'mr'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'Mr'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mrs'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'Mrs'</span><span style="color: #007700">))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">setRequired</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">addValidator</span><span style="color: #007700">(</span><span style="color: #DD0000">'NotEmpty'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$firstName&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Form_Element_Text</span><span style="color: #007700">(</span><span style="color: #DD0000">'firstName'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$firstName</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #DD0000">'First&nbsp;name'</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">setRequired</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">addValidator</span><span style="color: #007700">(</span><span style="color: #DD0000">'NotEmpty'</span><span style="color: #007700">);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$lastName&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Form_Element_Text</span><span style="color: #007700">(</span><span style="color: #DD0000">'lastName'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$lastName</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #DD0000">'Last&nbsp;name'</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">setRequired</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">addValidator</span><span style="color: #007700">(</span><span style="color: #DD0000">'NotEmpty'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$email&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Form_Element_Text</span><span style="color: #007700">(</span><span style="color: #DD0000">'email'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$email</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #DD0000">'Email&nbsp;address'</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">addFilter</span><span style="color: #007700">(</span><span style="color: #DD0000">'StringToLower'</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">setRequired</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">addValidator</span><span style="color: #007700">(</span><span style="color: #DD0000">'NotEmpty'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">addValidator</span><span style="color: #007700">(</span><span style="color: #DD0000">'EmailAddress'</span><span style="color: #007700">);&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$submit&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Zend_Form_Element_Submit</span><span style="color: #007700">(</span><span style="color: #DD0000">'submit'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$submit</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setLabel</span><span style="color: #007700">(</span><span style="color: #DD0000">'Contact&nbsp;us'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addElements</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$title</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$firstName</span><span style="color: #007700">,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$lastName</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$email</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$submit</span><span style="color: #007700">));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;
}
</span>
</span></code></pre>
<p>We set the name of the form first and then create five elements; one select box for the title, three text boxes for first name, surname and email address and then finally a submit button. Note that as we have overridden the constructer, we also call up to the parent's constructor so that all the Zend_Form initialisation happens. </p>
<p>Creating an element is quite simple. Each element is of the type Zend_Form_Element_Xxx where Xxx is the type, such as Text, Submit, Select, etc. We then configure the element how we want it, such as setting a label and adding filters and validators. Finally we add all the elements to the form using the <tt>addElements()</tt> function.</p>
<p>Note that if you have multiple validators, you can pass in <tt>true</tt> as the second parameter ($breakChainOnFailure) to stop processing the remaining validators at that point. This can be because the further validators do not make sense or because you only ever want the user to see one message per field to avoid overwhelming them.</p>
<h3>Displaying the form</h3>
<p>Having defined out form, we need to get it to the screen. In the controller we need the following:</p>
<pre class="phpcode"><span style="color: #0000BB">
</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">IndexController&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">indexAction</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pageTitle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Zend_Form&nbsp;Example"</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bodyCopy&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"&lt;p&nbsp;&gt;Please&nbsp;fill&nbsp;out&nbsp;this&nbsp;form.&lt;/p&gt;"</span><span style="color: #007700">;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$form&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">forms_ContactForm</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">form&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</span>
</span></code></pre>
<p>This code assigns some variables to the view, then instantiates the form and assigns that to the view also. The view script is similarly simple:</p>
<p>view/scripts/index/index.phtml:</p>
<pre class="phpcode">
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pageTitle&nbsp;</span><span style="color: #007700">;</span><span style="color: #0000BB">?&gt;
&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bodyCopy&nbsp;</span><span style="color: #007700">;</span><span style="color: #0000BB">?&gt;
</span>
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">form&nbsp;</span><span style="color: #007700">;</span><span style="color: #0000BB">?&gt;
</span>
</span></code></pre>
<p>To render the form, we simply echo it. By default, the form is marked up as a definition list and its HTML looks like this:</p>
<pre class="phpcode"><span style="color: #0000BB">
</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">form&nbsp;name</span><span style="color: #007700">=</span><span style="color: #DD0000">"contact_us"&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">=</span><span style="color: #DD0000">"contact_us"&nbsp;</span><span style="color: #0000BB">enctype</span><span style="color: #007700">=</span><span style="color: #DD0000">"application/x-www-form-urlencoded"&nbsp;</span><span style="color: #0000BB">action</span><span style="color: #007700">=</span><span style="color: #DD0000">""&nbsp;</span><span style="color: #0000BB">method</span><span style="color: #007700">=</span><span style="color: #DD0000">"post"</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">dl&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"zend_form"</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">dt</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">label&nbsp;</span><span style="color: #007700">for=</span><span style="color: #DD0000">"title"&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"required"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Title</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">label</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">dt</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">dd</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">select&nbsp;name</span><span style="color: #007700">=</span><span style="color: #DD0000">"title"&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">=</span><span style="color: #DD0000">"title"</span><span style="color: #007700">&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">option&nbsp;value</span><span style="color: #007700">=</span><span style="color: #DD0000">"mr"&nbsp;</span><span style="color: #0000BB">label</span><span style="color: #007700">=</span><span style="color: #DD0000">"Mr"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Mr</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">option</span><span style="color: #007700">&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">option&nbsp;value</span><span style="color: #007700">=</span><span style="color: #DD0000">"mrs"&nbsp;</span><span style="color: #0000BB">label</span><span style="color: #007700">=</span><span style="color: #DD0000">"Mrs"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Mrs</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">option</span><span style="color: #007700">&gt;
&lt;/</span><span style="color: #0000BB">select</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">dd</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">dt</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">label&nbsp;</span><span style="color: #007700">for=</span><span style="color: #DD0000">"firstName"&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"required"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">First&nbsp;name</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">label</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">dt</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">dd</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"text"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"firstName"&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">=</span><span style="color: #DD0000">"firstName"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">""</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">dd</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">dt</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">label&nbsp;</span><span style="color: #007700">for=</span><span style="color: #DD0000">"lastName"&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"required"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Last&nbsp;name</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">label</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">dt</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">dd</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"text"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"lastName"&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">=</span><span style="color: #DD0000">"lastName"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">""</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">dd</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">dt</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">label&nbsp;</span><span style="color: #007700">for=</span><span style="color: #DD0000">"email"&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"optional"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Email&nbsp;address</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">label</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">dt</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">dd</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"text"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"email"&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">=</span><span style="color: #DD0000">"email"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">""</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">dd</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">dt</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">dt</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">dd</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"submit"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"submit"&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">=</span><span style="color: #DD0000">"submit"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">"Contact&nbsp;us"</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">dd</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">dl</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">form</span><span style="color: #007700">&gt;
</span>
</span></code></pre>
<p>Notice how the setRequired() has resulted in the "required" CSS class name being applied to those elements.</p>
<h3>Decorators</h3>
<p> If you do not want to use a definition list, then this can easily be changed using decorators. For instance, adding the following to the bottom of the form's constructor after the addElements() call:</p>
<pre class="phpcode"><span style="color: #0000BB">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">clearDecorators</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addDecorator</span><span style="color: #007700">(</span><span style="color: #DD0000">'FormElements'</span><span style="color: #007700">)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">addDecorator</span><span style="color: #007700">(</span><span style="color: #DD0000">'HtmlTag'</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">'tag'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'&lt;ul&gt;'</span><span style="color: #007700">))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;</span><span style="color: #0000BB">addDecorator</span><span style="color: #007700">(</span><span style="color: #DD0000">'Form'</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setElementDecorators</span><span style="color: #007700">(array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'ViewHelper'</span><span style="color: #007700">),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'Errors'</span><span style="color: #007700">),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'Description'</span><span style="color: #007700">),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'Label'</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">'separator'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'&nbsp;'</span><span style="color: #007700">)),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'HtmlTag'</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">'tag'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'li'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'class'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'element-group'</span><span style="color: #007700">)),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;buttons&nbsp;do&nbsp;not&nbsp;need&nbsp;labels
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$submit</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setDecorators</span><span style="color: #007700">(array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'ViewHelper'</span><span style="color: #007700">),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'Description'</span><span style="color: #007700">),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'HtmlTag'</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">'tag'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'li'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'class'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'submit-group'</span><span style="color: #007700">)),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;));
</span>
</span></code></pre>
<p>will result in the form being marked up as an unsigned list with each label and element pair inside each &lt;li&gt;. The marked up form looks like this:</p>
<pre class="phpcode"><span style="color: #0000BB">
</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">form&nbsp;name</span><span style="color: #007700">=</span><span style="color: #DD0000">"contact_us"&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">=</span><span style="color: #DD0000">"contact_us"&nbsp;</span><span style="color: #0000BB">enctype</span><span style="color: #007700">=</span><span style="color: #DD0000">"application/x-www-form-urlencoded"&nbsp;</span><span style="color: #0000BB">action</span><span style="color: #007700">=</span><span style="color: #DD0000">""&nbsp;</span><span style="color: #0000BB">method</span><span style="color: #007700">=</span><span style="color: #DD0000">"post"</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">ul</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">li&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"element-group"</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">label&nbsp;</span><span style="color: #007700">for=</span><span style="color: #DD0000">"title"&nbsp;</span><span style="color: #0000BB">tag</span><span style="color: #007700">=</span><span style="color: #DD0000">""&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"required"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Title</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">label</span><span style="color: #007700">&gt;&nbsp;
&lt;</span><span style="color: #0000BB">select&nbsp;name</span><span style="color: #007700">=</span><span style="color: #DD0000">"title"&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">=</span><span style="color: #DD0000">"title"</span><span style="color: #007700">&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">option&nbsp;value</span><span style="color: #007700">=</span><span style="color: #DD0000">"mr"&nbsp;</span><span style="color: #0000BB">label</span><span style="color: #007700">=</span><span style="color: #DD0000">"Mr"&nbsp;</span><span style="color: #0000BB">selected</span><span style="color: #007700">=</span><span style="color: #DD0000">"selected"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Mr</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">option</span><span style="color: #007700">&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">option&nbsp;value</span><span style="color: #007700">=</span><span style="color: #DD0000">"mrs"&nbsp;</span><span style="color: #0000BB">label</span><span style="color: #007700">=</span><span style="color: #DD0000">"Mrs"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Mrs</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">option</span><span style="color: #007700">&gt;
&lt;/</span><span style="color: #0000BB">select</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">li&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"element-group"</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">label&nbsp;</span><span style="color: #007700">for=</span><span style="color: #DD0000">"firstName"&nbsp;</span><span style="color: #0000BB">tag</span><span style="color: #007700">=</span><span style="color: #DD0000">""&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"required"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">First&nbsp;name</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">label</span><span style="color: #007700">&gt;&nbsp;
&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"text"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"firstName"&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">=</span><span style="color: #DD0000">"firstName"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">""</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">li&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"element-group"</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">label&nbsp;</span><span style="color: #007700">for=</span><span style="color: #DD0000">"lastName"&nbsp;</span><span style="color: #0000BB">tag</span><span style="color: #007700">=</span><span style="color: #DD0000">""&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"required"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Last&nbsp;name</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">label</span><span style="color: #007700">&gt;&nbsp;
&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"text"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"lastName"&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">=</span><span style="color: #DD0000">"lastName"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">""</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">li&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"element-group"</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">label&nbsp;</span><span style="color: #007700">for=</span><span style="color: #DD0000">"email"&nbsp;</span><span style="color: #0000BB">tag</span><span style="color: #007700">=</span><span style="color: #DD0000">""&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"required"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Email&nbsp;address</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">label</span><span style="color: #007700">&gt;&nbsp;
&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"text"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"email"&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">=</span><span style="color: #DD0000">"email"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">""</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">li&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">"submti-group"</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"submit"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"submit"&nbsp;</span><span style="color: #0000BB">id</span><span style="color: #007700">=</span><span style="color: #DD0000">"submit"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">"Contact&nbsp;us"</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">ul</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">form</span><span style="color: #007700">&gt;
</span>
</span></code></pre>
<p>As you can see, the decorator system makes it easy to render our form in the manner we wish. As a note of warning, pay attention to the order of the decorators as it matters! </p>
<h3>Processing</h3>
<p>The form is now displayed, so when the user presses the button we need to process it. In this example, this is done in the indexAction() controller function, so that the complete function looks like this with the addition in bold:</p>
<pre class="phpcode"><span style="color: #0000BB">
</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">IndexController&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">indexAction</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pageTitle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Zend_Form&nbsp;Example"</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bodyCopy&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;Please&nbsp;fill&nbsp;out&nbsp;this&nbsp;form.&lt;/p&gt;"</span><span style="color: #007700">;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$form&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">forms_ContactForm</span><span style="color: #007700">();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">isPost</span><span style="color: #007700">())&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$formData&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getPost</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$form</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">isValid</span><span style="color: #007700">(</span><span style="color: #0000BB">$formData</span><span style="color: #007700">))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'success'</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">populate</span><span style="color: #007700">(</span><span style="color: #0000BB">$formData</span><span style="color: #007700">);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">form&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</span>
</span></code></pre>
<p>Firstly we check the the request is a POST using the request object's <tt>isPost()</tt> function and then we use validate the submitted data using the form's <tt>isValid()</tt> function. If this returns true, then we have valid data and can process. If the data is not valid, we populate the form's elements with it and then redisplay so that the user can correct appropriately. The default decorators will display an error message next to each element that failed validation.</p>
<h3>Conclusion</h3>
<p>So there you have it. A very simple example of how to use Zend_Form.</p>
<p>Here's a zip file of this project: <a href="http://akrabat.com/wp-content/uploads/Zend_Form_Example.zip">Zend_Form_Example.zip</a> (It includes a snapshot of the trunk of the Zend Framework which is why it's 3MB big.)</p>
<p>It works for me, at least. </p>
<h3>Update</h3>
<p>With the release of Zend Framework 1.5, a change was made to the way buttons work and you should not put a label decorator on a button as the label property of a button is used for the text value of the button itself.</p>
<p>I have updated the code above to show that if you use setElementDecorators() you then need to reset the decorators for any buttons, the $submit button, in this case. I have also put back the Errors decorator when using an unsigned list.</p>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/php/simple-zend_form-example/feed/</wfw:commentRss>
		<slash:comments>109</slash:comments>
		</item>
		<item>
		<title>Simple Zend_Layout Example</title>
		<link>http://akrabat.com/php/simple-zend_layout-example/</link>
		<comments>http://akrabat.com/php/simple-zend_layout-example/#comments</comments>
		<pubDate>Tue, 11 Dec 2007 22:05:37 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://akrabat.com/2007/12/11/simple-zend_layout-example/</guid>
		<description><![CDATA[Zend_Layout is in the trunk now, so here's a super simple MVC example that shows it in action:

This example consists of three view files: the outer layout file, the index action view script and a right hand side bar. The remainder of this post describes the key files. If you just want to poke around [...]]]></description>
			<content:encoded><![CDATA[<p>Zend_Layout is in the trunk now, so here's a super simple MVC example that shows it in action:<br />
<img src="/wp-content/uploads/zend-layout-example-small.png" alt="Zend_Layout Example_Small.png" border="0" width="320" height="235" /></p>
<p>This example consists of three view files: the outer layout file, the index action view script and a right hand side bar. The remainder of this post describes the key files. If you just want to poke around with the code, then it's at the bottom, so page down now!</p>
<h2>Setting up</h2>
<p>This is the directory layout:</p>
<p><img src="/wp-content/uploads/zend-layout-directory1.png" alt="Zend_Layout Directory.png" border="0" width="259" height="353" /></p>
<p>As you can see, it's the standard layout and we have one controller, Index, with one action (also index). For good measure, I've thrown in a view helper to collect the base URL to reference the CSS file and also render into a sidebar.</p>
<p>Let's look at the bootstrap file, index.php, first:</p>
<pre class="phpcode"><span style="color: #0000BB">&lt;?php

define</span><span style="color: #007700">(</span><span style="color: #DD0000">'ROOT_DIR'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">)));

</span><span style="color: #FF8000">//&nbsp;Setup&nbsp;path&nbsp;to&nbsp;the&nbsp;Zend&nbsp;Framework&nbsp;files
</span><span style="color: #0000BB">set_include_path</span><span style="color: #007700">(</span><span style="color: #DD0000">'.'
</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PATH_SEPARATOR&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">ROOT_DIR</span><span style="color: #007700">.</span><span style="color: #DD0000">'/lib/'
</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PATH_SEPARATOR&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">get_include_path</span><span style="color: #007700">()
);

</span><span style="color: #FF8000">//&nbsp;Register&nbsp;the&nbsp;autoloader
</span><span style="color: #007700">require_once&nbsp;</span><span style="color: #DD0000">'Zend/Loader.php'</span><span style="color: #007700">;
</span><span style="color: #0000BB">Zend_Loader</span><span style="color: #007700">::</span><span style="color: #0000BB">registerAutoload</span><span style="color: #007700">();

</span><span style="color: #FF8000">//&nbsp;Initialise&nbsp;Zend_Layout's&nbsp;MVC&nbsp;helpers
</span><span style="color: #0000BB">Zend_Layout</span><span style="color: #007700">::</span><span style="color: #0000BB">startMvc</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'layoutPath'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">ROOT_DIR</span><span style="color: #007700">.</span><span style="color: #DD0000">'/app/views/layouts'</span><span style="color: #007700">));

</span><span style="color: #FF8000">//&nbsp;Run!
</span><span style="color: #0000BB">$frontController&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Zend_Controller_Front</span><span style="color: #007700">::</span><span style="color: #0000BB">getInstance</span><span style="color: #007700">();
</span><span style="color: #0000BB">$frontController</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addControllerDirectory</span><span style="color: #007700">(</span><span style="color: #0000BB">ROOT_DIR</span><span style="color: #007700">.</span><span style="color: #DD0000">'/app/controllers'</span><span style="color: #007700">);
</span><span style="color: #0000BB">$frontController</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">throwExceptions</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);
try&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$frontController</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dispatch</span><span style="color: #007700">();
}&nbsp;catch(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">nl2br</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">__toString</span><span style="color: #007700">());
}

</span>
</span></code></pre>
<p>This is a standard bootstrap with the exception that we initialise the Zend_Layout using the <tt>startMvc()</tt> function. This takes an array of options, but the one thing you really need to pass in is the directory to find the layout files. I've chosen app/views/layouts as it makes sense in this case. If you are using modules, then maybe app/layouts would be better.</p>
<h2>The controller</h2>
<p>The index controller contains two functions: init() to render the sidebar to a named response section for use in the layout and then the indexAction() which just puts some text into the view:</p>
<pre class="phpcode"><span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">IndexController&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Zend_Controller_Action
</span><span style="color: #007700">{
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">init</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Render&nbsp;sidebar&nbsp;for&nbsp;every&nbsp;action
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$response&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getResponse</span><span style="color: #007700">();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$response</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">'sidebar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">render</span><span style="color: #007700">(</span><span style="color: #DD0000">'sidebar.phtml'</span><span style="color: #007700">));&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">indexAction</span><span style="color: #007700">()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pageTitle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Zend&nbsp;Layout&nbsp;Example"</span><span style="color: #007700">;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bodyTitle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'&lt;h1&gt;Hello&nbsp;World!&lt;/h1&gt;'</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">view</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bodyCopy&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;Lorem&nbsp;ipsum&nbsp;dolor&nbsp;etc.&lt;/p&gt;"</span><span style="color: #007700">;
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</span>
</span></code></pre>
<h2>Two-step view</h2>
<p>The view is now two-step. This means that we split our HTML between multiple files. The first step is to render the "inner" scripts, such as the sidebar and the action specific scripts. Then we render the "outer", layout script which embeds the rendered "inner" scripts.</p>
<h3>The "inner" scripts</h3>
<p>The action view script, index/index.phtml is trivial as it just needs to display the text relevant to the index action only:</p>
<pre class="phpcode"><span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bodyTitle&nbsp;</span><span style="color: #007700">;</span><span style="color: #0000BB">?&gt;
&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bodyCopy&nbsp;</span><span style="color: #007700">;</span><span style="color: #0000BB">?&gt;
</span>
</span></code></pre>
<p>(told you it was simple!)</p>
<p>The sidebar.phtml is similarly, just the HTML required for our sidebar:</p>
<pre class="phpcode"><span style="color: #0000BB">
</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">h2</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Sidebar</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">h2</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">ul</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Item&nbsp;1</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Item&nbsp;2</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Item&nbsp;3</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Item&nbsp;4</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;
&lt;</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Item&nbsp;5</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">li</span><span style="color: #007700">&gt;
&lt;/</span><span style="color: #0000BB">ul</span><span style="color: #007700">&gt;
</span>
</span></code></pre>
<p>Again, nice and easy HTML, for this example at least!</p>
<h3>The layout script</h3>
<p>The layout script, layout.phtml, ties it all together. It contains the HTML that is common to all pages on our website and uses the special construct <tt>&lt;?php echo $this-&gt;layout()-&gt;content ?&gt;</tt> to render a named response segment. Note that the view renderer will render to "content" for the action controller's script.</p>
<p>The layout script looks like this:</p>
<pre class="phpcode">
&lt;!DOCTYPE&nbsp;html&nbsp;PUBLIC&nbsp;"-//W3C//DTD&nbsp;XHTML&nbsp;1.0&nbsp;Transitional//EN"&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html&nbsp;xmlns="http://www.w3.org/1999/xhtml"&nbsp;xml:lang="en"&nbsp;lang="en"&gt;
&lt;head&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta&nbsp;http-equiv="Content-Type"&nbsp;content="text/html;&nbsp;charset=utf-8"&nbsp;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">escape</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pageTitle</span><span style="color: #007700">);&nbsp;</span><span style="color: #0000BB">?&gt;</span>&lt;/title&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;link&nbsp;rel="stylesheet"&nbsp;href="<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">baseUrl</span><span style="color: #007700">();&nbsp;</span><span style="color: #0000BB">?&gt;</span>/main.css"&nbsp;type="text/css"&gt;
&lt;/head&gt;
&lt;body&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div&nbsp;id="content"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">layout</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">content&nbsp;?&gt;
</span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div&nbsp;id="sidebar"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">layout</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">sidebar</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">?&gt;
</span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</span></code></pre>
<p>For simplicity, we use the baseUrl view helper to retrieve the base URL from the request (via the Front Controller) and we render our two named response segments (content and sidebar) using the view helper layout() which is provide by Zend_Layout. </p>
<h2>Conclusion</h2>
<p>That's all there is to it. We could have use the partial() view helper to render the sidebar script and coming soon to a svn tree near you is other useful view helpers such as headScript() and headTitle() which will make the &lt;head&gt; section easier to manage.</p>
<p>Here's a zip file of this project:  <a href="/wp-content/uploads/Zend_Layout_Example.zip">Zend_Layout_Example.zip</a>(It includes a snapshot of the trunk of the Zend Framework which is why it's 3MB big.)</p>
<p>It works for me, at least.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/php/simple-zend_layout-example/feed/</wfw:commentRss>
		<slash:comments>100</slash:comments>
		</item>
	</channel>
</rss>
