<?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>jeremykendall.net &#187; how to</title>
	<atom:link href="http://www.jeremykendall.net/tag/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jeremykendall.net</link>
	<description>{web developer, entrepreneur }</description>
	<lastBuildDate>Fri, 23 Jul 2010 19:18:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Conditional Form Validation with Zend_Form</title>
		<link>http://www.jeremykendall.net/2008/12/24/conditional-form-validation-with-zend_form/</link>
		<comments>http://www.jeremykendall.net/2008/12/24/conditional-form-validation-with-zend_form/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 15:41:06 +0000</pubDate>
		<dc:creator>Jeremy Kendall</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.jeremykendall.net/?p=128</guid>
		<description><![CDATA[A question from &#8216;ronny stalker&#8217; in the Zend_Form_Element_Multi &#8211; Tips and Tricks comments:
I need to do different validations for field A depending on the value of field B and (possibly depending on a variable that is not in the form at all &#8211; C ).
in this kind of logic:
if (B ==1)
{
validator_B(A);
}
elseif (C)
{
validator_C(A);
}
else
{
validator_Default(A);
}
I understand that validators [...]]]></description>
			<content:encoded><![CDATA[<p>A question from &#8216;ronny stalker&#8217; in the <a href="http://www.jeremykendall.net/2008/12/04/zend_form_element_multi-tips-and-tricks/">Zend_Form_Element_Multi &#8211; Tips and Tricks</a> comments:</p>
<blockquote><p>I need to do different validations for field A depending on the value of field B and (possibly depending on a variable that is not in the form at all &#8211; C ).</p>
<p>in this kind of logic:</p>
<p>if (B ==1)<br />
{<br />
validator_B(A);<br />
}<br />
elseif (C)<br />
{<br />
validator_C(A);<br />
}<br />
else<br />
{<br />
validator_Default(A);<br />
}</p>
<p>I understand that validators get a secondary argument called $context &#8211; which can be used to check values of other fields, but how can a validator get knowledge of other variables in the environment?</p></blockquote>
<p>While this post may not answer ronny&#8217;s question exactly, hopefully it will give him a good starting point to get over the hump.</p>
<p><strong>If other, please explain &#8211; Conditional Validation Using $context</strong></p>
<p>Many forms have a set of radio buttons, or sometimes a select element, where a user can choose from one of several options.  Sometimes &#8220;other&#8221;  will be one of those options, with a corresponding &#8220;If other, please explain&#8221; text field placed directly after.  If &#8220;other&#8221; is selected, then the accompanying text field is usually required.  Since there&#8217;s not a standard <a href="http://framework.zend.com/manual/en/zend.validate.html">Zend Validate validator</a> for this scenario, I&#8217;ve written a custom validator that seems to do the trick.</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;">/**
 * Kendall Extensions
 * 
 * @category Kendall
 * @package  Kendall_Validate
 * @author   Jeremy Kendall 
 */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * @see Zend_Validate_Abstract
 */</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Zend/Validate/Abstract.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Requires field presence based on provided value of radio element.  
 * 
 * Example would be radio element with Yes, No, Other option, followed by an &quot;If 
 * other, please explain&quot; text area.
 * 
 * IMPORTANT: For this validator to work, allowEmpty must be set to false on 
 * the child element being validated.
 * 
 * From Zend Framework Documentation 15.3: &quot;By default, when an 
 * element is required, a flag, 'allowEmpty', is also true. This means that if 
 * a value evaluating to empty is passed to isValid(), the validators will be 
 * skipped. You can toggle this flag using the accessor setAllowEmpty($flag); 
 * when the flag is false, then if a value is passed, the validators will still 
 * run.&quot;
 * 
 * @uses     Zend_Validate_Abstract
 * @category Kendall
 * @package  Kendall_Validate
 * @author   Jeremy Kendall 
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Kendall_Validate_FieldDepends <span style="color: #000000; font-weight: bold;">extends</span> Zend_Validate_Abstract <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Validation failure message key for when the value of the parent field is an empty string
   */</span>
  <span style="color: #000000; font-weight: bold;">const</span> KEY_NOT_FOUND  <span style="color: #339933;">=</span> <span style="color: #0000ff;">'keyNotFound'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Validation failure message key for when the value is an empty string
   */</span>
  <span style="color: #000000; font-weight: bold;">const</span> KEY_IS_EMPTY   <span style="color: #339933;">=</span> <span style="color: #0000ff;">'keyIsEmpty'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Validation failure message template definitions
   *
   * @var array
   */</span>
  protected <span style="color: #000088;">$_messageTemplates</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">KEY_NOT_FOUND</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Parent field does not exist in form input'</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">KEY_IS_EMPTY</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Based on your answer above, this field is required'</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Key to test against
   *
   * @var string|array
   */</span>
  protected <span style="color: #000088;">$_contextKey</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * String to test for
   *
   * @var string
   */</span>
  protected <span style="color: #000088;">$_testValue</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * FieldDepends constructor
   *
   * @param string $contextKey Name of parent field to test against
   * @param string $testValue Value of multi option that, if selected, child field required
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$contextKey</span><span style="color: #339933;">,</span> <span style="color: #000088;">$testValue</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTestValue</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$testValue</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setContextKey</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$contextKey</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Defined by Zend_Validate_Interface
   *
   * Wrapper around doValid()
   *
   * @param  string $value
   * @param  array  $context
   * @return boolean
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isValid<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$contextKey</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContextKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// If context key is an array, doValid for each context key</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$contextKey</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$contextKey</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ck</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setContextKey</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ck</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Returns true if dependant field value is not empty when parent field value
   * indicates that the dependant field is required
   *
   * @param  string $value
   * @param  array  $context
   * @return boolean
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doValid<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$testValue</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTestValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$contextKey</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContextKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$value</span>      <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_setValue<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$contextKey</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_error<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">KEY_NOT_FOUND</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$context</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$contextKey</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$parentField</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$contextKey</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$parentField</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$contextKey</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$testValue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$testValue</span> <span style="color: #339933;">==</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$parentField</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_error<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">KEY_IS_EMPTY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parentField</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_error<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">KEY_IS_EMPTY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * @return string
   */</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> getContextKey<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_contextKey<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * @param string $contextKey
   */</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> setContextKey<span style="color: #009900;">&#40;</span><span style="color: #000088;">$contextKey</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_contextKey <span style="color: #339933;">=</span> <span style="color: #000088;">$contextKey</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * @return string
   */</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> getTestValue <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_testValue<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * @param string $testValue
   */</span>
  protected <span style="color: #000000; font-weight: bold;">function</span> setTestValue <span style="color: #009900;">&#40;</span><span style="color: #000088;">$testValue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_testValue <span style="color: #339933;">=</span> <span style="color: #000088;">$testValue</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The validator above is essentially a conditional NotEmpty validator.  It checks the value of a parent field to see if a child field should be required.  IMPORTANT:  allowEmpty <em>must</em> be set to false on the child field.</p>
<p>Here&#8217;s an example of how to use the validator.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Parent element</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'radio'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'flavor'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'required'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'label'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Choose a flavor'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'multiOptions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Vanilla'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Vanilla'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Chocolate'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Chocolate'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Other'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Other'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Child element. IMPORTANT: allowEmpty must be set to false!</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'flavorOther'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'allowEmpty'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'label'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'If Other, provide flavor here'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'validators'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Kendall_Validate_FieldDepends<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flavor'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Other'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Again, please note that allowEmpty has been set to false on the child field.  This is necessary to run the FieldDepends validator even when the &#8220;If other . . .&#8221; element is empty.</p>
<p>While I&#8217;m sure there&#8217;s plenty of room for refactoring, the above code has served me well.</p>
<p><strong>Adding Validators After Submission but Before Validation</strong></p>
<p>Expanding on the example above, what if it became necessary to add additional validators to the &#8220;If other . . .&#8221; field?  Because the &#8220;If other . . .&#8221; field has allowEmpty set to false, and because an empty value is sometimes a valid value, it is not possible to add additional validators that will run only if the field is not empty.  The additional validators will run regardless of the value of the &#8220;If other . . .&#8221; element, throwing errors when the element is empty.  Additional validators will have to be added somewhere else.</p>
<p>In order to work around this issue, I added a custom method called preValidation() to my form class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> preValidation<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'flavorOther'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">flavorOther</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> FlavorOther_Validator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The preValidation() method is called after submission but before validation.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Flavor_Form<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</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: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Display form</span>
  <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: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> 
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">preValidation</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Failed validation, redisplay form with values and errors</span>
  <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: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">// Passed validation</span></pre></div></div>

<p>While the preValidation() code above adds validation depending on the state of an element in the form, it would be trivial to add validation to the form based on any number of conditions, including conditions that exist as a result of business rules rather than the form&#8217;s input.</p>
<p><strong>Wrapping Up</strong></p>
<p>Writing custom validators for the Zend Framework makes server side validation of unique validation scenarios a breeze.  I have yet to encounter a non-standard validation scenario where I haven&#8217;t been able to address it by writing a custom validator.  With the ability to extend Zend Form with a couple of helpful custom methods, adding additional validation after form submission becomes trivial.</p>
<p>Have you ever had to write any custom validators?  Any suggestions on improving the code above?  Jump down to the comments and let us know!</p>
<p><strong>UPDATED</strong> to add code comments to the validator implementation example.  Thanks to reader <a href="http://www.11outof10.com/">Neil</a> for the suggestion.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremykendall.net/2008/12/24/conditional-form-validation-with-zend_form/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>IEs4Linux Blank Screen Bug and Installation Issues</title>
		<link>http://www.jeremykendall.net/2008/02/16/ies4linux-blank-screen-bug-and-installation-issues/</link>
		<comments>http://www.jeremykendall.net/2008/02/16/ies4linux-blank-screen-bug-and-installation-issues/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 15:38:37 +0000</pubDate>
		<dc:creator>Jeremy Kendall</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[ie]]></category>

		<guid isPermaLink="false">http://dev.jeremykendall.net/wp/?p=17</guid>
		<description><![CDATA[If you want / need to install Internet Explorer in Linux, I highly recommend IEs4Linux by S&#233;rgio Lopes.
IEs4Linux is the simpler way to have Microsoft Internet Explorer running on Linux (or any OS running Wine).
I recently purchased a new Dell 1420n with Ubuntu 7.10 pre-installed.  One of the first things I wanted to do [...]]]></description>
			<content:encoded><![CDATA[<p>If you want / need to install Internet Explorer in Linux, I highly recommend <a href="http://www.tatanka.com.br/ies4linux/page/Main_Page">IEs4Linux</a> by S&eacute;rgio Lopes.</p>
<blockquote><p>IEs4Linux is the simpler way to have Microsoft Internet Explorer running on Linux (or any OS running Wine).</p></blockquote>
<p>I recently purchased a new <a href="http://www.dell.com/ubuntu">Dell 1420n with Ubuntu 7.10 pre-installed</a>.  One of the first things I wanted to do was install all of my development tools, including IEs4Linux.  What should have been an easy installation turned into a nightmare.  I kept running into python errors that aborted the install.  Once I finally got the application to install it was unusable.  The IE toolbar was missing, including the address bar, the application would frequently crash, and I experienced maddening display bugs.</p>
<p>Apparently the blank screen bug has been a widespread issue.  S&eacute;rgio has <a href="http://www.tatanka.com.br/ies4linux/news/54">addressed it</a> on his blog and pushed an emergency release of IEs4Linux, version 2.99.0.1, to resolve this issue only.</p>
<p>That&#8217;s all well and good, but I still had the python issues to deal with.  What finally worked for me was to install IEs4Linux with the &#8211;no-gui flag, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ ./ies4linux --no-gui</pre></div></div>

<p>For more information on installing IEs4Linux, including IE versions 5 and 5.5, visit the <a href="http://www.tatanka.com.br/ies4linux/page/Installation">IEs4Linux installation page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremykendall.net/2008/02/16/ies4linux-blank-screen-bug-and-installation-issues/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mail Notification 5.0 with SSL in Ubuntu</title>
		<link>http://www.jeremykendall.net/2008/02/14/mail-notification-50-with-ssl-in-ubuntu/</link>
		<comments>http://www.jeremykendall.net/2008/02/14/mail-notification-50-with-ssl-in-ubuntu/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 22:24:22 +0000</pubDate>
		<dc:creator>Jeremy Kendall</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://dev.jeremykendall.net/wp/?p=8</guid>
		<description><![CDATA[The Problem
We recently began using SSL to connect to IMAP at work.  Prior to switching to SSL I had been using the excellent Mail Notification to let me know if I had any messages in my inbox.   As soon as we switched over to SSL, Mail Notification quit alerting me to the [...]]]></description>
			<content:encoded><![CDATA[<p><b>The Problem</b></p>
<p>We recently began using SSL to connect to IMAP at work.  Prior to switching to SSL I had been using the excellent <a href="http://www.nongnu.org/mailnotify/">Mail Notification</a> to let me know if I had any messages in my inbox.   As soon as we switched over to SSL, Mail Notification quit alerting me to the presence of new email.</p>
<p>I figured that all I had to do was change my preferences in Mail Notification and select SSL.  Turns out I was right, except I couldn&#8217;t select SSL &#8211; all of the &#8220;SSL/TSL&#8221; options were grayed out.  Why in the world would that be?  After some research I discovered that:</p>
<ul>
<li>SSL isn&#8217;t available if the package was built without SSL support (makes sense)</li>
<li>The OpenSSL license conflicts with the Debian license</li>
</ul>
<p>Mail Notification <i>is</i> available in the Ubuntu repository, but without SSL support.  Bummer.</p>
<p><b>Build it Yourself</b></p>
<p>The resolution is to build Mail Notification with SSL support enabled, but I quickly discovered that building Mail Notification was not as easy as I thought it would be.  Although the installation is well documented in the INSTALL file, I still ran into a lot of problems with dependencies.</p>
<p><b>Dependencies Required</b></p>
<p>After a lot of troubleshooting and not a little frustration, I came up with a list of dependencies that I needed installed in order to configure and make Mail Notification 5.0 on Ubuntu Gutsy:</p>
<ul>
<li>build-essential</li>
<li>gnome-core-devel</li>
<li>libnotify-dev</li>
<li>libgnome2-dev</li>
<li>libgmime-2.0-2-dev</li>
<li>libssl-dev</li>
</ul>
<p>I used the Synaptic Package Manager to install each of these dependencies.  Read on if you&#8217;re looking to troubleshoot a specific error that you&#8217;re running into.</p>
<p><b>Errors and Troubleshooting</b></p>
<p>The first time I ran</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ ./configure</pre></div></div>

<p> I got this error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">checking for C compiler default output file name... 
configure: error: C compiler cannot create executables
See `config.log' for more details.</pre></div></div>

<p>Installing <b>build-essential</b> took care of the C compiler issue, but I found a new one the next time I ran configure:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Error: checking for GNOME... no
configure: error: unable to find the GNOME libraries</pre></div></div>

<p>This one drove me a little batty.  What does it mean it can&#8217;t find the GNOME libraries?  I&#8217;m running GNOME for Pete&#8217;s sake!  After a decent amount of hair pulling and a seemingly endless amount of Googling, I finally found that <b>gnome-core-devel</b>, <b>libnotify-dev</b>, and <b>libgnome2-dev</b> resolved the</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">unable to find the GNOME libraries</pre></div></div>

<p> error.</p>
<p>Next up was the GMIME error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">checking for GMIME... configure: error: Package requirements (gmime-2.0 &gt;= 2.1.0) were not met:
&nbsp;
No package 'gmime-2.0' found</pre></div></div>

<p>At least by now I was making it most of the way through the</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">configure</pre></div></div>

<p> process.  I found that installing <b>libgmime-2.0-2-dev</b> resolved the issue, finally allowing me to complete the configuration.</p>
<p>Of course, the whole point was to build Mail Notify with SSL support.  What do you think I found when configure finally ran all of the way through?  Down at the bottom of the options list, I saw this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">--enable-ssl                 no (OpenSSL not found)</pre></div></div>

<p>By now, I had started seeing a pattern: look up the dependencies, find their dev libraries, install their dev libraries, and voila, on to the next issue.  With that in mind, I installed <b>libssl-dev</b> and ran</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ ./configure</pre></div></div>

<p> one last time.  Mail Notify configured without errors and with SSL support.  A quick</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">make</pre></div></div>

<p> and</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">make install</pre></div></div>

<p> later and I had a Mail Notify 5.0 installation complete with SSL support.</p>
<p><b>Other Options</b></p>
<p>There seem to be a lot of different ways to skin this particular cat.  The solution above is what worked for me, your mileage may vary.  You may find it useful to refer to the discussion in this <a href="https://bugs.launchpad.net/ubuntu/+source/mail-notification/+bug/44335">related bug report</a> for background.  </p>
<p>For another way to resolve this issue, you might try &#8220;<a href="http://www.howtoforge.com/repackage_deb_packages_debian_ubuntu">How to make a small change to a Debian tool and repackage it.</a>&#8221;  I didn&#8217;t find this article until after I had resolved the issue myself, but it looks like it might be a lot simpler.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremykendall.net/2008/02/14/mail-notification-50-with-ssl-in-ubuntu/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
