View Single Post
Old 08-16-2009, 01:10 PM   #1 (permalink)
flyingbuddha
The Contributor
 
flyingbuddha's Avatar
 
Join Date: Jan 2008
Location: Birmingham, UK
Posts: 60
Thanks: 10
flyingbuddha is on a distinguished road
Default Help with decorator setup using Zend_Form

Hi all,

I'm trying to get my head around the Zend framework. One part I'm really getting stuck on is using the decorators with Zend_Form. I'm trying to add a couple of <div>'s before and after the main area but don't know how to - and the documentation is so hard to understand unless you know what you're looking for.

Here's what I'm trying to achieve:
HTML Code:
<form method="post" action="." enctype="multipart/form-data" accept-charset="utf8">
    <div class="head"></div>
    <div class="main">
        
        <div class="row cf">
            <label for="first_name">First Name:</label>
            <div class="w"><input type="text" name="first_name" id="first_name" title="First Name"  value="" class="text" /></div>
        </div>
        <div class="row cf">
            <label for="last_name">Last Name:</label>
            <div class="w"><input type="text" name="last_name" id="last_name" title="Last Name" value="" class="text" /></div>
        </div>
        <div class="row cf">
            <label for="email">Email:</label>
            <div class="w"><input type="text" name="email" id="email" title="Email" value="" class="text" /></div>
        </div>
        <div class="row cf">
            <label for="address">Address:</label>
            <div class="w"><textarea name="address" id="address" title="Address:" rows="5" cols="75"></textarea></div>
        </div>
        <div class="row cf">
            <label for="password">Password:</label>
            <div class="w"><input type="text" name="password" id="password" value="" class="text" autocomplete="off" /></div>
        </div>
        <div class="row cf">
            <label for="password_2">Confirm Password:</label>
            <div class="w"><input type="text" name="password_2" id="password_2" value="" class="text" /></div>
        </div>
    </div>
    <div class="foot">
        <input type="submit" value="Submit" class="button" />
    </div>
</form>
And this is what I have so far:
PHP Code:
<?php

    
class Default_Form_Register extends Zend_Form{
        
        public function 
init(){
            
            
// we don't want the default decorators
            
$this->setDisableLoadDefaultDecorators(true);
            
            
// set the form method
            
$this->setMethod('post');
            
            
// set the form decorator
            
$this->addDecorator('FormElements')
                ->
addDecorator('HtmlTag', array('tag' => 'div''class' => 'main'))
                ->
addDecorator('Form');

            
// set the element decorators
            
$this->setElementDecorators(array(
                
'ViewHelper',
                
'Label',
                
'Errors',
                new 
Zend_Form_Decorator_HtmlTag(array(
                    
'tag'   => 'div'
                    
'class' => 'row cf'
                
))
            ));
            
            
$this->addElement('text''first_name', array(
                
'label'         => 'First Name:',
                
'required'      => true,
                
'validators'    => array('NotEmpty')
            ));
            
$this->addElement('text''last_name', array(
                
'label'         => 'Last Name:',
                
'required'      => true,
                
'validators'    => array('NotEmpty')
            ));
            
$this->addElement('text''email', array(
                
'label'         => 'Email:',
                
'required'      => true,
                
'filters'       => array('StringTrim'),
                
'validators'    => array('EmailAddress')
            ));
            
$this->addElement('textarea''address', array(
                
'label'         => 'Address:',
                
'required'      => true,
                
'validators'    => array('NotEmpty')
            ));
            
$this->addElement('password''password', array(
                
'label'         => 'Password:',
                
'required'      => true,
                
'validators'    => array('NotEmpty')
            ));
            
// add CSRF protection
            
$this->addElement('hash''csrf', array(
                
'ignore' => true,
            ));
            
            
// Add the submit button
            
$this->addElement('submit''submit', array(
                
'ignore'        => true,
                
'label'         => 'Register',
            ));
            
            
// remove label from submit button
            
$this->submit->removeDecorator('Label');
        }
    }
Can someone whose a dab hand at using Zend help me fill in the blanks so that my html looks like what I'm after.

Cheers!
__________________
Pro. Geek
http://www.mikeholloway.co.uk
flyingbuddha is offline  
Reply With Quote