Categories


Loading feed
Loading feed
Loading feed

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.

Zend Form

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.

<?php
$form = new Zend_Form;
$form->setAction('/resource/process')
     ->setMethod('post')
     ->setAttrib('id', 'login');

$username = new Zend_Form_Element_Text('username');
$username->addValidator('alnum')
         ->addValidator('regex', false, array('/^[a-z]/i'))
         ->setRequired(true)
         ->addFilter('StringToLower');
$form->addElement($username);

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

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

<?php
$form = new Zend_Form(array(
    'action'   => '/user/login',
    'method'   => 'post',
    'elements' => array(
        'username' => array('text', array(
            'validators' => array(
                'alnum',
                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.

[development]
; 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.

Comments


Tuesday, January 29, 2008
AWESOME! BEAUTIFUL!
2:57PM PST · justinwoods
AMAZING ZF WANTED FUNCTION
3:09PM PST · joenilson
GREAT EXAMPLE
9:01PM PST · dinoboff
Wednesday, January 30, 2008
ADDING FIELDSETS
4:58AM PST · weierophinney
STORING IN INI
5:29AM PST · sicouk
ZEND_CONFIG OFFERS FLEXIBILITY
7:22AM PST · weierophinney
QUICK QUESTION
11:13AM PST · Steve Smith [unregistered]
UGH! FORMS IN CONTROLLERS!!
10:30PM PST · tariquesani
Thursday, January 31, 2008
AWESOME
4:39AM PST · bossakungen
FILES
9:04AM PST · weierophinney
DISPLAY VERSUS BUSINESS LOGIC
9:18AM PST · weierophinney
Friday, February 1, 2008
JUST WOW
6:18AM PST · joeyadms
XML CONFIG
7:00AM PST · jurgen [unregistered]
HOW TO MVC
8:03AM PST · chelala
Saturday, February 2, 2008
GOOD
10:52AM PST · deusx
Friday, February 8, 2008
ZEND_FILTER_INPUT VS ZEND_FORM
7:59AM PST · weierophinney
USING ZEND_FORM IN THE MVC
8:04AM PST · weierophinney
Tuesday, March 18, 2008
GREAT
9:25AM PDT · baonhan
SORRY
9:29AM PDT · baonhan
Friday, May 30, 2008
PLUGINS
6:48AM PDT · kwaadschiks
Sunday, June 29, 2008
@XML CONFIG
3:33PM PDT · Runishe [unregistered]