<?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>Wed, 01 Sep 2010 14:33:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<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>Simple Zend_Form Example</title>
		<link>http://akrabat.com/tutorial/simple-zend_form-example/</link>
		<comments>http://akrabat.com/tutorial/simple-zend_form-example/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 21:01:21 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Tutorial]]></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 [...]]]></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/tutorial/simple-zend_form-example/feed/</wfw:commentRss>
		<slash:comments>109</slash:comments>
		</item>
		<item>
		<title>Simple Zend_Layout Example</title>
		<link>http://akrabat.com/tutorial/simple-zend_layout-example/</link>
		<comments>http://akrabat.com/tutorial/simple-zend_layout-example/#comments</comments>
		<pubDate>Tue, 11 Dec 2007 22:05:37 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Tutorial]]></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 [...]]]></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/tutorial/simple-zend_layout-example/feed/</wfw:commentRss>
		<slash:comments>100</slash:comments>
		</item>
		<item>
		<title>Notes on using Oracle with my tutorial</title>
		<link>http://akrabat.com/tutorial/notes-on-using-oracle-with-my-tutorial/</link>
		<comments>http://akrabat.com/tutorial/notes-on-using-oracle-with-my-tutorial/#comments</comments>
		<pubDate>Wed, 06 Jun 2007 05:43:14 +0000</pubDate>
		<dc:creator>Rob...</dc:creator>
				<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://akrabat.com/2007/06/06/notes-on-using-oracle-with-my-tutorial/</guid>
		<description><![CDATA[William Graham has been playing with the Zend Framework and Oracle using my tutorial. His notes are very useful if you want to get my admittedly MySQL-centric tutorial working with Oracle. I've come across that uppercase field name thing before in an application I wrote at work that needed to transfer some data from Oracle [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://liamgraham.wordpress.com">William Graham</a> has been <a href="http://liamgraham.wordpress.com/2007/06/05/oracle-modifications-for-rob-allens-zend-framework-tutorial/">playing</a> with the Zend Framework and Oracle using my tutorial.</p>
<p>His notes are very useful if you want to get my admittedly MySQL-centric tutorial working with Oracle. I've come across that uppercase field name thing before in an application I wrote at work that needed to transfer some data from Oracle to SQL Server. Took me a while to work out what was going on. </p>
<p>I'm a little confused about the case of form element names in $_POST, so will have to test that at some point.</p>
]]></content:encoded>
			<wfw:commentRss>http://akrabat.com/tutorial/notes-on-using-oracle-with-my-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
