Applying decorators to all elements of form at once

11 Nov

“Read new version of this article here”, http://zendgeek.blogspot.com/2009/07/applying-zendform-decorators-to-all.html.

After writing two posts on how to setDecorators to different form elements I think it is now time to provide a simple way to set decorators to all form elements at once.
Take a look at the following code.

class SimpleForm extends Zend_Form
{
public function __construct($options = null)
{

parent::__construct($options);
$countryList=array(
‘United States’,
‘United Kindom’,
‘Pakistan’
);

$firstName = $this->createElement(‘text’, ‘firstName’);
$firstName ->addValidator(‘alnum’)

->addValidator(‘regex’, false, array(‘/^[a-z]+/’))
->addValidator(‘stringLength’, false, array(6, 20))
->setRequired(true)
-> setLabel(‘First Name:’)
->addFilter(‘StringToLower’);

$lastName = $this->createElement(‘text’, ‘lastName’);
$lastName ->addValidator(‘StringLength’, false, array(6))

->setRequired(true)
-> setLabel(‘Last Name:’);

$address1 = $this->createElement(‘text’, ‘address1’);
$address1->setLabel(‘Address1:’)

->addValidator(‘StringLength’, false,array(3,50))
->setValue(”)
->setRequired(true);

$address2 = $this->createElement(‘text’, ‘address2’);
$address2->setLabel(‘Address2:’)

->addValidator(‘StringLength’, false,array(3,50))
->setValue(”)
->setRequired(false);

$postalCode = $this->createElement(‘text’, ‘postalCode’);
$postalCode->setLabel(‘Postalcode:’)

->addValidator(‘StringLength’, false,array(3,15))
->setValue(”)
->setRequired(false);

$city = $this->createElement(‘text’, ‘city’);
$city->setLabel(‘City:’)

->setValue(”)
->setRequired(false)
->setAttrib(‘tabindex’,’6′);

$state = $this->createElement(‘text’, ‘state’);
$state->setLabel(‘State:’)

->setAttrib(‘maxlength’, 2)
->setValue(”)
->setRequired(false)
->setAttrib(‘tabindex’,’7′);

$country = $this->createElement(‘select’, ‘country’);
$country->setLabel(‘Country:’)

->setAttrib(‘class’,’select’)
->addMultiOptions($countryList)
->setRequired(false);

$phone = $this->createElement(‘text’, ‘phone’);
$phone->setLabel(‘Phone:’)

->setAttrib(‘maxlength’, ’25’)
->setValue(”)
->setRequired(true);

$emailAddress = $this->createElement(‘text’, ’emailAddress’);
$emailAddress->setLabel(‘Email:’)

->addValidator(‘StringLength’, false,array(5,50))
->addValidator(‘EmailAddress’)
->setValue(”)
->setRequired(true);

$this->addElements( array (

$firstName,
$lastName,
$address1,
$address2,
$postalCode,
$city,
$state,
$country,
$phone

));
$this->setElementDecorators(array(

‘viewHelper’,
‘Errors’,
array(array(‘data’=>’HtmlTag’),array(‘tag’=>’td’)),
array(‘Label’,array(‘tag’=>’td’)),
array(array(‘row’=>’HtmlTag’),array(‘tag’=>’tr’))

));
$this->setDecorators(array(

‘FormElements’,
array(array(‘data’=>’HtmlTag’),array(‘tag’=>’table’)),
‘Form’

));
}
}
?>

If you have read my previous post on decorators, you will easily understand the code. However those how are new to my blog and decorators may need some explanation.
As usual we are extending our form from Zend_Form, define our constructor, call parent constructor and then define our elements.
The most important line are after we add elements to our form. The code

$this->setElementDecorators(array(

‘viewHelper’,
‘Errors’,
array(array(‘data’=>’HtmlTag’),array(‘tag’=>’td’)),
array(‘Label’,array(‘tag’=>’td’)),
array(array(‘row’=>’HtmlTag’),array(‘tag’=>’tr’))

));

simply set decorators to all the elements to the form elements. Here we are define that close label in “td”, form elements in “td” and put both these in “tr”.
If you want to set decorators to only few elements then

$this->setElementDecorators(array(

‘viewHelper’,
‘Errors’,
array(array(‘data’=>’HtmlTag’),array(‘tag’=>’td’)),
array(‘Label’,array(‘tag’=>’td’)),
array(array(‘row’=>’HtmlTag’),array(‘tag’=>’tr’))

),array(‘firstname’,’lastname’));

This code will set decorators to only fistname and lastname element of the form. Rest of the form elements will be render in the default decorators.
At the end we define

$this->setDecorators(array(

‘FormElements’,
array(array(‘data’=>’HtmlTag’),array(‘tag’=>’table’)),
‘Form’

));

This define the form level decorators. We are putting “table” tag after “form” tag. So all the row will be wrapped in table.

the above code will creat the following form.

Zend Form decorators applied

Zend Form decorators applied

4 Responses to “Applying decorators to all elements of form at once”

  1. Insane November 11, 2008 at 9:00 pm #

    thnks.😀

  2. Rusland January 7, 2009 at 9:51 pm #

    tnx man

  3. Naji Khattat March 14, 2009 at 9:46 am #

    Thank you very much, for this aricles and other interseting articles on zend_form and zend_form decorators, you help me so much!

    thank you again!

  4. Olga March 19, 2009 at 2:53 pm #

    Good article!
    I have question. I need use tag ‘a’ with ‘input’ . Example:

    Select company from list

    How can i do it with Zend_Form? Partners-list very large, and using ‘select’ impossible, only list in new window, with search etc. At this moment i use decorator “ViewScript”, only for link near input.
    Thanks🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: