<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Made of Everything You&#039;re Not &#187; HTML_QuickForm</title>
	<atom:link href="http://blog.ericlamb.net/tag/html_quickform/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ericlamb.net</link>
	<description>Thoughts on programming, people and life</description>
	<lastBuildDate>Thu, 29 Jul 2010 07:00:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Make Zend_Form Bend To Your Will</title>
		<link>http://blog.ericlamb.net/2009/09/make-zend_form-bend-to-your-will/</link>
		<comments>http://blog.ericlamb.net/2009/09/make-zend_form-bend-to-your-will/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 13:01:32 +0000</pubDate>
		<dc:creator>Eric Lamb</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[HTML_QuickForm]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Zend_Form]]></category>

		<guid isPermaLink="false">http://blog.ericlamb.net/?p=2516</guid>
		<description><![CDATA[The first thing you need when working with any new web language or framework is figure out how to work with forms. Forms are one of the constants when working online and it&#8217;s the rare occasion when you can bypass dealing with them. They are also one of the more rote and boring portions of [...]]]></description>
			<content:encoded><![CDATA[<p>The first thing you need when working with any new web language or framework is figure out how to work with forms. Forms are one of the constants when working online and it&#8217;s the rare occasion when you can bypass dealing with them. They are also one of the more rote and <em>boring</em> portions of a web app to build; they&#8217;re, pretty much, always the same requiring the same fields on the same pages with the same validation rules and the same database schema.</p>
<div id="attachment_2526" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.ericlamb.net/wp-content/uploads/2009/09/mind_control.gif" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2Fwp-content%2Fuploads%2F2009%2F09%2Fmind_control.gif','Make+Zend_Form+Bend+To+Your+Will')"><img class="size-medium wp-image-2526" title="Make Zend_Form Bend To Your Will" src="http://blog.ericlamb.net/wp-content/uploads/2009/09/mind_control-300x294.gif" alt="Make Zend_Form Bend To Your Will" width="300" height="294" /></a><p class="wp-caption-text">Make Zend_Form Bend To Your Will</p></div>
<p>If you&#8217;re smart dealing with forms effectively becomes a priority. My previously held favorite method was using HTML_QuickForm for handling forms. If you&#8217;ve never used; IT IS AWESOME.</p>
<p>Here&#8217;s an example login form script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Include HTML_QuickForm library
 */</span>
<span style="color: #22f;">require</span> <span style="color: #0000ff;">'HTML/QuickForm.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Include HTML_QuickForm Smarty Renderer
 */</span>
<span style="color: #22f;">require</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'HTML/QuickForm/Renderer/ArraySmarty.php'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$renderer</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000000; font-weight: bold;">new</span> HTML_QuickForm_Renderer_ArraySmarty<span style="color: #000;">&#40;</span><span style="color: #000088;">$smarty</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HTML_QuickForm<span style="color: #000;">&#40;</span><span style="color: #0000ff;">'login'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Email'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Email:'</span> <span style="color: #339933;">,</span><span style="color: #0000ff;">'class=&quot;textfield&quot; size=&quot;30&quot; maxlength=&quot;128&quot;'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addRule</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'Email'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Please Enter Your Email'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'client'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addRule</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'Email'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Please enter a valid email address'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'client'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Password'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Password:'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'class=&quot;textfield&quot; size=&quot;30&quot; maxlength=&quot;128&quot;'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addRule</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'Password'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Please Enter Your Password'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'client'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'checkbox'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'remember'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Remember Me?'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'submit'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'save'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Submit'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'class=&quot;csubmit&quot;'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validate</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//do the processing stuff here...</span>
&nbsp;
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">accept</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$renderer</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'LoginForm'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$renderer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toArray</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Very straightforward I think; instantiate the object, declare the elements, attach the rules and send to smarty (view) object.</p>
<p>The corresponding Smarty template is just as straightforward:</p>

<div class="wp_syntax"><div class="code"><pre class="smarty" style="font-family:monospace;"><span style="color: #000;">&#123;</span><span style="color: #00aaff;">$LoginForm</span>.<span style="color: #006600;">javascript</span><span style="color: #000;">&#125;</span>
<span style="color: #009000;">&lt;form {$LoginForm.attributes}&gt;</span>
<span style="color: #000;">&#123;</span><span style="color: #00aaff;">$LoginForm</span>.<span style="color: #006600;">hidden</span><span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #000;">&#123;</span><span style="color: #22f;">if</span> <span style="color: #00aaff;">$LoginForm</span>.<span style="color: #006600;">Email</span>.<span style="color: #006600;">error</span><span style="color: #000;">&#125;</span><span style="color: #009000;">&lt;span class=&quot;errorMessage&quot;&gt;</span><span style="color: #000;">&#123;</span><span style="color: #00aaff;">$LoginForm</span>.<span style="color: #006600;">Email</span>.<span style="color: #006600;">error</span><span style="color: #000;">&#125;</span><span style="color: #009000;">&lt;/span&gt;&lt;br /&gt;</span><span style="color: #000;">&#123;</span><span style="color: #D36900;">/</span><span style="color: #22f;">if</span><span style="color: #000;">&#125;</span>
<span style="color: #009000;">&lt;div id=&quot;email&quot;&gt;</span>
	Email: <span style="color: #000;">&#123;</span><span style="color: #00aaff;">$LoginForm</span>.<span style="color: #006600;">Email</span>.<span style="color: #006600;">html</span><span style="color: #000;">&#125;</span>
<span style="color: #009000;">&lt;/div&gt;</span>
&nbsp;
<span style="color: #009000;">&lt;div id=&quot;password&quot;&gt;</span>
	Password: <span style="color: #000;">&#123;</span><span style="color: #00aaff;">$LoginForm</span>.<span style="color: #006600;">Password</span>.<span style="color: #006600;">html</span><span style="color: #000;">&#125;</span>
<span style="color: #009000;">&lt;/div&gt;</span>
&nbsp;
<span style="color: #009000;">&lt;div id=&quot;remember&quot;&gt;</span>
	<span style="color: #000;">&#123;</span><span style="color: #00aaff;">$LoginForm</span>.<span style="color: #006600;">remember</span>.<span style="color: #006600;">html</span><span style="color: #000;">&#125;</span>
<span style="color: #009000;">&lt;/div&gt;</span>
&nbsp;
&nbsp;
<span style="color: #009000;">&lt;div id=&quot;submit&quot;&gt;</span>
	<span style="color: #000;">&#123;</span><span style="color: #00aaff;">$LoginForm</span>.<span style="color: #006600;">submit</span>.<span style="color: #006600;">html</span><span style="color: #000;">&#125;</span>
<span style="color: #009000;">&lt;/div&gt;</span>
<span style="color: #009000;">&lt;/form&gt;</span></pre></div></div>

<p>That&#8217;s my preferred method when layout of the form is important and/or outside the box; it&#8217;s totally possible to use HTML_QuickForm without the Smarty integration but I like the control. It allows me to build forms as complicated in layout as the I Am Legend community was (my baseline for form complexity):</p>
<div id="attachment_2523" class="wp-caption aligncenter" style="width: 244px"><a href="http://blog.ericlamb.net/wp-content/uploads/2008/12/I-Am-Legend_1253650807253.png" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2Fwp-content%2Fuploads%2F2008%2F12%2FI-Am-Legend_1253650807253.png','Register+for+I+Am+Legend+Community')"><img src="http://blog.ericlamb.net/wp-content/uploads/2008/12/I-Am-Legend_1253650807253-234x300.png" alt="Register for I Am Legend Community" title="Register for I Am Legend Community" width="234" height="300" class="size-medium wp-image-2523" /></a><p class="wp-caption-text">Register for I Am Legend Community</p></div>
<p>Since I&#8217;m now using the Zend Framework, and it&#8217;s form component is Zend_Form, I can honestly say after the flexibility of HTML_QuickForm and Smarty Zend_Form is, at first, not so awesome. A lot of the resources and tutorials/articles for Zend_Form tout it&#8217;s  use of the decorator design pattern and all the examples I found looked the same. Not. Good.</p>
<p>Almost none of the references I looked at mentioned anything about removing decorators and rendering just the raw form elements like I needed. It is possible though; you just have to attache a couple extra methods to remove the decorators as laid out below. Make note of the calls to &#8220;removeDecorator&#8221; and &#8220;setAttrib&#8221;. Those are the key pieces. </p>
<p>As a counter point here&#8217;s the above script using Zend_Form:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//APPLICATION_PATH/forms/Login.php</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Form_Login <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #000;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #000;">&#40;</span><span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #000;">&#41;</span>
	<span style="color: #000;">&#123;</span>
		parent<span style="color: #339933;">::</span>__construct<span style="color: #000;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Text<span style="color: #000;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLabel</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'Email'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRequired</span><span style="color: #000;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addFilter</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'StripTags'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addFilter</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'StringTrim'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'NotEmpty'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #000;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_Db_RecordExists<span style="color: #000;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'email'</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeDecorator</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'label'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeDecorator</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'htmlTag'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeDecorator</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttrib</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'class'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'transperentInput'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Password<span style="color: #000;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$password</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLabel</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'Password'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRequired</span><span style="color: #000;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addFilter</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'StripTags'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addFilter</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'StringTrim'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'NotEmpty'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeDecorator</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'label'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeDecorator</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'htmlTag'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeDecorator</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #000;">&#41;</span>
				<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttrib</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'class'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'transperentInput'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$submit</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_Submit<span style="color: #000;">&#40;</span><span style="color: #0000ff;">'submit'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>		
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElements</span><span style="color: #000;">&#40;</span><span style="color: #990000;">array</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #000088;">$submit</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000;">&#125;</span>
<span style="color: #000;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Since Zend_Form is a part of the Zend Framework MVC the above is just the form class and the below is the actual processing portion:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//APPLICATION_PATH/controllers/Login.php</span>
<span style="color: #000000; font-weight: bold;">class</span> LoginController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #000;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span>
	<span style="color: #000;">&#123;</span>
		<span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form_Login<span style="color: #339933;">;</span>
		<span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
&nbsp;
			<span style="color: #000088;">$formData</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$formData</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
                            <span style="color: #666666; font-style: italic;">//do the processing stuff here...</span>
&nbsp;
			<span style="color: #000;">&#125;</span>
&nbsp;
		<span style="color: #000;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
<span style="color: #000;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Sigh&#8230;<br />
And then in the view script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;form action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #22f;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAction</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; 
method=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #22f;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMethod</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; 
id=&quot;sumbitForm&quot;&gt;
&lt;div id=&quot;email&quot;&gt;
	Email: <span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;
&nbsp;
&lt;div id=&quot;password&quot;&gt;
	Password: <span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;
&nbsp;
&lt;div id=&quot;remember&quot;&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remember</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;
&nbsp;
&nbsp;
&lt;div id=&quot;submit&quot;&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">submit</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;
&lt;/form&gt;</pre></div></div>

<p>There you go: Zend_Form is your bitch.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ericlamb.net/2009/09/make-zend_form-bend-to-your-will/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>HTML_QuickForm Validation Functions</title>
		<link>http://blog.ericlamb.net/2009/04/html_quickform-validation-functions/</link>
		<comments>http://blog.ericlamb.net/2009/04/html_quickform-validation-functions/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 18:04:14 +0000</pubDate>
		<dc:creator>Eric Lamb</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[HTML_QuickForm]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://blog.ericlamb.net/?p=830</guid>
		<description><![CDATA[I&#8217;ve been using HTML_QuickForm for, what feels like, forever. I&#8217;m happy to say that I haven&#8217;t, manually, written a form in years. God, do they suck to write&#8230;. I may actually be in love with HTML_QuickForm. Sigh&#8230; Not everyone like HTML_QuickForm but it meets my needs pretty well. I don&#8217;t work with any php framework, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using HTML_QuickForm for, what feels like, forever. I&#8217;m happy to say that I haven&#8217;t, manually, written a form in <em>years</em>. <em>God</em>, do they suck to write&#8230;.</p>
<p>I may actually be in love with HTML_QuickForm. </p>
<div id="attachment_1331" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.ericlamb.net/wp-content/uploads/2009/04/quickform.jpg" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2Fwp-content%2Fuploads%2F2009%2F04%2Fquickform.jpg','HTML+QuickForm')"><img src="http://blog.ericlamb.net/wp-content/uploads/2009/04/quickform-300x199.jpg" alt="HTML QuickForm" title="HTML QuickForm" width="300" height="199" class="size-medium wp-image-1331" /></a><p class="wp-caption-text">HTML QuickForm</p></div>
<p>Sigh&#8230;</p>
<p>Not everyone like HTML_QuickForm but it meets my needs pretty well. I don&#8217;t work with any php framework, I use Smarty extensively (QuickForm fits like a glove with Smarty) and have yet to get hung up on the crossover between business and design logic with it. </p>
<p>It is lacking some core validation functionality though so in the spirit of open source here are just a few of the validation functions I&#8217;ve written; free and clear. </p>
<p>NOTE: I&#8217;m assuming you already know about HTML_QuickForm already. If you aren&#8217;t already familiar, the below functions just won&#8217;t mean that much to you.</p>
<p>In the below are QuickForm ready validation functions that do the following:<br />
1. Validate US PhoneNumber<br />
2. Look up a URL<br />
3. Check IP<br />
4. Ensure a date isn&#8217;t set to the past<br />
5. Ensure MySQL date format<br />
6. Ensure positive number<br />
7. Check decimal on number<br />
8. Ensure a URL is valid<br />
9. Make sure a file was uploaded (good for multi &#8220;file&#8221; field forms)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Validation Functions
 *
 * Contains all the system validation.
 *
 * @author Eric Lamb &lt;eric@ericlamb.net&gt;
 * @version 1.0
 * @copyright	@author
 * @filesource
 * @link http://blog.ericlamb.net
 * @copyright 2005-2009 Eric Lamb
 */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Ensures phone numbers are in the proper format
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	value of form field to check
 * @return  bool
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> CheckUSPhoneNum<span style="color: #000;">&#40;</span><span style="color: #000088;">$element_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span>
<span style="color: #000;">&#123;</span> 
	<span style="color: #000088;">$PhoneNumber</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ereg_replace</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">&quot;[^0-9]&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Strip out non-numerics</span>
	<span style="color: #22f;">if</span><span style="color: #000;">&#40;</span><span style="color: #990000;">ereg</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">&quot;^([2-9][0-9]{2})([2-9][0-9]{2})([0-9]{4})$&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$PhoneNumber</span><span style="color: #339933;">,</span> <span style="color: #000088;">$NumberParts</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
		<span style="color: #22f;">return</span> <span style="color: #0000ff;">&quot;(&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$NumberParts</span><span style="color: #000;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #000;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;) &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$NumberParts</span><span style="color: #000;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #000;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;-&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$NumberParts</span><span style="color: #000;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #000;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span> <span style="color: #22f;">else</span> <span style="color: #000;">&#123;</span>
		<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Looks up a domain and makes sure it's valid; only works on *nix
 *
 * @param   string  $type	Type of banning to check
 * @param   string  $data	The cooresponding data to check
 * @return  bool
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> FormDomainLookup<span style="color: #000;">&#40;</span><span style="color: #000088;">$element</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arg</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span> 
    <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'http://'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exec</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">&quot;host -t ns <span style="color: #006699; font-weight: bold;">$value</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$hasil</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span><span style="color: #990000;">ereg</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">&quot;host <span style="color: #006699; font-weight: bold;">$value</span> not found.&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">strtolower</span><span style="color: #000;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$hasil</span><span style="color: #000;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #000;">&#93;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span> 
        <span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
    <span style="color: #000;">&#125;</span> <span style="color: #22f;">else</span> <span style="color: #000;">&#123;</span> 
        <span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> 
    <span style="color: #000;">&#125;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Makes sure an IP address is valid
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	IP Address to check
 * @return  bool
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> CheckIP <span style="color: #000;">&#40;</span><span style="color: #000088;">$element_name</span><span style="color: #339933;">,</span><span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$ip</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$element_value</span><span style="color: #339933;">;</span>
	<span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$longip</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ip2long</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #000;">&#41;</span>
	<span style="color: #000;">&#123;</span>
		<span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span><span style="color: #000088;">$ip</span> <span style="color: #339933;">==</span> <span style="color: #990000;">long2ip</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$longip</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span>
		<span style="color: #000;">&#123;</span>
			<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #000;">&#125;</span> <span style="color: #22f;">else</span> <span style="color: #000;">&#123;</span>
			<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #000;">&#125;</span>
	<span style="color: #000;">&#125;</span> <span style="color: #22f;">else</span> <span style="color: #000;">&#123;</span>
		<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span> 
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Makes sure a &quot;date&quot; isn't set to a past date.
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	Date to Check
 * @return  bool
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> StopPastDate<span style="color: #000;">&#40;</span><span style="color: #000088;">$element_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$vars</span><span style="color: #339933;">,</span> <span style="color: #000088;">$now</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//check that it's a valid date first</span>
	<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #22f;">if</span><span style="color: #000;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">checkdate</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$temp</span><span style="color: #000;">&#91;</span><span style="color: #0000ff;">'1'</span><span style="color: #000;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$temp</span><span style="color: #000;">&#91;</span><span style="color: #0000ff;">'2'</span><span style="color: #000;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$temp</span><span style="color: #000;">&#91;</span><span style="color: #0000ff;">'0'</span><span style="color: #000;">&#93;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
		<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//now make sure the date isn't in the past.</span>
	<span style="color: #22f;">if</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$element_value</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$now</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
		<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span> <span style="color: #22f;">else</span> <span style="color: #000;">&#123;</span>
		<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Makes sure a &quot;date&quot; is a valid MYSQL format.
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	Date to Check
 * @return  bool
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> ValidateMysqlDate<span style="color: #000;">&#40;</span><span style="color: #000088;">$element_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$vars</span><span style="color: #339933;">,</span> <span style="color: #000088;">$now</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//check that it's a valid date first</span>
	<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #22f;">if</span><span style="color: #000;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">checkdate</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$temp</span><span style="color: #000;">&#91;</span><span style="color: #0000ff;">'1'</span><span style="color: #000;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$temp</span><span style="color: #000;">&#91;</span><span style="color: #0000ff;">'2'</span><span style="color: #000;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$temp</span><span style="color: #000;">&#91;</span><span style="color: #0000ff;">'0'</span><span style="color: #000;">&#93;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
		<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
	<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Makes sure an integer is positive
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	Int to Check
 * @return  bool
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> IsIntPositive<span style="color: #000;">&#40;</span><span style="color: #000088;">$element_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
&nbsp;
	<span style="color: #22f;">if</span><span style="color: #000;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
		<span style="color: #000088;">$element_value</span> <span style="color: #339933;">=</span> <span style="color: #000;">&#40;</span>int<span style="color: #000;">&#41;</span><span style="color: #000088;">$element_value</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
	<span style="color: #22f;">if</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$element_value</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
		<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
	<span style="color: #666666; font-style: italic;">//exit;</span>
&nbsp;
	<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Makes sure an integer isn't a decimal
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	Int to Check
 * @return  bool
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> IsIntDecimal<span style="color: #000;">&#40;</span><span style="color: #000088;">$element_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
&nbsp;
	<span style="color: #22f;">if</span><span style="color: #000;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
		<span style="color: #000088;">$element_value</span> <span style="color: #339933;">=</span> <span style="color: #000;">&#40;</span>int<span style="color: #000;">&#41;</span><span style="color: #000088;">$element_value</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
&nbsp;
	<span style="color: #22f;">if</span><span style="color: #000;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$element_value</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</span><span style="color: #000;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
		<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
	<span style="color: #666666; font-style: italic;">//exit;</span>
&nbsp;
	<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Makes sure a URL is valid
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	URL to Check
 * @return  bool
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> IsURLValid<span style="color: #000;">&#40;</span><span style="color: #000088;">$element_name</span><span style="color: #339933;">,</span><span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
	<span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">&quot;/^(http(s?):\/\/|ftp:\/\/{1})((\w+\.){1,})\w{2,}$/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
		<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span> <span style="color: #22f;">else</span> <span style="color: #000;">&#123;</span>
		<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
	<span style="color: #000;">&#125;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Makes sure at least one file was uploaded
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	
 * @return  bool
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> EnsureUploadedFile<span style="color: #000;">&#40;</span><span style="color: #000088;">$element_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element_value</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
	<span style="color: #22f;">foreach</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$_FILES</span> <span style="color: #22f;">AS</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$data</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
		<span style="color: #22f;">if</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #000;">&#91;</span><span style="color: #0000ff;">'error'</span><span style="color: #000;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'0'</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#123;</span>
			<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
		<span style="color: #000;">&#125;</span>
	<span style="color: #000;">&#125;</span>
	<span style="color: #22f;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.ericlamb.net/2009/04/html_quickform-validation-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
