<?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>The Zend Framework in Practice &#187; Zend_Validate</title>
	<atom:link href="http://zf.gm-ram.com/topics/zend_validate/feed/" rel="self" type="application/rss+xml" />
	<link>http://zf.gm-ram.com</link>
	<description>Developing Web Applications with the Zend Framework</description>
	<lastBuildDate>Mon, 02 Jan 2012 10:20:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The Login Page: The Login Form</title>
		<link>http://zf.gm-ram.com/posts/the-login-page-the-login-form/</link>
		<comments>http://zf.gm-ram.com/posts/the-login-page-the-login-form/#comments</comments>
		<pubDate>Mon, 15 Nov 2010 22:51:07 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Form]]></category>
		<category><![CDATA[Zend_Validate]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=817</guid>
		<description><![CDATA[The Login Form is a simple form, consisting of three elements: the username field, the password field and the login button. The form class is derived from an abstract base class of my devising. This contains the functionality shared between all forms in an application. The abstract base class is itself derived from Zend_Form. The [...]]]></description>
			<content:encoded><![CDATA[<p>The Login Form is a simple form, consisting of three elements: the username field, the password field and the login button.<span id="more-817"></span></p>
<p>The form class is derived from an abstract base class of my devising. This contains the functionality shared between all forms in an application. The abstract base class is itself derived from Zend_Form.</p>
<p>The abstract base class is defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> Application_Form_Abstract <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFieldDecorators<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ViewHelper'</span><span style="color: #339933;">,</span> 
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HtmlTag'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'dd'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Label'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'dt'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFirstFormError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessages</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$field</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$messages</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$messages</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$message</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'field'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$error</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The class supplies contains two functions, both of which are connected to the rendering of error messages.</p>
<p>The first function modifies the standard field decorators to remove the error decorator, as I don&#8217;t usually want any error messages displayed beside the field. Instead, I prefer to show the first error message at the top of the form; it is the task of the second function to retrieve this.</p>
<p>The actual Login form class is straighforward:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Application_Form_Login <span style="color: #000000; font-weight: bold;">extends</span> CapArgo_Form_Abstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttrib</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'login_form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Username:'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'maxLength'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">64</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'required'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'filters'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'StringTrim'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'decorators'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFieldDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'errorMessages'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please enter your username'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Password:'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'maxLength'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">64</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'required'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'decorators'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFieldDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'errorMessages'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please enter your password'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'submit'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'submit'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'ignore'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Login'</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above code needs little explanation.</p>
<p>Note that there is a call to <strong>$this->getFieldDecorators()</strong> in order to attach the proper decorators to the input fields.</p>
<p>Also, note that the <strong>required</strong> option is set to true, and an error message is supplied, should the user fail to enter either a username or password; this uses Zend_Validate_NotEmpty to check that the user has entered a value.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-login-page-the-login-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking that a password is repeated correctly using Zend_Validate_Identical</title>
		<link>http://zf.gm-ram.com/posts/checking-that-a-password-is-repeated-correctly-using-zend_validate_identical/</link>
		<comments>http://zf.gm-ram.com/posts/checking-that-a-password-is-repeated-correctly-using-zend_validate_identical/#comments</comments>
		<pubDate>Sun, 01 Aug 2010 09:41:44 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Form]]></category>
		<category><![CDATA[Zend_Validate]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=664</guid>
		<description><![CDATA[When a user is asked to enter a new password, it is customary to ask the user to repeat it, in order to reduce the risk of it being entered incorrectly. In the Zend Framework, there is a validator that can check whether two items are identical, but using it with Zend_Form is tricky, because [...]]]></description>
			<content:encoded><![CDATA[<p>When a user is asked to enter a new password, it is customary to ask the user to repeat it, in order to reduce the risk of it being entered incorrectly. In the Zend Framework, there is a validator that can check whether two items are identical, but using it with Zend_Form is tricky, because it needs the values of two fields to make the comparison, while validators are attached to a single field, independently of the others. This article looks at how to use Zend_Validate_Identical with Zend_Form.<span id="more-664"></span></p>
<p>Let us assume that we are adding our password and password repeat fields in the <strong>init</strong> method of our form class, which is derived from Zend_Form:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Password:'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'maxLength'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">64</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'required'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'decorators'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFieldDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'errorMessages'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please enter a password'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'repeat_password'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Repeat Password:'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'maxLength'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">64</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'required'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'decorators'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFieldDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'errorMessages'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please repeat the password correctly'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note that both fields have the <strong>required</strong> option set to true; this is necessary event in the case of the <strong>repeat_password</strong> field, where we will be adding another validator later on. Also note that we have specified custom error messages for both fields, since the default messages are not appropriate.</p>
<p>The second and final step is to override the <strong>isValid</strong> method of the form class, and add the validator there:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isValid<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'repeat_password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>
      <span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_Identical<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>It is necessary to do it at this point, since it is only then that we have access to the value of the <strong>password</strong> field.</p>
<p>The form will now fail to validate if the password is not repeated correctly, and the appropriate error message will be generated.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/checking-that-a-password-is-repeated-correctly-using-zend_validate_identical/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Contact Page: The Contact Form</title>
		<link>http://zf.gm-ram.com/posts/the-contact-page-the-contact-form/</link>
		<comments>http://zf.gm-ram.com/posts/the-contact-page-the-contact-form/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 16:49:45 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Form]]></category>
		<category><![CDATA[Zend_Validate]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=452</guid>
		<description><![CDATA[Although the example website currently only has one form, the contact form, other forms may be added to it in the future; therefore, in implementing the form, I created an abstract base class for site forms, which contains the functionality shared by all forms. The abstract class is called Default_Form_Abstract and is stored in the [...]]]></description>
			<content:encoded><![CDATA[<p>Although the example website currently only has one form, the contact form, other forms may be added to it in the future; therefore, in implementing the form, I created an abstract base class for site forms, which contains the functionality shared by all forms.<span id="more-452"></span></p>
<p>The abstract class is called Default_Form_Abstract and is stored in the <em>application/forms</em> directory. It is derived from Zend Form.</p>
<p>The skeleton abstract class declaration is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> Default_Form_Abstract <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// The class methods and variables are declared here.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The Zend_Form module uses the decorator pattern to define the markup used to render a form. The base form class uses decorators to render the forms as tables. The decorators used to render the form fields are contained in the following two variables:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_fieldDecorators</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'ViewHelper'</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'HtmlTag'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'td'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'element'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Label'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'td'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'row'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'HtmlTag'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'tr'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_buttonDecorators</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'ViewHelper'</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'HtmlTag'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'td'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'element'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'HtmlTag'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'td'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'placement'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'prepend'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'row'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'HtmlTag'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'tr'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>These decorators are applied to field elements declared in derived classes using the following two methods. The first is used for most fields, which have labels; the second is used for buttons such as the submit button, which don&#8217;t have a label:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setFieldDecorators<span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$field</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_fieldDecorators<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setButtonDecorators<span style="color: #009900;">&#40;</span><span style="color: #000088;">$button</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$button</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_buttonDecorators<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The overall styling for the form is set in the init method:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Set the form decorators</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDecorators</span><span style="color: #009900;">&#40;</span>
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'FormElements'</span><span style="color: #339933;">,</span>
        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HtmlTag'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'table'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'Form'</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally, there is a method used to retrieve the first error message generated by a failed validation:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFirstFormError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessages</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$field</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$messages</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$messages</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$message</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'field'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$error</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This method is necessary as normally details of all fields that have failed validation are returned, but the usual (and my preferred) practice is to only report the first field that has failed validation. This has always struck me as less confusing for the user.</p>
<p>The abstract class is called Default_Form_Abstract and is stored in the <em>application/forms</em> directory. It is derived from Zend Form.</p>
<p>The skeleton abstract class declaration is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Default_Form_Contact <span style="color: #000000; font-weight: bold;">extends</span> Default_Form_Abstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// The code used to initialise the form is inserted here.</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The init method starts by calling the init method of the parent class:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    parent<span style="color: #339933;">::</span><span style="color: #004000;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is essential as the parent method sets up the form decorators.</p>
<p>Next the method sets the form ID:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Set the form ID</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttrib</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'contact_form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then the method sets the form submission method to POST:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Submit the form using a POST request</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The first form element is the <strong>Name</strong> field, which is a normal text field.</p>
<p>The script first creates an appropriate form element:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Name field</span>
    <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span>
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Your name:'</span><span style="color: #339933;">,</span> 
        <span style="color: #0000ff;">'required'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
        <span style="color: #0000ff;">'filters'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'StringTrim'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'maxlength'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">64</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The first parameter is the field name/id.</p>
<p>The second parameter is an array of additional settings. Here these are:</p>
<ul>
<li><strong>label</strong>: This specifies the label attached to the field.</li>
<li><strong>required</strong>: This specifies whether the field is required; in this case it is, so the field will fail validation if it is not.</li>
<li><strong>filters</strong>: This specifies the filters to apply to the field; in this case, it is the trim filter, used to remove leading and trailing space.</li>
<li><strong>maxlength</strong>: This specifies the maximum length of the data than can be entered in the field.</li>
</ul>
<p>The script then adds the error message to display, if the field fails validation:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$name</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setErrorMessages</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please enter your name.'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The script then adds the appropriate decorators to the field, to ensure that it is rendered as a table row:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFieldDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Finally the field is added to the form:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>A similar process is followed for the other form fields:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// E-mail field</span>
    <span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span> 
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Your e-mail address:'</span><span style="color: #339933;">,</span> 
        <span style="color: #0000ff;">'required'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
        <span style="color: #0000ff;">'filters'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'StringTrim'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
        <span style="color: #0000ff;">'validators'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'EmailAddress'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'maxlength'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">255</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setErrorMessages</span><span style="color: #009900;">&#40;</span>
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please enter a valid e-mail address.'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFieldDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Message field</span>
    <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Textarea<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">,</span> 
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'label'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Your message:'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'required'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'rows'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'cols'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">50</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setErrorMessages</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please enter a message.'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFieldDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Submit button</span>
    <span style="color: #000088;">$submit</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Submit<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'submit'</span><span style="color: #339933;">,</span> 
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'ignore'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
        <span style="color: #0000ff;">'label'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Send'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'class'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ui-state-default ui-corner-all'</span>
      <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setButtonDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$submit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$submit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The e-mail field has a specific validator applied to it, rather than the general <strong>required</strong> setting.</p>
<p>The text area has its <strong>rows</strong> and <strong>columns</strong> set, rather than a <strong>maxlength</strong>.</p>
<p>Note that the submit button has different decorators applied to it, as unlike the other fields, it does not have a label.</p>
<p>In my next most, I shall finish my coverage of the contact page by looking at the class used to send the contact e-mail.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-contact-page-the-contact-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

