Goofing off with unit tests

[UPDATE: The library and test have been refactored. See the commit for full details.]

Sometime last year I saw a unit test for the song “Ice Cream Paint Job“.  I thought it was hilarious, and I hate I’ve never been able to find it again.  I liked it so much, in fact, I decided to write my own musical unit integration test.  Behold the MelissaTest, a short, simple test covering the main premise of “Melissa“. Bonus points for me: the test passes.  Enjoy.

<?php
 
namespace MercyfulFate\Album\Melissa\Track;
 
use MercyfulFate\KingDiamond;
use MercyfulFate\Priest;
use MercyfulFate\Witch\Melissa as WitchMelissa;
use MercyfulFate\Album\Melissa\Track\Melissa as TrackMelissa;
 
class MelissaTest extends \PHPUnit_Framework_TestCase
{
 
    /**
     * @var MercyfulFate\KingDiamond
     */
    protected $king;
 
    /**
     * @var MercyfulFate\Priest
     */
    protected $priest;
 
    /**
     * @var MercyfulFate\Witch\Melissa
     */
    protected $witch;
 
    /**
     * @var MercyfulFate\Album\Melissa\Track\Melissa
     */
    protected $trackMelissa;
 
    protected function setUp()
    {
        $this->king = new KingDiamond();
        $this->priest = new Priest();
        $this->priest->attach($this->king);
        $this->witch = new WitchMelissa();
        $this->trackMelissa = new TrackMelissa($this->king, $this->witch, $this->priest);
    }
 
    protected function tearDown()
    {
        $this->trackMelissa = null;
    }
 
    public function testBurnMelissa()
    {
        $this->assertFalse($this->witch->isBurned());
        $this->assertFalse($this->king->swearsRevenge());
 
        $this->trackMelissa->priestBurnsWitch();
 
        $this->assertTrue($this->witch->isBurned());
        $this->assertTrue($this->king->swearsRevenge());
    }
 
}

Two More Apache Tweaks Required After Ubuntu 10.04 Upgrade

While I was troubleshooting why Apache stopped serving php apps from my home directory, I ran into two more annoyances that required attention. I figured I’d share them as well in case you run into them yourself.

Here’s what I saw when I reloaded Apache:

jkendall@san-diego:/etc/apache2$ sudo /etc/init.d/apache2 reload
  * Reloading web server config apache2
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Fri Apr 30 13:47:38 2010] [warn] NameVirtualHost *:80 has no VirtualHosts
                                                                                    [ OK ]

As you can see, Apache reloaded fine, but I don’t like seeing anything other than [ OK ] when I’m reloading my web server. Let’s tackle these one at a time.

Could not reliably determine the server’s fully qualified domain name

Why is Apache all of a sudden complaining about what’s worked for so long? I’m not sure exactly, but thankfully the fix was simple and easy. Simply adding “ServerName localhost” to /etc/apache2/httpd.conf took care of the first complaint (Big thanks to Mohamed Aslam for his clear instructions on how to get this fixed.).

NameVirtualHost *:80 has no VirtualHosts

The problem boiled down to having NameVirtualHost defined in more than one place. In my case, NameVirtualHost was defined both in /ect/apache2/ports.conf and /etc/apache2/sites-available/default. Commenting out the NameVirtualHost *:80 line in /etc/apache2/sites-available/default did the trick (Thanks to the guys in this Server Fault thread, especially to Ivan, for providing the necessary clues to track this one down.).

After making the above changes, reloading Apache didn’t throw any more warnings. w00t!

Upgrading to Ubuntu 10.04 Breaks Serving php from Home Directories

I upgraded to Ubuntu 10.04 this morning and immediately noticed I could no longer serve php applications from my home directory. When I tried to visit one of my projects, http://test.local for example, Firefox opened a download dialog with a message similar to “You have chosen to open index.phtml . . .”

After quite a bit of googling and forum searching, I went to Twitter asking for help. Many thanks to @sypherNL for helping me resolve this one.

It seems that something has changed in /etc/apache2/mods-enabled/php5.conf. If you’re experiencing this same issue, check your php5.conf and see if it matches mine.

<IfModule mod_php5.c>
    <FilesMatch "\.ph(p3?|tml)$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
    # To re-enable php in user directories comment the following lines
    # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
    # prevents .htaccess files from disabling it.
    <IfModule mod_userdir.c>
        <Directory /home/*/public_html>
            php_admin_value engine Off
        </Directory>
    </IfModule>
</IfModule>

If your php5.conf file looks like the one above, simply comment out the

<IfModule mod_userdir.c>

lines as instructed. Once that’s done, restart apache with the following command:

sudo /etc/init.d/apache2 reload

Clear your browser’s cache and try to visit your site again. I didn’t think the fix took at first, but after clearing cache everything worked just fine.

Related Links

From Zero to Zend Framework Project in 10 Minutes

I first started working with the Zend Framework in July of 2007. The framework has come a long way since then, and one of my favorite new components is Zend Tool. Until Zend Tool came on the scene, my least favorite part of any Zend Framework project was getting the project up and running, from zero to “Hello World,” if you will. I always left out some important piece of configuration, screwed up the directory structure, or made some other equally foolish, simple mistake that kept me chasing my tail until I finally got everything set up just right. No longer! Zend Tool does all of that work for me, allowing me to get to work on the meat of my project right away.

Of course, you can’t use Zend Tool until you’ve got the framework installed. Once the framework is installed, there are very specific steps required to fire up Zend Tool. Once Zend Tool builds the project for you, you’re going to need a virtual host set up so that you can actually run the thing. No problem, right?  Well, maybe not.

It can be really challenging for the new guy to get from zero to a Zend Framework project without wanting to chunk his computer out the window (headdesk anyone?). I know it was that way for me. In the interest of saving his sanity and helping the new gal get going, I’ve tried to put all the necessary steps together in one place.

Here’s what we’re going to do:

  • Install the Zend Framework
  • Get Zend Tool up and running
  • Create a new Zend Framework project
  • Get an virtual host up and running
  • Celebrate victory!

Disclaimer

While I’m going try to stay true to my “Zero to Zend Framework Project” thesis, I am making a few assumptions. This tutorial is written for Ubuntu, PHP 5, and Apache. While I’d love to be able to address Windows, Mac, and other *nix distros, I can only share what I know.  I’m also assuming PHP 5 (PHP 5.2.4 at minimum) and Apache are both installed and functioning correctly, and Apache’s mod_rewrite module is up and running. If that’s not the case, please refer to the resources section at the end of this post to find instructions on getting everything ready to go.

Installing the Zend Framework

Installing the Zend Framework is as easy as downloading a .zip or .tar.gz file (your choice) and extracting the contents onto your machine.

  • Create a directory named phplib in your home folder.
  • Head over to Zend Framework: Downloads and download Zend Framework <version_num> Full.
  • Double click on the downloaded file, choose “Extract,” navigate to the phplib directory, and click “Extract.”

As of this writing, the most recent version of the Zend Framework is 1.10.3, so the full path to my Zend Framework installation would be

/home/jkendall/phplib/ZendFramework-1.10.3

IMPORTANT: Make sure that you downloaded Zend Framework <version_num> Full.  That will be important later.

TIP: As new versions of the framework are released, I like to be able to switch between them easily.  I always make a soft link (symbolic link, symlink) to the latest release and name it Zend.  You can either right-click on the ZendFramework-1.10.3 folder and select “Make Link” (make sure to name the link “Zend”), or create a soft link from the command line like so:

~/phplib/ZendFramework-1.10.3$ ln -s ZendFramework-1.10.3 Zend

When a new version of the framework is released, I install the new version to its own folder and switch the soft link to target the directory containing the latest release.  That can come in handy down the road.

Get Zend Tool running

There are a few different ways to get Zend Tool running, perhaps better than mine, but I like to create a bash alias for zf.sh (For information on how to get bash aliases working, see this bash aliases tutorial.). My alias looks like alias zf='/usr/share/phplib/Zend/bin/zf.sh'.

Whichever method you use, make sure to test your alias by calling zf --help from the command line.

Creating a new Zend Framework Project

Now we’re getting to the good stuff.  First, head back to your home directory and create a new directory called public_html.  This is where you’re going to store your Zend Framework project (and any future web projects, for that matter).

Next, cd into the public_html directory and execute the following command:

~/public_html$ zf create project ZeroToZF

That’s all there is to it!  You’ve got your project structure in place, a default IndexController and ErrorController, your application config, the necessary view scripts, etc (See the Zend Application Quick Start for full rundown of what got created.).

IMPORTANT: While the full project structure is now in place, the Zend Framework is not included in your project for you.  Deciding how the framework should be included in the project is up to the developer.  I like to copy the library out of the Zend Framework install folder into the application’s library folder.  In our example, I would copy

/home/jkendall/phplib/ZendFramework-1.10.3/library/Zend

into

/home/jkendall/public_html/ZeroToZF/library

When done properly, the full path to Zend/Log.php in your application should be

/home/<username>/public_html/ZeroToZF/library/Zend/Log.php

Sweet!  We’re almost there.

Creating a Virtual Host

There are a decent number of steps here, but executing them is straightforward, so bear with me.  Also, a lot of these steps need to be executed from the command line.  Forewarned is forearmed and all that.

If you have not done so already, open the terminal application (Applications -> Accessories -> Terminal) and cd into /etc/apache2/sites-available.  Here you’ll find virtual host definitions.  Most likely you’ll see a file named default (or possibly 000-default).  Peek at that if you’d like to see an example of a virtual host, but what we’re going to put together is a lot simpler.

IMPORTANT: Since /etc and its subfolders are owned by root, you’ll need to run all of the following commands as root.  We’ll be using both the sudo and the gksudo commands to do that.

After using the command line to cd into /etc/apache2/sites-available, execute the following command to create your own vhost file:

/etc/apache2/sites-available$ gksudo gedit ZeroToZf.local

The file extension isn’t important, a lot of people use .conf, but I like to use .local for sites hosted locally.

Once you’ve got gedit open, the file’s contents should look like this:

<VirtualHost *:80>
        ServerName zerotozf.local
        DocumentRoot /home/jkendall/public_html/ZeroToZF/public
</VirtualHost>

Of course, jkendall should be replaced by your username.

Next we need to make your new virtual host file available to Apache.  You can do that by executing

/etc/apache2/sites-available$ sudo a2ensite ZeroToZF.local

If everything worked properly, you should see the following message:

Enabling site ZeroToZF.local.
Run '/etc/init.d/apache2 reload' to activate new configuration!

Next, edit your /etc/hosts file by adding zerotozf.local like so:

/etc/apache2/sites-available$ gksudo gedit /etc/hosts

Add the following line below the entry for localhost

127.0.0.1 zerotozf.local

NOTE: When adding a host to /etc/hosts, the case of the hostname is not important.  I like to add hostnames in lowercase, but you can add it as ZeroToZF.local if you like.

Save the file, close gedit, and execute

/etc/apache2/sites-available$ sudo /etc/init.d/apache2 reload

You should see the message

* Reloading web server config apache2                                          [ OK ]

Open up your browser and visit http://zerotozf.local.  You should see the Zend Framework welcome screen.

Congrats!  You did it!

Next Steps

Now that you’ve got your first project up and running, you’re probably going to want to actually do something with it.  I’d highly recommend heading over to Rob Allen’s site for his excellent Getting Started with Zend Framework tutorial.  It’s the same tutorial I followed when I first started with the framework (an earlier version, of course), and I’ve referred back to it many times since.

Wrapping Up

Getting started with the Zend Framework was a challenging proposition for me.  Just getting to the point where I could start working on a project was sometimes maddening as a result of all the steps involved, many of which had nothing to do with the framework, at least not directly.  I’ve tried, hopefully successfully, to lay out all the steps you might need to get from zero to a Zend Framework project in (about) 10 minutes.

Working with the Zend Framework has been a rich and rewarding experience for me.  I’ve learned almost everything I know about best practices, object oriented programming, *nix, and Apache (to name just a few) as a direct or indirect result of the Zend Framework.  I wouldn’t have been able to do that without the help of the Zend Framework community.  I won’t name names, as I’m sure to unintentionally leave out some great folks, so I’ll throw you a link to the Zend Framework Community Forum.  Head over there when you’ve got a Zend Framework problem you just can’t solve on your own.  You’ll meet some great folks and learn a lot in the process.  They’ve saved my bacon more than once.

If I’ve left out any steps or made some egregious error, please let me know in the comments.  I’ll be grateful and post updates and corrections as soon as possible.

Resources

DIY Network Monitoring and Logging with Perl

The Problem

I’ve been having trouble with my AT&T DSL installation here at the new place. My internet connection will come and go, seemingly at random, and for random amounts of time. I tweeted about it once already, hoping that sharing my frustration with the world might make me feel a little better. Nope. Didn’t work.

I’ve had this problem before with AT&T, when I got DSL installed at my last place. Things were rough for a while, and then somehow they seemed to straighten out on their own. I’m not crossing my fingers that I’ll have such luck again.

What’s Really Going On?

I decided to try and log the bounces so that I could get a better feel for what was going on. I couldn’t find exactly what I wanted online (and I couldn’t always get online), so I whipped up a Perl script to keep an eye on my connection for me.

#!/usr/bin/perl
 
#
# Test AT&T DSL connectivity. If network is down, log to file
# Will use log info as ammo when I call tech support.
#
 
use warnings;
use strict;
use Log::Handler;
use Net::Ping;
use Sys::HostIP;
 
my $log = Log::Handler->new();
 
$log->add(
    file => {
        filename => "/var/log/testNetwork.log",
        mode     => "append",
        maxlevel => "info",
        minlevel => "warning",
    }
);
 
my $ipAddress          = Sys::HostIP->ip; 
my $matchHomeNetworkIp = ($ipAddress =~ /^192\.168\.10\.\d{1,3}$/);
 
if (!$matchHomeNetworkIp) {
    $log->debug("Current IP $ipAddress does not appear to be on your home network. Exiting");
    exit;
}
 
my $router = "192.168.10.1";
my $modem  = "192.168.1.254";
my $host   = "www.yahoo.com";
 
my $p = Net::Ping->new("icmp");
 
if (!$p->ping($router)) {
    $log->warning("Router $router is unreachable. Exiting.");
    exit;
}
 
if (!$p->ping($modem)) {
    $log->warning("Modem $modem is unreachable. Exiting.");
    exit;
}
 
if ($p->ping($host, 2)) {
    $log->info("$host is reachable");
} else {
    $log->warning("$host is NOT reachable");
}
 
$p->close();

Code Review

While the code is simple, there are a couple of things to note. First, you’ll notice I’ve baked my router’s IP range into a regex that will tell me if I can even get that far. If not, there’s no point in checking anything else, so I exit.

Next I fire up Net::Ping. While there are six different options for Net::Ping->new(), the only one that worked for me was icmp. Icmp requires root privileges to run, so keep that in mind.

Next I ping important parts of the home network. I want to be sure that my wireless router and the DSL modem are both online before I try and ping an external site.

If everything looks good on the inside, I’ll ping an external site and see if I get anything back. You’ll notice that I’ve added a second parameter to ping this time. That’s the number of seconds I’ll wait for a response before failing (of course, it is possible that Yahoo! won’t respond to a ping request here and there, making it appear as if the network is down, but I’m not so worried about a false positive or two).

Implementation

Now that I’ve got this nifty script to tell me about the health of my network, I need a way to run it on a regular basis. It’s important to run it as root because of the icmp ping. Cron was the obvious solution, as it’s purpose is to run commands on a predetermined schedule. As long as I add the script to root’s cron file, it’ll run with root permissions. Sweet!

Adding my script to the root cron file was as easy as issuing

sudo crontab -e

and adding the following line:

*/1 * * * * perl /home/jkendall/dev/perl/util/testNetwork.pl

With my cron file in place, my script fires off every minute. As long as I’m on my home network, I’ll get a log entry telling me whether or not the network is up.

Keeping an Eye on the Results

Now that my script is actually doing something, I need to be able to parse the results. A quick

tail -f /var/log/testNetwork.log

allows me to keep track of the results as they come in. I’m also using grep to pull all of the “NOT reachable” lines out of the log file with

grep -n --color "NOT reachable" /var/log/testNetwork.log

I could always just open the log file and read through it from top to bottom, but what fun is that? A decent tool to parse the fail log is going to be necessary, but I haven’t whipped it up yet.

Teh Suck

As I’ve been writing this blog post, I’ve been running this script in the background. Here are the results of an hour or so of logging (all times are CDT):

Oct 27 21:03:33 [INFO] www.yahoo.com is reachable
Oct 27 21:04:03 [WARNING] Router 192.168.10.1 is unreachable. Exiting.
Oct 27 21:05:02 [INFO] www.yahoo.com is reachable
Oct 27 21:06:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:07:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:08:42 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:09:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:10:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:11:02 [INFO] www.yahoo.com is reachable
Oct 27 21:12:01 [INFO] www.yahoo.com is reachable
Oct 27 21:13:01 [INFO] www.yahoo.com is reachable
Oct 27 21:14:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:15:32 [INFO] www.yahoo.com is reachable
Oct 27 21:16:01 [INFO] www.yahoo.com is reachable
Oct 27 21:17:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:18:42 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:19:11 [INFO] www.yahoo.com is reachable
Oct 27 21:20:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:21:42 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:22:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:23:03 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:24:01 [INFO] www.yahoo.com is reachable
Oct 27 21:25:42 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:26:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:27:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:28:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:29:42 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:30:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:31:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:32:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:33:42 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:34:21 [INFO] www.yahoo.com is reachable
Oct 27 21:35:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:36:42 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:37:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:38:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:39:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:40:12 [INFO] www.yahoo.com is reachable
Oct 27 21:41:01 [INFO] www.yahoo.com is reachable
Oct 27 21:42:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:43:42 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:44:31 [INFO] www.yahoo.com is reachable
Oct 27 21:45:01 [INFO] www.yahoo.com is reachable
Oct 27 21:46:42 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:47:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:48:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:49:42 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:50:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:51:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:52:42 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:53:41 [WARNING] www.yahoo.com is NOT reachable
Oct 27 21:54:01 [INFO] www.yahoo.com is reachable

Weak sauce, man. Weak sauce.

Wrapping Up

Having this script handy doesn’t make the fail any better, but at least I know a little more about what’s happening. I’d like to think it’ll give me some leverage when I try to get this worked out with AT&T, but who knows. I’ll let you know how it goes.

[Update: post title change to better reflect content]

ZF-7984 – Zend_Tool Exits with Fatal Errors after installing PHPUnit 3.4.0+

I’m lucky enough to have made it to ZendCon again this year, and I’m having a blast learning new stuff, hanging out with old friends, making new friends, and generally grabbing up as much schwag as possible.

One of the topics that I’m most interested in is unit testing, specifically unit testing Zend Framework MVC apps.  While there’s a lot I have yet to learn on that topic, I ran into a bug last night that I wanted to let you know about.

In preparation to dig into ZF unit testing, I updated my install of PHPUnit to the latest version (currently 3.4.1, installed via PEAR).  When I tried to create a new ZF project using Zend_Tool, I received the following error:

jkendall@san-diego:~/dev/www$ zf create project asplode
 
Fatal error: Cannot redeclare class phpunit_framework_testsuite_dataprovider in /usr/share/php/PHPUnit/Framework/TestSuite/DataProvider.php on line 64
 
Call Stack:
    0.0020     111440   1. {main}() /usr/share/phplib/ZendFramework-1.9.3PL1/bin/zf.php:0
    0.0020     111560   2. zf_main() /usr/share/phplib/ZendFramework-1.9.3PL1/bin/zf.php:23
    0.0220     686832   3. zf_run($zfConfig = array ('HOME' => '/home/jkendall')) /usr/share/phplib/ZendFramework-1.9.3PL1/bin/zf.php:36
    0.0221     686952   4. Zend_Tool_Framework_Client_Console::main($options = array ()) /usr/share/phplib/ZendFramework-1.9.3PL1/bin/zf.php:214
    0.0221     687440   5. Zend_Tool_Framework_Client_Abstract->dispatch() /usr/share/phplib/ZendFramework-1.9.3PL1/library/Zend/Tool/Framework/Client/Console.php:96
    0.0222     687560   6. Zend_Tool_Framework_Client_Abstract->initialize() /usr/share/phplib/ZendFramework-1.9.3PL1/library/Zend/Tool/Framework/Client/Abstract.php:209
    0.0296     866600   7. Zend_Tool_Framework_Loader_Abstract->load() /usr/share/phplib/ZendFramework-1.9.3PL1/library/Zend/Tool/Framework/Client/Abstract.php:118
    0.4100    2729736   8. include_once('/usr/share/php/PHPUnit/Framework/TestSuite/DataProvider.php') /usr/share/phplib/ZendFramework-1.9.3PL1/library/Zend/Tool/Framework/Loader/Abstract.php:90
 
jkendall@san-diego:~/dev/www$

As it turns out, this is a known bug in version 1.9.0+ of the Zend Framework.  See ZF-7894 in the ZF issue tracker for full details.  While this issue is not yet resolved in the tracker, Raphael Stolt has provided a workaround in the form of a diff file attached to the issue.  While your mileage may vary, the patch worked perfectly for me.  I’m able to go ahead and dive into unit testing my Zend Framework applications.

UPDATE: ZF-7894 was resolved during Bug Hunt days this week. Many thanks to Benjamin Eberlei!

Dynamically Adding Elements to Zend_Form

There have been some requests on the Zend Framework mailing lists for information on how to dynamically add elements to Zend_Form.  This is something that I’ve been looking into myself, and I’d like to share what I’ve come up with.

Please note that this code is a proof of concept / request for peer review detailing the work that I’ve done to date, and not an example of what I might consider the best way to address this use case.  Special thanks go to Cory Wiles who helped me think things through when I first started giving this a go.

First, let’s do a high level walk through of what the code is going to do, then take a look at the code, and wrap up with a live example.

High Level Overview

The form in this example extends Zend_Form and consists of a hidden element that stores an ID, a single text element, buttons for adding and removing dynamic elements, and a submit button.  The add and remove buttons are used to trigger a jQuery script that adds and removes dynamic elements. The jQuery script uses the value of the hidden ID element to set element order and make the dynamic element names and IDs unique.

The form class consists of the standard init() method for building the form and two custom methods for dealing with dynamic elements.  There is a preValidation() method, called after the form is submitted but before it is validated, that searches the submitted form data for dynamically added fields.  If any new fields are found, the addNewField() method takes care of adding the new fields to the form.

jQuery is used to request the new form element from the form’s Controller via Ajax, utilizing the AjaxContext action helper. jQuery is also used to find the most recently added dynamic element, allowing for easy removal of dynamically added elements from the form.

The action controller contains the action that displays the form, and it also has a newfieldAction() that utilizes the AjaxContext to return markup for new fields.

The Zend_Form Subclass

Let’s start with the code for the form.  The most important item here is that each form element has its order property set.  You can see the huge jump in the order between the “name” element and the  “addElement” button.  This gap occurs so the dynamic elements can be placed exactly where I want them and so they’ll maintain their position in the form once they’ve been added to the form object.

public function init() {
 
  $this->addElement('hidden', 'id', array(
    'value' => 1
  ));
 
  $this->addElement('text', 'name', array(
    'required' => true,
    'label'    => 'Name',
    'order'    => 2,
  ));
 
  $this->addElement('button', 'addElement', array(
    'label' => 'Add',
    'order' => 91
  ));
 
  $this->addElement('button', 'removeElement', array(
    'label' => 'Remove',
    'order' => 92
  ));
 
  // Submit
  $this->addElement('submit', 'submit', array(
    'label' => 'Submit',
    'order' => 93
  ));
}

Action Controller

The action that displays the form is straightforward.  If you’ve ever done any work with Zend_Form, I’m sure you recognize what’s going on here.  The only thing to note is the $form->preValidation() method.  That’s where the magic happens.  We’ll get to that in a bit.

/**
 * Shows the dynamic form demonstration page
 */
public function dynamicFormElementsAction() {
 
  $form = new Code_Form_Dynamic();
 
  // Form has not been submitted - pass to view and return
  if (!$this->getRequest()->isPost()) {
    $this->view->form = $form;
    return;
  }
 
   // Form has been submitted - run data through preValidation()
  $form->preValidation($_POST);
 
   // If the form doesn't validate, pass to view and return
  if (!$form->isValid($_POST)) {
    $this->view->form = $form;
    return;
  }
 
   // Form is valid
  $this->view->form = $form;
}

Next comes the controller’s newfieldAction().  This action utilizes the AjaxContext action helper to pass the new field’s markup back to the form view.

/**
 * Ajax action that returns the dynamic form field
 */
public function newfieldAction() {
 
  $ajaxContext = $this->_helper->getHelper('AjaxContext');
  $ajaxContext->addActionContext('newfield', 'html')->initContext();
 
  $id = $this->_getParam('id', null);
 
  $element = new Zend_Form_Element_Text("newName$id");
  $element->setRequired(true)->setLabel('Name');
 
  $this->view->field = $element->__toString();
}

jQuery

The jQuery script is also fairly straightforward.  I attach event listeners to the “Add” and “Remove” buttons that call the ajaxAddField and removeField methods respectively.

The ajaxAddField method makes a post request to the newfieldAction using jQuery’s .ajax method, passing in the current value of the hidden ID element.  On success, the new element’s markup is added to the form, and the ID is incremented and stored in the hidden ID element.

The removeField method finds the last element in the page with the class dynamic, removes it, then decrements the current ID and stores the new value in the hidden ID element.

<script type="text/javascript">
 
$(document).ready(function() {
 
  $("#addElement").click( 
      function() { 
          ajaxAddField();
       }
    );
 
  $("#removeElement").click(
      function() {
          removeField();
      }
    );
  }
);
 
// Get value of id - integer appended to dynamic form field names and ids
var id = $("#id").val();
 
// Retrieve new element's html from controller
function ajaxAddField() {
  $.ajax(
    {
      type: "POST",
      url: "<?=$this->url(array('action' => 'newfield', 'format' => 'html'));?>",
      data: "id=" + id,
      success: function(newElement) {
 
        // Insert new element before the Add button
        $("#addElement-label").before(newElement);
 
        // Increment and store id
        $("#id").val(++id);
      }
    }
  );
}
 
function removeField() {
 
  // Get the last used id
  var lastId = $("#id").val() - 1;
 
  // Build the attribute search string.  This will match the last added  dt and dd elements.  
  // Specifically, it matches any element where the id begins with 'newName<int>-'.
  searchString = '*[id^=newName' + lastId + '-]';
 
  // Remove the elements that match the search string.
  $(searchString).remove()
 
  // Decrement and store id
  $("#id").val(--id);
}
</script>

Zend_Form: preValidation() and addNewField()

Now on to the fun stuff.  All of the code up to this point is present to support what happens in the form’s preValidation() method.  Remember that preValidation() is called after the form has been submitted but before the form is validated.  preValidation() searches through the submitted form’s data for new fields.  If it finds any new fields, it calls addNewField() and adds the new fields to the form object.  By adding the new form fields to the form object before validation, any filters and validators attached to the new fields will be run as if those fields had always existed in the form object.

/**
 * After post, pre validation hook
 * 
 * Finds all fields where name includes 'newName' and uses addNewField to add
 * them to the form object
 * 
 * @param array $data $_GET or $_POST
 */
public function preValidation(array $data) {
 
  // array_filter callback
  function findFields($field) {
    // return field names that include 'newName'
    if (strpos($field, 'newName') !== false) {
      return $field;
    }
  }
 
  // Search $data for dynamically added fields using findFields callback
  $newFields = array_filter(array_keys($data), 'findFields');
 
  foreach ($newFields as $fieldName) {
    // strip the id number off of the field name and use it to set new order
    $order = ltrim($fieldName, 'newName') + 2;
    $this->addNewField($fieldName, $data[$fieldName], $order);
  }
}
 
/**
 * Adds new fields to form
 *
 * @param string $name
 * @param string $value
 * @param int    $order
 */
public function addNewField($name, $value, $order) {
 
  $this->addElement('text', $name, array(
    'required'       => true,
    'label'          => 'Name',
    'value'          => $value,
    'order'          => $order
  ));
}

Live Example

If you’d like to see working version of this proof of concept, please visit the live example at code.jeremykendall.net.

Summary

The ability to dynamically add form fields to Zend_Form is a feature I’d really like to see added to Zend_Form.  If I were talented enough, I might attempt to make a formal proposal myself.  In the meantime, what I’ve come up with can perhaps serve as a starting point for adding very simple elements to very simple forms.

Thanks again to Cory Wiles for helping me work out some of the kinks during the planning phase. Any mistakes, bad practices, or egregious coding errors are the result of my implementation, not his insight and suggestions.

Request for Comments / Peer Review

If you’ve made it this far, I’m grateful to you for hanging in with me.  If you have suggestions for improvements to the code, an implementation of your own, or if you see mistakes I’ve made or poor practices that I’ve employed, I’d appreciate your input.  Thank you in advance for taking the time to discuss this concept with myself and with the ZF community at large.

Full Controller, Form, and View Code

If you’re interested in the complete code for the controller, form, and views, I’ve posted them over at pastebin. Follow the links below to view / grab the code.

Further reading:

Conditional Form Validation with Zend_Form

A question from ‘ronny stalker’ in the Zend_Form_Element_Multi – 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 – 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 get a secondary argument called $context – which can be used to check values of other fields, but how can a validator get knowledge of other variables in the environment?

While this post may not answer ronny’s question exactly, hopefully it will give him a good starting point to get over the hump.

If other, please explain – Conditional Validation Using $context

Many forms have a set of radio buttons, or sometimes a select element, where a user can choose from one of several options.  Sometimes “other”  will be one of those options, with a corresponding “If other, please explain” text field placed directly after.  If “other” is selected, then the accompanying text field is usually required.  Since there’s not a standard Zend Validate validator for this scenario, I’ve written a custom validator that seems to do the trick.

<?php
/**
 * Kendall Extensions
 * 
 * @category Kendall
 * @package  Kendall_Validate
 * @author   Jeremy Kendall 
 */
 
/**
 * @see Zend_Validate_Abstract
 */
require_once 'Zend/Validate/Abstract.php';
 
/**
 * Requires field presence based on provided value of radio element.  
 * 
 * Example would be radio element with Yes, No, Other option, followed by an "If 
 * other, please explain" 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: "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."
 * 
 * @uses     Zend_Validate_Abstract
 * @category Kendall
 * @package  Kendall_Validate
 * @author   Jeremy Kendall 
 */
class Kendall_Validate_FieldDepends extends Zend_Validate_Abstract {
 
  /**
   * Validation failure message key for when the value of the parent field is an empty string
   */
  const KEY_NOT_FOUND  = 'keyNotFound';
 
  /**
   * Validation failure message key for when the value is an empty string
   */
  const KEY_IS_EMPTY   = 'keyIsEmpty';
 
  /**
   * Validation failure message template definitions
   *
   * @var array
   */
  protected $_messageTemplates = array(
    self::KEY_NOT_FOUND  => 'Parent field does not exist in form input',
    self::KEY_IS_EMPTY   => 'Based on your answer above, this field is required',
  );
 
  /**
   * Key to test against
   *
   * @var string|array
   */
  protected $_contextKey;
 
  /**
   * String to test for
   *
   * @var string
   */
  protected $_testValue;
 
  /**
   * 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
   */
  public function __construct($contextKey, $testValue = null) {
    $this->setTestValue($testValue);
    $this->setContextKey($contextKey);
  }
 
  /**
   * Defined by Zend_Validate_Interface
   *
   * Wrapper around doValid()
   *
   * @param  string $value
   * @param  array  $context
   * @return boolean
   */
  public function isValid($value, $context = null) {
 
    $contextKey = $this->getContextKey();
 
    // If context key is an array, doValid for each context key
    if (is_array($contextKey)) {
      foreach ($contextKey as $ck) {
        $this->setContextKey($ck);
        if(!$this->doValid($value, $context)) {
          return false;
        }
      }
    } else {
      if(!$this->doValid($value, $context)) {
        return false;
      }
    }
    return true;
  }
 
  /**
   * 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
   */
  public function doValid($value, $context = null) {
    $testValue  = $this->getTestValue();
    $contextKey = $this->getContextKey();
    $value      = (string) $value;
    $this->_setValue($value);
 
    if ((null === $context) || !is_array($context) || !array_key_exists($contextKey, $context)) {
      $this->_error(self::KEY_NOT_FOUND);
      return false;
    }
 
    if (is_array($context[$contextKey])) {
      $parentField = $context[$contextKey][0];
    } else {
      $parentField = $context[$contextKey];
    }
 
    if ($testValue) {
      if ($testValue == ($parentField) && empty($value)) {
        $this->_error(self::KEY_IS_EMPTY);
        return false;
      }
    } else {
      if (!empty($parentField) && empty($value)) {
        $this->_error(self::KEY_IS_EMPTY);
        return false;
      }
    }
 
    return true;
  }
 
  /**
   * @return string
   */
  protected function getContextKey() {
    return $this->_contextKey;
  }
 
  /**
   * @param string $contextKey
   */
  protected function setContextKey($contextKey) {
    $this->_contextKey = $contextKey;
  }
 
  /**
   * @return string
   */
  protected function getTestValue () {
    return $this->_testValue;
  }
 
  /**
   * @param string $testValue
   */
  protected function setTestValue ($testValue) {
    $this->_testValue = $testValue;
  }
}

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 must be set to false on the child field.

Here’s an example of how to use the validator.

// Parent element
$this->addElement('radio', 'flavor', array(
  'required'     => true,
  'label'        => 'Choose a flavor',
  'multiOptions' => array('Vanilla' => 'Vanilla', 'Chocolate' => 'Chocolate', 'Other' => 'Other')
));
 
// Child element. IMPORTANT: allowEmpty must be set to false!
$this->addElement('text', 'flavorOther', array(
  'allowEmpty' => false,
  'label'      => 'If Other, provide flavor here',
  'validators' => array(new Kendall_Validate_FieldDepends('flavor', 'Other')),
));

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 “If other . . .” element is empty.

While I’m sure there’s plenty of room for refactoring, the above code has served me well.

Adding Validators After Submission but Before Validation

Expanding on the example above, what if it became necessary to add additional validators to the “If other . . .” field?  Because the “If other . . .” 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 “If other . . .” element, throwing errors when the element is empty.  Additional validators will have to be added somewhere else.

In order to work around this issue, I added a custom method called preValidation() to my form class.

public function preValidation($data) {
 
  if (!empty($data['flavorOther'])) {
    $this->flavorOther->addValidator(new FlavorOther_Validator());
  }
 
  return $data;
}

The preValidation() method is called after submission but before validation.

$form = new Flavor_Form();
 
if (!$this->getRequest()->isPost()) {
  // Display form
  $this->view->form = $form;
  return;
} 
 
$data = $form->preValidation($_POST);
 
if (!$form->isValid($data)) {
  // Failed validation, redisplay form with values and errors
  $this->view->form = $form;
  return;
}
 
 
// Passed validation

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’s input.

Wrapping Up

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’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.

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!

UPDATED to add code comments to the validator implementation example. Thanks to reader Neil for the suggestion.

Zebra Tables with jQuery

I’ve used the classic A List Apart Zebra Tables technique to stripe my tables for years.  It’s always worked well, and I never really considered updating the technique until last week.  I’ve been making heavy use of the jQuery library lately, and I really disliked including another external js file whenever I wanted to stripe a table, so I thought I’d see if someone had come up with a jQuery friendly table striping technique.  It took about 10 seconds on Google to find what I was looking for, and the solution was so simple and elegant that I wanted to kick myself for not thinking of it, er, myself.

For the whole scoop, head over and read the tutorial.  If you’re like me, you want to get right to the point, so here goes.

The idea is to use jQuery to select alternate rows from your table and apply a css class to them. In the example below, the table has a class of ‘striped’ and jQuery adds the class ‘alt’ to the even rows.

$(document).ready(function(){
    $(".striped tr:even").addClass("alt");
});

Whip up a little css that adds a background color to .alt and you’re done. Not bad, huh?

The tutorial author also included an example of jQuery code that allows for a nice hover effect when you mouse over the table rows.  I wasn’t as interested in that, so you’ll have to head over there for the scoop.

Zend_Form_Element_Multi – Tips and Tricks

I’m responsible for creating a lot of forms at my day job. It seems that any project I get involved in requires at least one form. The Zend Form component has made my life a lot easier. After putting together more forms than I can count, I’ve picked up a couple of tricks that I’d like to share. Here are some for the Zend_Form_Element_Multi elements.

As noted in the API documentation, Zend_Form_Element_Multi is the base class for multi-option form elements. Its direct descendants are the Zend Form Select, Radio, and MultiCheckbox elements. Adding options to these elements is possible using the addMultiOptions method. Most of what I want to cover is about retrieving, creating, and adding options, with a short detour into validation.

Using array_combine

Sometimes you want the displayed element options to be the same options returned by the form (as opposed to displaying a string while returning an id). Perhaps you’ll be sending the value(s) along in an email or storing them as strings in a database. While you can create an associative array with matching keys and values, the process quickly becomes tedious with an array of any appreciable size. Why not use array_combine to make life easier?

$options = array('Vanilla', 'Chocolate', 'Strawberry', 'Cookies and Cream', 'Chocolate Chip');
$options = array_combine($options, $options);

Using array_merge

array_merge is helpful when you’d like to add an item to your options array that isn’t already a part of the options array. For example, I frequently add a “Please make a selection” option to my select elements. Extending the above example, I might choose to add the new option like this:

$options = array_merge(array('Please select a flavor'), $options);

One word of caution: array_merge will reindex numerically indexed arrays. array_merge should never be used in a situation where the original array needs to be preserved, such as a numerically indexed array of id and value pairs pulled from a database. In those cases, I use the + operator.

// $options is an array of database ids and flavor descriptions
$options = array('Select a flavor') + $options;

Retrieving options using Zend_Db

I frequently retrieve options from a database, using the record id as the array’s index and a related string as the array’s value. There are a lot of ways retrieve options using Zend_Db, but my favorite is the fetchPairs method.

The fetchPairs() method returns data in an array of key-value pairs, as an associative array with a single entry per row. The key of this associative array is taken from the first column returned by the SELECT query. The value is taken from the second column returned by the SELECT query. Any other columns returned by the query are discarded.

Here’s what that might look like.

$select = 'SELECT flavor_id, flavor FROM flavors';
$options = $db->fetchPairs($select);

I especially enjoy using this method with Zend_Db_Table, using custom table class methods to retrieve my options. My table class usually looks like this:

class Flavors extends Zend_Db_Table_Abstract {
 
  protected $_name = 'flavors';
 
  public function getFlavorOptions() {
 
    $select = $this->select()->from($this, array('flavor_id', 'flavor'));
    $result = $this->getAdapter()->fetchPairs($select);
 
    return $result;
  }
}

Grabbing your options now becomes ridiculously simple.

$flavors = new Flavors();
$flavorOptions = $flavors->getFlavorOptions();

Validation with Zend_Validate_InArray

Zend_Validate_InArray is the default validator for Multi elements, but the InArray validator can be a little tricky to implement properly. Below are the two gotchas that I’ve run into.

Let’s say you’ve got a select element in your form. In order to force the user to select an option, you’ve added a “Please Select” option to the beginning of your options array. If you use the default InArray validation against the full list of options, “Please Select” becomes a valid selection, and you may end up stuck with a lot of bad form submissions. In order to get around this, I make sure to pass the original array of options to the validator, and use array_merge or the + operator to add the “Please select” option before adding the options to the element.

// Create list of flavor options
$flavorOptions = array('Vanilla', 'Chocolate', 'Strawberry', 'Cookies and Cream', 'Chocolate Chip');
$flavorOptions = array_combine($flavorOptions, $flavorOptions);
 
// Add "Select a flavor" option
$flavorMultiOptions = array_merge(array('Select a flavor'), $flavorOptions);
 
// Add flavor options to flavor select element
$form->flavor->addMultiOptions($flavorMultiOptions);
 
// Add validation, validating against original $flavorOptions array
$form->flavor->addValidator(new Zend_Validate_InArray($flavorOptions));

The second gotcha has to do with option arrays where the keys and values don’t match. InArray tests the element’s selected value against the values of the options array, but what you really want to do is test the element’s selected value against the keys of the options array. The trick is to use PHP’s array_keys function.

// Get flavor ids and descriptions from database
$flavors = new Flavors();
$flavorOptions = $flavors->getFlavorOptions();
 
// Add "Select a flavor" option, preserving original array with the + operator
$flavorMultiOptions = array('Select a flavor') + $flavorOptions;
 
// Add flavor options to flavor select element
$form->flavor->addMultiOptions($flavorMultiOptions);
 
// Add validation, validating against array keys of the original $flavorOptions array
$form->flavor->addValidator(new Zend_Validate_InArray(array_keys($flavorOptions)));

Do you have any Zend_Form tips or tricks that you’d like to share? Have I made any egregious errors above that need to be corrected? Hit the comments and let me know.