Zend Framework: Building forms without decorators

When I first started using Zend Framework, I created forms using the standard form decorators. But I found in many instances, I needed more flexibility when creating forms, so now, I tend to build forms without decorators.

Zend Form Decorators

The example below shows the use of decorators to layout a form in a table:

class Form_Search_Agent extends Zend_Form
{
	public function init()
	{
		$this->setAttrib('id', 'frmAgentSearch');
		$this->addElement('text', 'AgentLastName', array('label' => 'Last Name:', 'size' => '30', 'maxLength' => '50',
				'description' => '*',	
				'decorators' => array('viewHelper',
			        array('Description', array('tag' => 'span')), 
				array(
					array('data'=>'HtmlTag'), 
					array('tag' => 'td')
				),
                   		array('Label', array('tag' => 'th')),		
                   		array(
                   			array('row'=>'HtmlTag'),
                   			array('tag'=>'tr')
                   	        )
                   	)	
		    ));
 
		$this->addElement('text', 'AgentFirstName', array('label' => 'First Name:', 'size' => '30', 'maxLength' => '50',
				'description' => '*',
				'decorators' => array('viewHelper', array('Description', array('tag' => 'span')),
				array(array('data'=>'HtmlTag'), array('tag' => 'td')),
                                array('Label', array('tag' => 'th')),
                                array(array('row'=>'HtmlTag'),array('tag'=>'tr')))
		    ));
 
		$this->addElement('text', 'AgentID', array('label' => 'Agent ID:', 'size' => '15', 'maxLength' => '15',
				'decorators' => array('viewHelper', array(array('data'=>'HtmlTag'), array('tag' => 'td')),
                                array('Label', array('tag' => 'th')),
                                array(array('row'=>'HtmlTag'),array('tag'=>'tr')))
		    ));
 
		$this->addElement('text', 'AgentEmail', array('label' => 'Email Address:', 'size' => '20', 'maxLength' => '50',
				'description' => '@*.* ',
				'decorators' => array('viewHelper', array('Description', array('tag' => 'span')),
				array(array('data'=>'HtmlTag'), array('tag' => 'td')),
                                array('Label', array('tag' => 'th')))
		    ));
 
		$this->addElement('button', 'btnAgentSearch', array('label' => 'Search',
				decorators' => array('viewHelper', 
				array(
					array('data'=>'HtmlTag'), 
					array('tag' => 'td', 'colspan'=>'2','align'=>'left', 'class' => 'searchBtnTD')
				),
                   		array(
                   			array('row'=>'HtmlTag'),
                   			array('tag'=>'tr')
                   		)
                   	)
		));
		$this->addElement('hidden', 'id_integer', array(
					'decorators' => array('viewHelper', array(
					array('data'=>'HtmlTag'), 
					     array('tag' => 'td', 'colspan'=>'2')
					),
                   			array(
                   				array('row'=>'HtmlTag'),
                   				array('tag'=>'tr')
                   			)
                   		)
		));
		$this->setDecorators(array(]'FormElements',array(array('data'=>'HtmlTag'),array('tag'=>'table', 'class'=>'search')),
               'Form'
       ));
 
    }	
}

I instantiate the form in the controller method:

public function searchAction()
{
    $request = $this->getRequest();
    $data = array('AgentFirstName' => '', 'AgentLastName' => '', 'AgentID' => '', 'AgentEmail' => '');
    $agentId = '';
    if ($request->isGet()) {
        $data['AgentID'] = $request->getParam('id');
    }   
    $form = new Form_search_agent();
    $this->view->form = $form->populate($data);	
}

To display the form in a view, you simply echo the form:

echo $this->form

Zend Framework does all the work, so you don’t have to take the time to layout a form. But there may be situations where you want to layout out the form yourself.

Zend Form Without Decorators

To create a form without decorators, I create each element using the createElement method and then add the element to the form after I have removed all of the decorators using the removeDecorator method.

class Form_Search_Agent extends Zend_Form
{
     public function init()
     {
	$fld = $this->createElement('text', 'AgentLastName', array('size' => '30', 'maxLength' => '50');
	$fld->removeDecorator('DtDdWrapper');
	$fld->removeDecorator('HtmlTag');
	$fld->removeDecorator('Label');
	$this->addElement($fld);
 
	$fld = $this->createElement('text', 'AgentFirstName', array('size' => '30', 'maxLength' => '50');
	$fld->removeDecorator('DtDdWrapper');
	$fld->removeDecorator('HtmlTag');
	$fld->removeDecorator('Label');
	$this->addElement($fld);				
 
	$fld = $this->createElement('text', 'AgentID', array('size' => '15', 'maxLength' => '15');
	$fld->removeDecorator('DtDdWrapper');
	$fld->removeDecorator('HtmlTag');
	$fld->removeDecorator('Label');
	$this->addElement($fld);
 
	$fld = $this->createElement('text', 'AgentEmail', array('size' => '20', 'maxLength' => '50');
	$fld->removeDecorator('DtDdWrapper');
	$fld->removeDecorator('HtmlTag');
	$fld->removeDecorator('Label');
	$this->addElement($fld); 
 
	$fld = $this->createElement('button', 'btnAgentSearch', array('label' => 'Search');
	$fld->removeDecorator('DtDdWrapper');
	$fld->removeDecorator('HtmlTag');
	$fld->removeDecorator('Label');
	$this->addElement($fld);     
 
	$fld = $this->createElement('hidden', 'id_integer');
	$fld->removeDecorator('DtDdWrapper');
	$fld->removeDecorator('HtmlTag');
	$fld->removeDecorator('Label');
	$this->addElement($fld); 
 
	$this->setDecorators( array( array('ViewScript', array('viewScript' => 'agent/searchForm.phtml'))));   
 
     }	
}

Notice the last line of the form class:

$this->setDecorators( array( array('ViewScript', array('viewScript' => 'agent/searchForm.phtml'))));

This tells Zend where to find the layout file for the form.

searchForm.phtml

<form id="frmAgentSearch" action="/mris-customers/my-account/agent-sales-report/" method="post">
<label>First Name:</label>
<?php echo $this->element->AgentFirstName; ?>
<label>Last Name:</label>
<?php echo $this->element->AgentLastName; ?>
<label>Agent ID:</label>
<?php echo $this->AgentID; ?>
<label>Agent ID:</label>
<?php echo $this->AgentEmail; ?>
<?php echo $this->btnAgentSearch; ?>
<?php echo $this->id_integer; ?>
</form>

That’s it. The controller and view do not change. Although there is an extra step when using building Zend Framework forms without decorators, I prefer having the flexibility to layout a form the way that I want. Enjoy.

Be Sociable, Share!

Checkout My New Site - T-shirts For Geeks