Lifting the Skirt on Zend Framework 1.5 - Zend_Form
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.
[img_assist|nid=569|title=|desc=|link=url|url=http://framework.zend.com/manual/en/zend.form.html|align=left|width=240|height=200]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;
$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))
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.