<?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>Andrew Vayanis &#187; Zend_Form Tables</title>
	<atom:link href="http://www.vayanis.com/tag/zend_form-tables/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vayanis.com</link>
	<description>Developer, Gamer, Thinker</description>
	<lastBuildDate>Wed, 19 May 2010 15:58:46 +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>Using Zend_Form with Zend_Config</title>
		<link>http://www.vayanis.com/2008/03/17/using-zend_form-with-zend_config/</link>
		<comments>http://www.vayanis.com/2008/03/17/using-zend_form-with-zend_config/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 05:35:41 +0000</pubDate>
		<dc:creator>Andrew Vayanis</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Guides]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_Form]]></category>
		<category><![CDATA[Zend_Form Tables]]></category>

		<guid isPermaLink="false">http://www.vayanis.com/2008/03/17/using-zend_form-with-zend_config/</guid>
		<description><![CDATA[In my previous post Zend Framework, A First Look, I discussed the lacking nature of ZF&#8217;s documentation, in particular, with regards to Zend_Form. I have since then learned that this is partly due to the fact that Zend_Form is a relatively new component. However, I still wanted to make use of Zend_Form in my current [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post <a href="http://www.vayanis.com/2008/03/14/zend-framework-a-first-look/">Zend Framework, A First Look</a>, I discussed the lacking nature of ZF&#8217;s documentation, in particular, with regards to Zend_Form.  I have since then learned that this is partly due to the fact that Zend_Form is a relatively new component.  However, I   still wanted to make use of Zend_Form in my current project and decided to trudge through the learning curve of creating a simple custom login form in conjunction with Zend_Config; the end result being an easy to maintain, custom form and this guide.  Hopefully, this guide will make it easier for anyone else looking to take advantage of this very cool feature.<br />
<span id="more-28"></span><br />
Before I begin talking about how Zend_Form works, I think it is important to explain a bit about Zend_Config and how an INI file is translated into PHP.  As the <a href="http://framework.zend.com/manual/en/zend.config.html">Zend_Config Documentation</a> points out, it makes use of PHP native parse_ini_file, however, this only goes so far as reading in the contents of the file and turning it into a 1 dimensional associative array(2 if a section is provided).  Zend_Config extends this behavior by taking the keys of each association and breaking them down further based upon the specified <em>key separator (default is &#8216;.&#8217;).</em> and turning the remainder into arrays.  In the end, Zend_Config takes a series of declarations such as the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">login.elements.username.type <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;text&quot;</span></pre></div></div>

<p>And turns it into:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>login<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>elements<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>username<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                        <span style="color: #009900;">&#40;</span>
                            <span style="color: #009900;">&#91;</span>type<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> text
                        <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Now, back to Zend_Form.  I think my biggest gripe with the documentation provided for Zend_Form is that it does not explain how anything is working behind the scenes.  For instance, it does not explain that Zend_Form normalizes config statements by prepending &#8216;set&#8217; when making calls to member methods.  This is invaluable as it explains why</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setElementDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ViewHelper'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>in PHP becomes</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">    elementDecorators.helper <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;ViewHelper&quot;</span></pre></div></div>

<p>in a Zend_Config_Ini.  Internally, elementDecorators tells Zend_Form to call setElementDecorators();  This small example also shows why it is important to know how Zend_Config_Ini actually translates an INI to PHP&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>elementDecorators<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>helper<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> ViewHelper
        <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>As we can see this declares an array of decorators to be passed into setElementDecorators();  The keys, in this case &#8220;helper&#8221;, do not necessarily matter as it is only needed to index the array, the values are what matter, as that is what is passed and used within Zend_Form.  Similarly, it is important to know that almost all aspects of form and form elements, excluding form element type, are implemented internally as options.  The following is an example INI file with comments explaining particular sections.</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">; A basic Form config</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">; First level attributes are automatically treated as options.</span>
    <span style="color: #000099;">action</span> <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;login/submit&quot;</span>
    <span style="color: #000099;">method</span> <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;post&quot;</span>
    <span style="color: #000099;">id</span> <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;login&quot;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">; Form Decorators</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">; Again these decorators are treated as options of the main form.</span>
    decorators.elements.decorator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;FormElements&quot;</span>
    decorators.table.decorator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;HtmlTag&quot;</span>
    decorators.table.options.tag <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;table&quot;</span>
    decorators.form.decorator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Form&quot;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">; Username Element</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">; Form Element Type is specified explicityly, not as an option.</span>
    elements.username.type <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;text&quot;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">; Username attributes (Label, Required, and validators are all</span>
    <span style="color: #666666; font-style: italic;">; declared as options).</span>
    elements.username.options.label <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Username:&quot;</span>
    elements.username.options.required <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> true</span>
    elements.username.options.validators.alnum.validator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;alnum&quot;</span>
    elements.username.options.validators.regex.validator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;regex&quot;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">; Validator parameters such as pattern to regex are also</span>
    <span style="color: #666666; font-style: italic;">; declared as options. I have not looked into it, but I</span>
    <span style="color: #666666; font-style: italic;">; assume this is due to the way Zend_Validate handles its</span>
    <span style="color: #666666; font-style: italic;">; own constructors.</span>
    elements.username.options.validators.regex.options.pattern <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;/^[a-z]/i&quot;</span>
    elements.username.options.validators.strlen.validator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;StringLength&quot;</span>
    elements.username.options.validators.strlen.options.min <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;5&quot;</span></pre></div></div>

<p>Bringing it all together, the following is the first version of my login form.</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">    <span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>login<span style="">&#93;</span></span>
    <span style="color: #666666; font-style: italic;">; General Form Information</span>
    login.action <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;login/submit&quot;</span>
    login.method <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;post&quot;</span>
    login.id <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;login&quot;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">; Form Decorators</span>
    login.decorators.elements.decorator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;FormElements&quot;</span>
    login.decorators.table.decorator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;HtmlTag&quot;</span>
    login.decorators.table.options.tag <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;table&quot;</span>
    login.decorators.form.decorator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Form&quot;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">; Username Element</span>
    login.elements.username.type <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;text&quot;</span>
    login.elements.username.options.label <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Username:&quot;</span>
    login.elements.username.options.required <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> true</span>
    login.elements.username.options.validators.alnum.validator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;alnum&quot;</span>
    login.elements.username.options.validators.regex.validator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;regex&quot;</span>
    login.elements.username.options.validators.regex.options.pattern <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;/^[a-z]/i&quot;</span>
    login.elements.username.options.validators.strlen.validator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;StringLength&quot;</span>
    login.elements.username.options.validators.strlen.options.min <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;5&quot;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">; Password Element</span>
    login.elements.password.type <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;password&quot;</span>
    login.elements.password.options.label <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Password:&quot;</span>
    login.elements.password.options.required <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> true</span>
    login.elements.password.options.validators.strlen.validator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;StringLength&quot;</span>
    login.elements.password.options.validators.strlen.options.min <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;6&quot;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">; Submit Form Element</span>
    login.elements.submit.type <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;submit&quot;</span>
    login.elements.submit.options.label <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Submit&quot;</span>
&nbsp;
    login.elementDecorators.viewHelper <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;ViewHelper&quot;</span>
    login.elementDecorators.errors <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Errors&quot;</span>
&nbsp;
    login.elementDecorators.tableData.decorator.td <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;HtmlTag&quot;</span>
    login.elementDecorators.tableData.options.tag <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;td&quot;</span>
    login.elementDecorators.tableData.options.class <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;test2&quot;</span>
&nbsp;
    login.elementDecorators.label.decorator <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Label&quot;</span>
    login.elementDecorators.label.options.tag <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;td&quot;</span>
&nbsp;
    login.elementDecorators.tableRow.decorator.tr <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;HtmlTag&quot;</span>
    login.elementDecorators.tableRow.options.tag <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;tr&quot;</span></pre></div></div>

<p>This creates a login form with a username and password field along with a submit button.  However, instead of being wrapped in a definition list using definition titles for labels and definition data for input fields, I have wrapped the labels and input fields within a table similar to the google login form.  Notice though, that I have sectioned this form using <em>[login]</em>.  Tthis is so that I can define all my necessary forms in one forms.ini file and load each form by section.  I have also prepended each declaration with <em>login.</em> as a way to keep my form definitions organized and easy to read (to me at least).  </p>
<p>Concluding this guide, I would like to reiterate, how useful I think Zend_Form is as it solves one of the most tedious process for any PHP project; form creation and validation.  I hope it continues to evolve and mature, and I hope others find it as handy as I have.  Although, if anyone has an easier solution, please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vayanis.com/2008/03/17/using-zend_form-with-zend_config/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)

Served from: www.vayanis.com @ 2010-09-09 10:05:03 -->