For the past 10 years Cal has worked with PHP and MySQL on Linux, OSX, and Windows. He has built a variety of projects ranging in size from simple web pages to multi-million dollar web applications. When not banging his head on his monitor, attempting a blood sacrifice to get a particular piece of code working, he enjoys building and managing development teams using his widely imitated but never patented management style of "management by wandering around". Cal is a DZone MVB and is not an employee of DZone and has posted 28 posts at DZone. You can read more from them at their website. View Full User Profile

Lifting the Skirt on Zend Framework 1.5 - Zend_Form

  • submit to reddit

With the preview release of Zend Framework 1.5 now out and things looking good for a General release in late February, let's take a moment to really see what is new. This will be an ongoing series for a while to help you get an idea of what's coming down the pipe.

Many developers like the model of being able to programmatically create all elements on a page. An equal number like to code the HTML separately. If you are one of the ones that like to create things in code, you are going to love Zend Form. Zend_Form gives you all the tools necessary to create forms and form elements via PHP code. Like everything in Zend Framework, almost everything is configurable; however the defaults are probably good for most uses. Zend_Form helps you by simplifying the creation of these form elements as well as adding programmatic controls for validation, ordering, filtering, and grouping. The Zend Framework documentation contains a great Quick-Start Guide written by the component author, Matthew Weier O'Phinney that gives you the basics. To show you just how simple this is to use, here's a quick code snippet.



$form = new Zend_Form;
->setAttrib('id', 'login');

$username = new Zend_Form_Element_Text('username');
->addValidator('regex', false, array('/^[a-z]/i'))

$password = new Zend_Form_Element_Password('password');
$password->addValidator('stringLength', false, array(6))

$form->addElement(new Zend_Form_Element_Submit());

You can begin to get a feel for how this works from that snippet and how simple it is to build not only simple but really complex forms. I've listed the verbose version above so that you can see the granularity of control you have; here is the sparse version.

$form = new Zend_Form(array(
'action' => '/user/login',
'method' => 'post',
'elements' => array(
'username' => array('text', array(
'validators' => array(
array('regex', false, array('/^[a-z]/i')),
array('stringLength', false, array(6, 20))
'required' => true,
'filters' => array('StringToLower')
'password' => array('password', array(
'validators' => array(
array('stringLength', false, array(6))
'required' => true,
'submit' => 'submit',

You will notice that in this version everything is defined in an array and just handed to Zend_Form. If you work with Zend Framework at all you know that if it's an array, it can most likely be a config. Zend_Form is no exception. It can take a Zend_Config as a parameter instead of an array. This allows you to define your form in an INI file. If we were creating our login form via a Zend_Config, the INI would look like this.

; general form metainformation
user.login.action = "/user/login"
user.login.method = "post"

; username element
user.login.elements.username.type = "text"
user.login.elements.username.options.validators.alnum.validator = "alnum"
user.login.elements.username.options.validators.regex.validator = "regex"
user.login.elements.username.options.validators.regex.options.pattern = "/^[a-z]/i"
user.login.elements.username.options.validators.strlen.validator = "StringLength"
user.login.elements.username.options.validators.strlen.options.min = "6"
user.login.elements.username.options.validators.strlen.options.max = "20"
user.login.elements.username.options.required = true
user.login.elements.username.options.filters.lower.filter = "StringToLower"

; password element
user.login.elements.password.type = "password"
user.login.elements.password.options.validators.strlen.validator = "StringLength"
user.login.elements.password.options.validators.strlen.options.min = "6"
user.login.elements.password.options.required = true

; submit element
user.login.elements.submit.type = "submit"

To implement this you simply instantiate your Zend_Config() and then your form:

$config = new Zend_Config_Ini('/path/to/config.ini', 'development')
$form = new Zend_Form($config->user->login);

You can see the complete example, including a controller that uses it not only to display the form but also to validate the results, on the documentation page.

As I said before, some developers prefer to develop like this and others don't. Those who prefer this method will rejoice with singing and dancing because this is a very nice implementation. It is flexible enough to be used for complex forms but the syntax is simple to grasp and won't get in the way when building simple forms.

Those who prefer to code the HTML directly have already stopped reading by now.


This article was originally posted on Zend's Developer Zone and is used by permission.

Published at DZone with permission of Cal Evans, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Paulo Coutinho replied on Sun, 2008/02/03 - 12:44pm



Each day that passed i like PHP much more!!!


I have created a simple class to do it from xml, because dont know that Zend can load from INI.


Very Good :)


Can load from XML intestead of INI ?


Thanks a lot. 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.