<?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"
	>

<channel>
	<title>Springboard Software</title>
	<atom:link href="http://www.springboard-software.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.springboard-software.com</link>
	<description>Web Development With a Twist</description>
	<pubDate>Thu, 24 Jul 2008 16:36:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>Choosing Ruby or PHP over Java?</title>
		<link>http://www.springboard-software.com/2008/04/02/choosing-ruby-or-php-over-java/</link>
		<comments>http://www.springboard-software.com/2008/04/02/choosing-ruby-or-php-over-java/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 21:40:41 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Java Development]]></category>

		<category><![CDATA[PHP Development]]></category>

		<category><![CDATA[Php Development]]></category>

		<category><![CDATA[Ruby On Rails]]></category>

		<guid isPermaLink="false">http://www.springboard-software.com/2008/04/02/choosing-ruby-or-php-over-java/</guid>
		<description><![CDATA[In a recent article on CRM Developers Journal, Coach Wei asks: Why Do &#8216;Cool Kids&#8217; Choose Ruby or PHP to Build Websites Instead of Java?
— Here is a question that I have been pondering on and off for quite a while: Why do &#8216;cool kids&#8217; choose Ruby or PHP to build websites instead of Java? [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent article on CRM Developers Journal, Coach Wei asks: <blockquote><a href="http://crm.sys-con.com/read/457324.htm">Why Do &#8216;Cool Kids&#8217; Choose Ruby or PHP to Build Websites Instead of Java?</a>
— Here is a question that I have been pondering on and off for quite a while: Why do &#8216;cool kids&#8217; choose Ruby or PHP to build websites instead of Java? I have to admit that I do not have an answer. Why do I even care? Because I am a Java developer. Like many Java developers, I get along with Java well. Not only the language itself, but the development environments (Eclipse for example), step-by-step debugging helper, wide availability of libraries and code snippets, and the readily accessible information on almost any technical question I may have on Java via Google. Last but not least, I go to JavaOne and see 10,000 people that talk and walk just like me.</blockquote>
</p>

<p>Coach then goes on to basically not answer his own question, although he offers a couple ideas about the popularity of Ruby and Php in what some folks at Sun see as a Java world.</p>

<p>I don&#8217;t think there&#8217;s any surprise about why the Ruby and Php are so popular.  Unlike Java, when you write in a scripting language, you get immediate gratification and feedback about your web app.  Many industrial-strength Java web applications are complex enough to require build and deploy scripts, multiple deployment tiers, and really beefy workstations to run local developer instances.  Very little of that&#8217;s required with Ruby or PHP.  This translates into something viscerally satisfying to web developers.  The ability to change something, and then immediately see that change in the working application.  Don&#8217;t like the change?  Do a little tap, tap, tap on the keyboard and then reload your browser.  Boom.  Now you have a new version of the page.</p>

<p>This dynamic of small, incremental changes leading to immediate application feedback IS available in Java, using JSP.  The problem here is overhead.  In most shared hosting environments running Linux, there is usually no stable Servlet/JSP engine set up.  This means the developer - oft times what corporate types like me call &#8220;Junior Developers&#8221; - are required to install the servlet engine themselves.  That, or use the web development tools that are already available.</p>

<p>Like PHP.</p>]]></content:encoded>
			<wfw:commentRss>http://www.springboard-software.com/2008/04/02/choosing-ruby-or-php-over-java/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Springboard Site Gets a Facelift</title>
		<link>http://www.springboard-software.com/2008/03/29/the-springboard-site-gets-a-facelift/</link>
		<comments>http://www.springboard-software.com/2008/03/29/the-springboard-site-gets-a-facelift/#comments</comments>
		<pubDate>Sun, 30 Mar 2008 00:15:29 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[SpringBoard News]]></category>

		<category><![CDATA[Site Redesign]]></category>

		<category><![CDATA[Springboard Software Web Site]]></category>

		<guid isPermaLink="false">http://www.springboard-software.com/2008/03/29/the-springboard-site-gets-a-facelift/</guid>
		<description><![CDATA[Well, we finally took the plunge, and started refitting the look and feel of the Springboard Software site.  It had been pretty minimalist for quite a while, and we decided we needed a little more meat on dem bones.  Look around, and let us know if you like what you see. 


]]></description>
			<content:encoded><![CDATA[<p>Well, we finally took the plunge, and started refitting the look and feel of the Springboard Software site.  It had been pretty minimalist for quite a while, and we decided we needed a little more meat on dem bones.  Look around, and let us know if you like what you see. 
</p>
<!--adsensestart-->
]]></content:encoded>
			<wfw:commentRss>http://www.springboard-software.com/2008/03/29/the-springboard-site-gets-a-facelift/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Flex Goes Open Source</title>
		<link>http://www.springboard-software.com/2007/04/26/flex-goes-open-source/</link>
		<comments>http://www.springboard-software.com/2007/04/26/flex-goes-open-source/#comments</comments>
		<pubDate>Fri, 27 Apr 2007 00:25:58 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Flex Development]]></category>

		<category><![CDATA[Object Oriented Analysis &#038; Design]]></category>

		<guid isPermaLink="false">http://blog.springboard-software.com/2007/04/26/flex-goes-open-source/</guid>
		<description><![CDATA[Adobe announced today that it will release the Flex SDK under the Mozilla Public License: 



 Adobe Flex goes open source by ZDNet&#8217;s Ryan Stewart &#8212; Adobe is announcing the open source release of the Flex SDK. Included are all of the libraries and tools that are offered for free as part of the SDK. [...]]]></description>
			<content:encoded><![CDATA[<p>Adobe announced today that it will release the Flex SDK under the Mozilla Public License: 
</p>


<blockquote><a href="http://blogs.zdnet.com/Stewart/?p=353" rel="bookmark" title="Permalink"> Adobe Flex goes open source</a> by <a href="http://zdnet.com">ZDNet</a>&#8217;s Ryan Stewart &#8212; Adobe is announcing the open source release of the Flex SDK. Included are all of the libraries and tools that are offered for free as part of the SDK. It&#8217;s part of a continuing trend by Adobe to be more open and it&#8217;s going to give developers access to the guts of Flex. This should mean better code, better components, and lots of good things for Flex developers and the Flex community.</blockquote>



<p>That&#8217;s good news for developers - and even better news for companies wanting to deploy RIA as part of their web offerings. </p>]]></content:encoded>
			<wfw:commentRss>http://www.springboard-software.com/2007/04/26/flex-goes-open-source/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Example Amazon Search Tool in Flex 2.0</title>
		<link>http://www.springboard-software.com/2007/04/10/example-amazon-search-tool-in-flex-20/</link>
		<comments>http://www.springboard-software.com/2007/04/10/example-amazon-search-tool-in-flex-20/#comments</comments>
		<pubDate>Tue, 10 Apr 2007 17:18:06 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Flex Development]]></category>

		<category><![CDATA[SpringBoard News]]></category>

		<guid isPermaLink="false">http://blog.springboard-software.com/2007/04/10/example-amazon-search-tool-in-flex-20/</guid>
		<description><![CDATA[Previously, we spent a bit of time talking about how to access the Amazon ItemSearch web service in Flex 2.0.  As an example of what you can do with this technology, we have just included a slightly modified version of this Flex application on the left navigator.  


The new version supports:

	Passing your AWS [...]]]></description>
			<content:encoded><![CDATA[<p>Previously, we spent a bit of time talking about <a href="http://blog.springboard-software.com/2007/03/28/amazon-itemsearch-in-flex/">how to access the Amazon ItemSearch web service in Flex 2.0</a>.  As an example of what you can do with this technology, we have just included a slightly modified version of this Flex application on the left navigator.  
</p>

<p>The new version supports:
<ul>
	<li>Passing your AWS key as a parameter</li>
	<li>Fits in a standard 160X600 Skyscraper</li>
	<li>Searching for Books, DVDs, Software, and Video Games</li>

</ul>
</p>

<p>Go ahead, take it for a spin.</p>

<!--adsensestart-->
]]></content:encoded>
			<wfw:commentRss>http://www.springboard-software.com/2007/04/10/example-amazon-search-tool-in-flex-20/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Java, WS-BPEL and SOA</title>
		<link>http://www.springboard-software.com/2007/04/09/java-ws-bpel-and-soa/</link>
		<comments>http://www.springboard-software.com/2007/04/09/java-ws-bpel-and-soa/#comments</comments>
		<pubDate>Tue, 10 Apr 2007 02:40:15 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Java Development]]></category>

		<category><![CDATA[Object Oriented Analysis &#038; Design]]></category>

		<category><![CDATA[SOA]]></category>

		<category><![CDATA[WS-BPEL]]></category>

		<guid isPermaLink="false">http://blog.springboard-software.com/2007/04/09/java-ws-bpel-and-soa/</guid>
		<description><![CDATA[There's an <a href="http://java.sys-con.com/read/358049.htm" target="_blank">interesting read</a> on JDJ about leveraging Java, BPEL and SOA to deliver "Composite" (a term we like) applications in Business time.]]></description>
			<content:encoded><![CDATA[There&#8217;s an <a href="http://java.sys-con.com/read/358049.htm" target="_blank">interesting read</a> on JDJ about leveraging Java, BPEL and SOA to deliver &#8220;Composite&#8221; (a term we like) applications in Business time.

<blockquote><a href="http://java.sys-con.com/read/358049.htm">How To Deliver Composite Applications with Java, WS-BPEL &#038; SOA</a>
Java is an outstanding language for building components, services, and many applications that are vendor and platform neutral. The vast adoption of Java technology by the industry in the past decade is a testament to the power of Java. Development of new applications, services, and components using Java is not going away, but many organizations have progressively moved to the next phase in maturing their IT Infrastructure. This phase is driven by many factors including how businesses operate today, having to constantly adjust to market trends, and that IT has moved from being a support organization to being the backbone of business and, hence, needs to keep pace with the organization. Continuous and faster alignment with changing business needs, time-to-market, and cost are the factors that determine success in this phase.</blockquote>

Lots of interesting stuff about NetBeans 5.5, and how you can use it to compose and test WS-BPEL services.  

]]></content:encoded>
			<wfw:commentRss>http://www.springboard-software.com/2007/04/09/java-ws-bpel-and-soa/feed/</wfw:commentRss>
		</item>
		<item>
		<title>IBM OmniFind Yahoo! Edition</title>
		<link>http://www.springboard-software.com/2007/04/04/ibm-omnifind-yahoo-edition/</link>
		<comments>http://www.springboard-software.com/2007/04/04/ibm-omnifind-yahoo-edition/#comments</comments>
		<pubDate>Wed, 04 Apr 2007 20:21:28 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Object Oriented Analysis &#038; Design]]></category>

		<guid isPermaLink="false">http://blog.springboard-software.com/?p=21</guid>
		<description><![CDATA[Yahoo and IBM have released a free enterprise search engine for small and medium businesses that will index a up to 500,000 documents on Windows and Linux intranet servers.  Omnifind is built upon Apache Lucene search technology, providing fast, full-text indexing and search capabilities of various document types, such as MS Word or PDF, [...]]]></description>
			<content:encoded><![CDATA[<p>Yahoo and IBM <a href="http://omnifind.ibm.yahoo.net/" target="_blank">have released a free enterprise search engine</a> for small and medium businesses that will index a up to 500,000 documents on Windows and Linux intranet servers.  Omnifind is built upon Apache Lucene search technology, providing fast, full-text indexing and search capabilities of various document types, such as MS Word or PDF, as well as web pages.
</p>
<p>IBM and Yahoo! state:

<blockquote>IBM OmniFind Yahoo! Edition is a no-charge, entry-level enterprise search software solution that enables rapid deployment of intranet and file system search for both employees and customers.
View Flash Overview
</blockquote>
</p>

<p>The value add for SMBs is that IBM OmniFind Yahoo! Edition is not only free and built upon proven open source technology, but that it&#8217;s backed by IBM and Yahoo, as well as a community of volunteers that can help you tailor your solution to your enterprise search needs.  Look for Springboard to be deeply involved with this technology, as well as sample OmniFind scripts to be posted here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.springboard-software.com/2007/04/04/ibm-omnifind-yahoo-edition/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How To Call Amazon ItemSearch in Flex</title>
		<link>http://www.springboard-software.com/2007/03/28/amazon-itemsearch-in-flex/</link>
		<comments>http://www.springboard-software.com/2007/03/28/amazon-itemsearch-in-flex/#comments</comments>
		<pubDate>Wed, 28 Mar 2007 15:46:03 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Flex Development]]></category>

		<category><![CDATA[How To]]></category>

		<category><![CDATA[Object Oriented Analysis &#038; Design]]></category>

		<category><![CDATA[Amazon Item Search]]></category>

		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://blog.springboard-software.com/?p=19</guid>
		<description><![CDATA[If you didn&#8217;t know it, this is the &#8220;Age of RIA&#8221; - or Rich Internet Applications.  Several technologies have made writing rich and responsive web applications a reality.  Today, we&#8217;re going to look at an example of how to use the Amazon ItemSearch web service using Flex 2, Adobe&#8217;s RIA development language.

The key [...]]]></description>
			<content:encoded><![CDATA[<p>If you didn&#8217;t know it, this is the &#8220;Age of RIA&#8221; - or Rich Internet Applications.  Several technologies have made writing rich and responsive web applications a reality.  Today, we&#8217;re going to look at an example of how to use the <a href="http://docs.amazonwebservices.com/AWSEcommerceService/4-0/ApiReference/ItemSearchOperation.html" target="_blank">Amazon ItemSearch</a> web service using <a href="http://www.adobe.com/products/flex/" target="_blank">Flex 2</a>, Adobe&#8217;s RIA development language.</p>

<p>The key elements that I want to cover are:

<ul>
	<li>Formatting the Service Request</li>
	<li>Parsing the Resulting XML response</li>
</ul>
</p>

<p>Before we go too far, take a look at the finished application in the left navigator of this site.  Scroll down until you see the Amazon Search widget in the left navigator, enter a Keyword like &#8220;Adobe Flex&#8221;, select &#8220;Books&#8221; and press the &#8220;Find It&#8221; button.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.springboard-software.com/2007/03/28/amazon-itemsearch-in-flex/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP Example: The Strategy Pattern</title>
		<link>http://www.springboard-software.com/2006/06/29/php-example-the-strategy-pattern/</link>
		<comments>http://www.springboard-software.com/2006/06/29/php-example-the-strategy-pattern/#comments</comments>
		<pubDate>Thu, 29 Jun 2006 18:12:35 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Design Patterns]]></category>

		<category><![CDATA[Object Oriented Analysis &#038; Design]]></category>

		<category><![CDATA[PHP Development]]></category>

		<guid isPermaLink="false">http://blog.springboard-software.com/?p=13</guid>
		<description><![CDATA[Even though we liked the old color scheme of the phppatterns.com site better than the new one, we  think the content is still aces.  One of our favorite areas is the design section with it&#8217;s excellent examples of various design patterns written in PHP.  

One pattern that we find ourselves using repeatedly [...]]]></description>
			<content:encoded><![CDATA[<p>Even though we liked the old color scheme of the phppatterns.com site better than the new one, we  think the content is still aces.  One of our favorite areas is the design section with it&#8217;s excellent examples of various design patterns written in PHP.  </p>

<p>One pattern that we find ourselves using repeatedly is the Strategy Pattern.  The Strategy Pattern is one of the original Gang of Four design patterns.  The Strategy Pattern is grouped with other <em>behavioral patterns</em> because it&#8217;s best suited to solving problems that affect the behavior of your program. </p>

<p>You can define the Strategy Pattern like so:</p>

<blockquote>Encapsulate various related algorithms in individual subclasses of a common superclass.*</blockquote>

<p>You might wonder why this would offer a solution that affects the behavior of your programs.  Here&#8217;s the beauty of this pattern - if you&#8217;ve ever found yourself writing lots of if-else if-else blocks, or if you&#8217;ve ever written loads of long switch or case statements, then you&#8217;re going to love this thing.  I&#8217;m not saying you won&#8217;t have if-blocks, but you&#8217;ll find that your code will become much cleaner and more flexible if you start applying some Strategy.  Read on for the example.</p>
<span id="more-13"></span>
<p>The Strategy Pattern example on the phppatterns.com site, <a href="http://www.phppatterns.com/docs/design/strategy_pattern" target="foo">described here</a>, uses something pretty common in web application programming - form validation.  That&#8217;s the example we&#8217;ll use - and extend here.</p>

<p>Web applications are often filled with HTML forms, and these forms can often have many fields.  It&#8217;s pretty common to have the web application validate the data submitted in these fields on the server - and this is often in addition to client (or browser) based field validation.  This is usually a response to the ability that users have to disable javascript within their browser, opening up the possibility to completely bypass client-based validation. </p>

<p>So - let&#8217;s consider a simple form for user registration.  In this application, you only have three fields that you&#8217;re concerned with - the user name, the password, and a second password field that you&#8217;ll use to confirm the user typed their password correctly.  Told you the form was simple.  Now, let&#8217;s look at the requirements - which are anything but simple:  </p>

<p>Let&#8217;s assume your customer&#8217;s requirements are to make all three fields required for new users.  For existing users, only username is required - but if a user enters a password, then the confirmation password field will also be required.  In all cases, user names and passwords must be between 6 and 20 characters, can only contain letters and numbers (no punctuation), are case sensitive, and of course, the password and confirmation passwords must match one another.  Finally - user names must not already exist in your user database.  That&#8217;s quite a bit of business logic for such a trivial form, but you&#8217;ll probably find that the business rules that are important to your clients often provide you with the most complex challenges.</p>

<p>So let&#8217;s assume you have already created the form, and the logic to write the data into the database, and you&#8217;re ready to apply these business requirements. </p>

<p>You might be tempted to start off thinking in terms of if-then or case statements.  I won&#8217;t even bother with the sample code <em>because I want you to stop thinking that way</em> - so I won&#8217;t enable your addiction to long, buggy, complex if-else if blocks.</p>

<p>Instead, let&#8217;s look at how we can apply the Strategy Pattern.  Remember, we defined the strategy pattern is about encapsulating related algorithms in subclasses of a common abstract class, and we clearly have several related algorthms.  In the one case, we have an algorithm for validating the username field.  In the other case, we have an algorithm for validating the password (and confirm password) field.  We also have an algorithm for validating whether any arbitrary required field is present or not.  So - by the Strategy pattern - three related algorithms would mean three related subclasses.</p>

<p><a href="http://blog.springboard-software.com/wp-content/uploads/2006/06/FieldValidator.png" title="Field Validator Strategy"><img id="image17" src="http://blog.springboard-software.com/wp-content/uploads/2006/06/FieldValidator.thumbnail.png" alt="Field Validator Strategy" height="77" width="128" border="0" /></a>What do these related classes have in common?   In all cases, they encapsulate logic to validate a form field.  So, we&#8217;ll start with with an abstract <em>FieldValidator</em> class - and create a simple interface: we&#8217;ll just have a validate() method that gets called during object construction.  The validate method will check the business rules for the current subclass, and add any errors it encounters to the errorMsg list.  We&#8217;ll also add an isValid() mathod that checks the error list and returns true if there are no errors.  If validation fails, the validation error(s) for the current field will get recorded in an array of error messages that we can display back to the user with a getError() method.  Once we have defined the superclass, we&#8217;ll then create three subclasses - one for the username, one for the password, and one for any required field.  The UML for the FieldValidator strategy is presented here in the figure to the left.</p>

<p>Now that we&#8217;ve got the various classes defined, lets write the code. While this bit  is almost identical to the PHPPatterns.com example, we&#8217;ll get to the extension shortly.</p> 
<code><pre><ol><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> *  FieldValidator </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * &quot;Abstract&quot; superclass for form field validation</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * Example of the Strategy Pattern presented on </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * phppatterns.net</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">class</span> FieldValidator <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Private</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * $errorMsg stores error messages if not valid</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$errors</span>;</div></li><li> </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">//! A constructor.</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Constucts a new FieldValidator object</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> FieldValidator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">errors</span>=<a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">validate</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li> </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">//! A manipulator</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * @return void</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> validate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #808080; font-style: italic;">// Superclass method does nothing</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li> </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">//! A manipulator</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Adds an error message to the array</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * @return void</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> setError <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$msg</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">errors</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #0000ff;">$msg</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li> </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">//! An accessor</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Returns true if string valid, false if not</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * @return boolean</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> isValid <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">errorMsg</span> != <span style="color: #000000; font-weight: bold;">null</span> &amp;&amp; <a href="http://www.php.net/sizeof"><span style="color: #000066;">sizeof</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">errorMsg</span> <span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Pops the last error message off the array</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * @return string</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> getError <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array_pop"><span style="color: #000066;">array_pop</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">errorMsg</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">?&gt;</span> </div></li></ol></pre></code>
<p> And now, the three subclasses</p>

<code><pre><ol><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> *  RequiredFieldValidator subclass of FieldValidator</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> *  Validates a that a required field is present </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">class</span> RequiredFieldValidator extends FieldValidator <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Private</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * $user the fieldName to validate</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$fieldName</span>;</div></li><li>    </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Private</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * $user the fieldName to validate</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$fieldValue</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Constucts a new Validator object</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * @param $fieldName the Name of the Field</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * @param  $fieldValue The Value of the Field</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> RequiredFieldValidator <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fieldName</span>, <span style="color: #0000ff;">$fieldValue</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">fieldName</span>=<span style="color: #0000ff;">$fieldName</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">fieldValue</span>=<span style="color: #0000ff;">$fieldValue</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        FieldValidator::<span style="color: #006600;">FieldValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Validates the field is present</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * @return void</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> validate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">fieldValue</span>  == <span style="color: #000000; font-weight: bold;">null</span> || <span style="color: #ff0000;">&#8220;&#8221;</span>== <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">fieldValue</span> <span style="color: #66cc66;">&#41;</span> </div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setError</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">fieldName</span> . <span style="color: #ff0000;">&#8216; is a required field.&#8217;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> *  UserNameValidator subclass of FieldValidator</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> *  Validates a username</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">class</span> UserNameValidator extends FieldValidator <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Private</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * $user the username to validate</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$user</span>;</div></li><li>    </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Constucts a new ValidateUser object</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * @param $user the string to validate</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> UserNameValidator <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$user</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">user</span>=<span style="color: #0000ff;">$user</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        FieldValidator::<span style="color: #006600;">FieldValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li>   </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Validates a username</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * @return void</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> validate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #808080; font-style: italic;">// do we have OK characters?</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<a href="http://www.php.net/preg_match"><span style="color: #000066;">preg_match</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;/^[a-zA-Z0-9]+$/&#8217;</span>,<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">user</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;User Login is null or contains invalid characters&#8217;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #808080; font-style: italic;">// do we meet minimum length requirement?</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">user</span><span style="color: #66cc66;">&#41;</span> &lt; <span style="color: #cc66cc;">6</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;User Login mut be at least 6 characters&#8217;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #808080; font-style: italic;">// do we meet the max length requirement?</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">user</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">20</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;User Login is too long&#8217;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #808080; font-style: italic;">// does the current username already exist?</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">       	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">user</span> != <span style="color: #000000; font-weight: bold;">null</span> &amp;&amp; <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">alreadyExists</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">user</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">       	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;The User Login, &quot;&#8217;</span>.  <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">user</span> .<span style="color: #ff0000;">&#8216;&quot; is already in use&#8217;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">       	<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">// alreadyExists - return true if the user name is in the DB    </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> alreadyExists<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$userName</span> <span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <span style="color: #808080; font-style: italic;">// we&#8217;ll leave it to you to determine how implement this method as an exercise.</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> *  ValidatorPassword subclass of FieldValidator</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> *  Validates a password</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">class</span> PasswordValidator extends FieldValidator <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Private</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * $pass the password to validate</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$pass</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Private</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * $conf to confirm the passwords match</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$conf</span>;</div></li><li> </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">//! A constructor.</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Constucts a new ValidatePassword object subclass or FieldValidator</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * @param $pass the string to validate</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * @param $conf to compare with $pass for confirmation</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> PasswordValidator <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pass</span>,<span style="color: #0000ff;">$conf</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">pass</span>=<span style="color: #0000ff;">$pass</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">conf</span>=<span style="color: #0000ff;">$conf</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        FieldValidator::<span style="color: #006600;">FieldValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li> </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">//! A manipulator</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * Validates a password</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    * @return void</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">    */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> validate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">pass</span>!=<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">conf</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;Passwords do not match&#8217;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<a href="http://www.php.net/preg_match"><span style="color: #000066;">preg_match</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;/^[a-zA-Z0-9_]+$/&#8217;</span>,<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">pass</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;Password is null or contains invalid characters&#8217;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">pass</span><span style="color: #66cc66;">&#41;</span> &lt; <span style="color: #cc66cc;">6</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;Password must be at least 6 characters long&#8217;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">pass</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">20</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;Password cannot be more than 20 characters&#8217;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">conf</span><span style="color: #66cc66;">&#41;</span> &lt; <span style="color: #cc66cc;">6</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;Confirmation Password must be at least 6 characters long&#8217;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">conf</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">20</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;Confirmation Password cannot be more than 20 characters&#8217;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">?&gt;</span> </div></li></ol></pre></code>

<p>This is roughly the point at which the example on the phppatterns.com ends.  After introducing the Strategy Pattern, and then providing the field validator examples, you&#8217;re given a little bit of code that shows you how to call you new validator classes.  What would have made this an even better example is the recognition that, in addition to the tasks of <em>field</em> validation, you could just as easily apply the Strategy pattern to the task of <em>form</em> validation.  </p>

<p><a href="http://blog.springboard-software.com/wp-content/uploads/2006/06/FormValidator.png" title="Form Validator Strategy"><img id="image18" src="http://blog.springboard-software.com/wp-content/uploads/2006/06/FormValidator.thumbnail.png" alt="Form Validator Strategy" height="86" width="128" border="0"/></a>Remember our business requirements?  Our requirements in this example say that some of the fields are required for new users, but different fields are required for existing users.  Once again, we have related algorithms, which, in this case, are the individual form level validation requirements.  In our example, the related &#8220;forms&#8221; are the new user form and an existing user form.  Regardless of whether you actually use different HTML forms for these, you can think of their validation requirements distinctly - and can represent them as concrete implementations of a common <em>FormValidator</em> superclass.</p>
<p>Our new FormValidator class allows you to register new FieldValidator instances to it using the addValidator() method, and provides the abiity to call the validate() method on each one through the isValid() call.  Let&#8217;s see how this code plays out:</p>

<code><pre><ol><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/*</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * FormValidator</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * Abstract class that performs field-level validation</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * for a given HTML form.</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> *  </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">class</span> FormValidator</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$errors</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$validatorList</span>;</div></li><li> 	</li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #000000; font-weight: bold;">function</span> FormValidator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">validatorList</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#125;</span> </div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> 	 * validate</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> 	 * </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> 	 * Main abstract method that needs to be implemented in </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> 	 * each subclass</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> 	 */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #000000; font-weight: bold;">function</span> validate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	   <span style="color: #0000ff;">$returnVal</span> = <span style="color: #000000; font-weight: bold;">true</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	   <span style="color: #808080; font-style: italic;">// Perform validation</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	   <span style="color: #808080; font-style: italic;">// against each validator in the list</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	   <span style="color: #808080; font-style: italic;">// every one must return true for this method</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	   <span style="color: #808080; font-style: italic;">// to return true</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	   <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getValidators</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$validator</span><span style="color: #66cc66;">&#41;</span> </div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    	   <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      	  	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$validator</span>-&gt;<span style="color: #006600;">isValid</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> </div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        	    <span style="color: #0000ff;">$returnVal</span> = <span style="color: #000000; font-weight: bold;">false</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        	    <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$error</span>=<span style="color: #0000ff;">$validator</span>-&gt;<span style="color: #006600;">getError</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> </div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            	   <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                       <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">addError</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$error</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">            	   <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        	<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    	   <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    	   <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$returnVal</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#125;</span></div></li><li> </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">function</span> getErrors<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">errors</span>; </div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#125;</span></div></li><li> </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #000000; font-weight: bold;">function</span> addError<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$message</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">errors</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$message</span>;</div></li><li> 	</li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#125;</span></div></li><li> </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #000000; font-weight: bold;">function</span> addValidator<span style="color: #66cc66;">&#40;</span>&amp; <span style="color: #0000ff;">$validatorObject</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">validatorList</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$validatorObject</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #000000; font-weight: bold;">function</span> getValidators<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">validatorList</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">?&gt;</span> </div></li></ol></pre></code>

<p>And now, let&#8217;s examine the FormValidator subclasses - for the New and Existing user forms.</p>

<code><pre><ol><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/*</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * NewUserFormValidator</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * Validates that the business rules for new user form submissions are followed</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> */</span></div></li><li> </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #000000; font-weight: bold;">class</span> NewUserFormValidator extends FormValidator</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #000000; font-weight: bold;">function</span> NewUserFormValidator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 		parent::<span style="color: #006600;">FormValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> PasswordValidator<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_REQUEST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&#8216;passWord&#8217;</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #0000ff;">$_REQUEST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&#8216;confirmPassword&#8217;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> UserNameValidator<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_REQUEST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&#8216;userName&#8217;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	 	<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> RequiredFieldValidator<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;Password&#8221;</span>, <span style="color: #0000ff;">$_REQUEST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&#8216;passWord&#8217;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	 	<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> RequiredFieldValidator<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;User Login&#8221;</span>, <span style="color: #0000ff;">$_REQUEST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&#8216;userName&#8217;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li> 		</li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/**</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * ExistingUserValidator - </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * This is a formvalidator subclass that gets called when </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * a user record is updated</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> */</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #000000; font-weight: bold;">class</span> ExistingUserFormValidator extends FormValidator</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #66cc66;">&#123;</span></div></li><li> </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">function</span> ExistingUserFormValidator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		parent::<span style="color: #006600;">FormValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li>		</li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> RequiredFieldValidator<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;User Login&#8221;</span>,<span style="color: #0000ff;">$_REQUEST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&#8216;userName&#8217;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">// only validate password and the confirmation password if either one is present</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$_REQUEST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&#8216;passWord&#8217;</span><span style="color: #66cc66;">&#93;</span> != <span style="color: #ff0000;">&#8220;&#8221;</span> || <span style="color: #0000ff;">$_REQUEST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&#8216;confirmPassword&#8217;</span><span style="color: #66cc66;">&#93;</span> != <span style="color: #ff0000;">&#8220;&#8221;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	 	<span style="color: #66cc66;">&#123;</span>	</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	 	 	<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> RequiredFieldValidator<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;Password&#8221;</span>, <span style="color: #0000ff;">$_REQUEST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&#8216;passWord&#8217;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 		 	<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> RequiredFieldValidator<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;Confirmation Password&#8221;</span>, <span style="color: #0000ff;">$_REQUEST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&#8216;confirmPassword&#8217;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 		 	<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">addValidator</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PasswordValidator<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_REQUEST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&#8216;passWord&#8217;</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #0000ff;">$_REQUEST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&#8216;confirmPassword&#8217;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	 	<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> 	<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">?&gt;</span> </div></li></ol></pre></code>

<p>Now that the various building blocks are in place, let&#8217;s see how we might tie this together in our page controller script.  You&#8217;re probably using some kind of MVC pattern for your system, right?  At the controller level - all we&#8217;re really concerned with is if we&#8217;re dealing with a new or existing user.  For new users, we&#8217;ll utilize the NewUserFormValidator to do the validation - and if it&#8217;s an existing user, we use the ExistingUserFormValidator.  Let&#8217;s say we have already implemented a method to decide which validator to use.  Maybe the User BusinessObject could tell us based on the value of some inbound action parameter.  Sure.  That might look something like this:</p>
<code><pre><ol><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">. . .</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #000000; font-weight: bold;">function</span> saveUser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">               <span style="color: #808080; font-style: italic;">// assume we&#8217;ve already populated a user object from the html post</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">               <span style="color: #808080; font-style: italic;">// see the blog entry about using introspection for an example of how to do this easily</span></div></li><li> </li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">               <span style="color: #0000ff;">$theUser</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">user</span>; </div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">// assume form validators are related to business objects</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">// get the appropriate validator for inserts and updates</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">// often these are the same, but not always - it depends on the </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">// business object </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                <span style="color: #808080; font-style: italic;">// here we&#8217;ll use &quot;action&quot; parameter - which may have been the </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                <span style="color: #808080; font-style: italic;">// value of the user form submit button or a hidden field to help figure out </span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                <span style="color: #808080; font-style: italic;">// what form validator to use</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #0000ff;">$validator</span> = <span style="color: #000000; font-weight: bold;">null</span>;</div></li><li>		</li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">action</span> == <span style="color: #ff0000;">&#8220;NEW&#8221;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #0000ff;">$validator</span> = <span style="color: #000000; font-weight: bold;">new</span> NewUserFormValidator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">else</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #0000ff;">$validator</span> = <span style="color: #000000; font-weight: bold;">new</span> ExistingUserFormValidator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                <span style="color: #808080; font-style: italic;">// in a real system, make sure $validator isn&#8217;t null here</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$validator</span>-&gt;<span style="color: #006600;">validate</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #0000ff;">$user</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;Location: &#8220;</span> . <span style="color: #0000ff;">$successPage</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">return</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">else</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                    <span style="color: #808080; font-style: italic;">// since there are errors, let&#8217;s get the error list from the validator</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                    <span style="color: #808080; font-style: italic;">// and pass that to back to our web form so we can display them</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		    <span style="color: #0000ff;">$errors</span> = <span style="color: #0000ff;">$validator</span>-&gt;<span style="color: #006600;">errors</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                    <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">$editorPage</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">. . . </div></li></ol></pre></code>
<p>So that&#8217;s it.  Looks like a lot of work for such a simple form, huh?  I agree - except, that in real world applications, you rarely have just one form, you might have dozens or more - each with dozens of individual form fields.  The beauty of this sort of application of the Strategy pattern is that for each new type of field validation task, you simply create a new FieldValidator subclass - and then register the new FieldValidator on the appropriate FormValidator where it&#8217;s needed.  For example, what if we add email address to our user form?  You could easily create an EmailFieldValidator - and then simply add a line to call the addValidator() method in the appriate FormValidator class to use it.  What if the field validation rules change as a result of supporting different languages or countries?  For example, say you have a need to validate postal codes in the US and Canada. You could simply create US and Canada PostalCodeFieldValidator classes that encapsulate the rules for valid postal formats in those countries. In addition, it would now be pretty easy to add any number of new forms that contain complex validation logic.  Need to start writing Contacts or Leads or Invoice details in your web app? Simply create a new FormValidator for each one, and then you business logic will be neatly encapsulated and ready for use as well as extension.   </p>
<p><em>* Note: I talk quite a bit here about abstract superclasses when I discuss the Strategy Pattern.  There&#8217;s absolutely nothing wrong with using an interface instead.  So, if you&#8217;re a PHP 5 or Java  programmer, and your problem is better suited toward an interface, then by all means, use an interface.  Since I&#8217;m writing this example in PHP 4 - there really isn&#8217;t even any &#8220;abstract&#8221; nature to these superclasses.  Don&#8217;t let that put you off - the main point here is to code to a public interface provided by your superclass so you can enjoy the benefits in each concrete implementation.</em></p> ]]></content:encoded>
			<wfw:commentRss>http://www.springboard-software.com/2006/06/29/php-example-the-strategy-pattern/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using Introspection in PHP</title>
		<link>http://www.springboard-software.com/2006/06/27/using-introspection-in-php/</link>
		<comments>http://www.springboard-software.com/2006/06/27/using-introspection-in-php/#comments</comments>
		<pubDate>Tue, 27 Jun 2006 17:00:39 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Object Oriented Analysis &#038; Design]]></category>

		<category><![CDATA[PHP Development]]></category>

		<guid isPermaLink="false">http://blog.springboard-software.com/?p=12</guid>
		<description><![CDATA[One of the things we love about Java is its ability to use introspection.  Don&#8217;t know what methods or field an object has?  Just ask it.

It turns out that PHP provides some nifty introspection features as well.  We&#8217;ve found one to be particularly useful: the ability of a PHP object to tell [...]]]></description>
			<content:encoded><![CDATA[One of the things we love about Java is its ability to use introspection.  Don&#8217;t know what methods or field an object has?  Just ask it.

<p>It turns out that PHP provides some nifty introspection features as well.  We&#8217;ve found one to be particularly useful: the ability of a PHP object to tell us its member variables at runtime.  Not sure what that means?  Let&#8217;s look at an example.</p>

<p>Suppose you&#8217;re building a web-enabled content manager (who isn&#8217;t?).  You want to provide a set of web forms that let your client enter and manage information that gets stored in a database.  Further, let&#8217;s assume you want to manage several different <em>types</em> of objects that are relavent to your client&#8217;s busienss in the database - say, for example, Leads, Events, Stories, Links, Product Reviews, and Blog Entries (maybe you&#8217;ve got a demanding client).</p>

<p>In this example, we&#8217;re not going to worry about the web forms, nor are we going to worry about the database.  I assume you know how to build web pages, and that you know how to read and write data with a database.  Instead, we&#8217;re going to focus on the business objects that ferry data from the web form to the database, and from the database to the web form.  One of the key tasks you&#8217;re going to find yourself performing again and again is populating these business objects with data, either from the web form, or from the results of a database lookup.</p>

<p>So, how can introspection help?  First, let&#8217;s build our base class for our business objects.  Let&#8217;s call this BusinessObject, and let&#8217;s give it the ability to populate itself from an arbitrary array.</p>
<span id="more-12"></span>
<code><pre><ol><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">class</span> BusinessObject</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> populateFromArray<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$someArray</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$fieldList</span> = <a href="http://www.php.net/get_class_vars"><span style="color: #000066;">get_class_vars</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fieldList</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$key</span> =&gt; <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span>		</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	   <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #0000ff;">$key</span> = <span style="color: #0000ff;">$someArray</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#93;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div></li></ol></pre></code>

<p>There it is.  The call to <strong>get_class_vars($this)</strong> returns a list of all the member variables of the current object.  We then loop through the list and assign the value of each element (<strong>$someArray[$key]</strong>) to the matching member variable (<strong>$this->$key</strong>).  So, at this point, any subclass of BusinessObject will inherit the ability to populate itself from a PHP associative array. But what good does that do?</p> 

<p>The answer lies in the fact that many things of interest to PHP developers arrive as arrays.  Let&#8217;s add a couple more methods to our base class to illustrate:</p> 

<code><pre><ol><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">class</span> BusinessObject</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> populateFromArray<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$someArray</span><span style="color: #66cc66;">&#41;</span> </div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$fieldList</span> = <a href="http://www.php.net/get_class_vars"><span style="color: #000066;">get_class_vars</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fieldList</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$key</span> =&gt; <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span>		</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	   <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #0000ff;">$key</span> = <span style="color: #0000ff;">$someArray</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#93;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> populateFromRequest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">populateFromArray</span><span style="color: #66cc66;">&#40;</span>  <span style="color: #0000ff;">$_REQUEST</span> <span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> populateByPrimaryKey<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0000ff;">$tableName</span> = <a href="http://www.php.net/get_class"><span style="color: #000066;">get_class</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0000ff;">$pkField</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getPrimaryKeyFieldName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li>		</li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">// Assume the PKs are numeric</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">&#8220;select * from $tableName where $pkField = $id&#8221;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0000ff;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0000ff;">$row</span> = <a href="http://www.php.net/mysql_fetch_array"><span style="color: #000066;">mysql_fetch_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">           <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">populateFromArray</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$row</span> <span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div></li></ol></pre></code>
<p>Now we have an BusinessObject that provides all its subclasses with the ability to populate themselves from an HTTP request, as well as from a database when given the primary key.  Some things to note here: for simplicity, we assume that we&#8217;ve essentially followed Fowler&#8217;s <em>Active Record</em> pattern. In a real system, you may want to put the database access calls into a finder object (as per Fowler&#8217;s Row Data Gateway pattern).  This wouldn&#8217;t change the code much - your finder would call the BusinessObject.populateFromArray() method in your subclass with the row instead of having the subclass do it itself. </p>

<p>Also - note that the introspective call: <strong>$tableName = get_class($this)</strong> in the populateByPrimaryKey method implicitly assumes that the name of your database table is exactly the same as the name of your BusinessObject subclass - so records for the Article BusinessObject are stored in an Article table, etc.</p>

<p>Finally, we&#8217;ve also assumed that member variables in your BusinessObject classes have the same names as the database fields - as well as the html form elements that are going to be used to populate them.  This may not be realistic for your system, but it does add a nice level of predictability to your code as you won&#8217;t have to guess what the field mapping from form to object to database will look like.  </p>

<p>What are the caveats?  First - you may notice that little call to getPrimaryKeyFieldName().  You have to specify a name for your primary key field.  OK - that&#8217;s not so bad.  What might be bad is if, while looping through $someArray in the populateByArray() method, you don&#8217;t want to overwrite some fields - even if they&#8217;re not in the array.  In this implementation, you could erase your own primary key in your BusinessObject if it&#8217;s not present in the list of fields in a web request.</p>

<p>To solve these problems, you can make the following adjustments to BusinessObject:
<code><pre><ol><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">class</span> BusinessObject</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> populateFromArray<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$someArray</span><span style="color: #66cc66;">&#41;</span> </div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$fieldList</span> = getFieldList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fieldList</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$key</span> =&gt; <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span>		</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	   <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #0000ff;">$key</span> = <span style="color: #0000ff;">$someArray</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#93;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> getPrimaryKeyFieldName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">primaryKeyFieldName</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">function</span> getFieldList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#123;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$allFields</span> = <a href="http://www.php.net/get_class_vars"><span style="color: #000066;">get_class_vars</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #0000ff;">$excludeFields</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">excludeFields</span>;	</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array_merge"><span style="color: #000066;">array_merge</span></a><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array_diff"><span style="color: #000066;">array_diff</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$allFields</span>, <span style="color: #0000ff;">$excludeFields</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #66cc66;">&#125;</span></div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&#8230;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div></li></ol></pre></code>
<p>Now, what we&#8217;ve done is placed a requirement for you to define 2 things in your BusinessObject subclasses: a field containing the name of the primary key, and an array of fields to exclude from automatic population.  This lets you get around the problems associated with erasing metadata or other private data that you might want to keep track of - or may want to populate or calculate in some other manner.</p>]]></content:encoded>
			<wfw:commentRss>http://www.springboard-software.com/2006/06/27/using-introspection-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Defining Vision</title>
		<link>http://www.springboard-software.com/2006/06/22/defining-vision/</link>
		<comments>http://www.springboard-software.com/2006/06/22/defining-vision/#comments</comments>
		<pubDate>Thu, 22 Jun 2006 16:03:37 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Object Oriented Analysis &#038; Design]]></category>

		<guid isPermaLink="false">http://blog.springboard-software.com/?p=11</guid>
		<description><![CDATA[This is sort of a follow up to the comments on the Springboard Twist post.  I said that before we take the next steps in applying Deductive Business Logic (that is, in mapping Business Value to the Specific things you should implement in a software project) you ought to make sure you&#8217;ve take the [...]]]></description>
			<content:encoded><![CDATA[<p>This is sort of a follow up to the comments on the Springboard Twist post.  I said that before we take the next steps in applying Deductive Business Logic (that is, in mapping Business Value to the Specific things you should implement in a software project) you ought to make sure you&#8217;ve take the first step.  At Springboard, we believe the first step is to help our clients articulate their vision of what constitutes the Business Value for a given project.</p>

<p>Of course, that begs the question: What are the tangible components to a statement of the vision of a software project?</p>
 
<p>I think it&#8217;s just three things:</p>

<ol>
<li><strong>Identify who the stakeholders are:</strong>  Find out who&#8217;s paying for the project, and who has final authority for approving the work produced during the project&#8217;s life.  The stakeholders are not only paying the bills, they are the people who have the most to gain or lose from the success or falure of the project.
</li>
<li><strong>Identify what the stakeholders percieve as the business needs for the project.</strong>  This might be one or more busines problems that they face, or opportunities that they want to take advantage of.  You should be ready to help them think about how to articulate their business needs, and there are a number of techniques you can use, including interviews and surveys, brainstorming, workshops, and even role playing.  
</li>
<li><strong>Identify the features of the proposed system that would meet these needs.</strong>  The notion of a feature at this point is not the same as a software requirement.  Think of features as services that the system will provide - and in this context - these are services that the business stakeholders and the development team both think will meet the business needs we identified earlier.</li>
</ol>  

Once you have identified these three things, you will defined most (if not all) of the &#8220;Business Value&#8221; box in our <a href="http://blog.springboard-software.com/?p=8">Deductive Business Logic</a> model. The next steps?  I suppose that&#8217;s going to have to wait for another post.  ]]></content:encoded>
			<wfw:commentRss>http://www.springboard-software.com/2006/06/22/defining-vision/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

