TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Help with decorator setup using Zend_Form (http://www.talkphp.com/advanced-php-programming/4854-help-decorator-setup-using-zend_form.html)

flyingbuddha 08-16-2009 01:10 PM

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!

flyingbuddha 08-21-2009 09:59 AM

Alan? Anyone?
To expand a little to make a little clearer:

HTML Code:

<div class="head"></div>
and

HTML Code:

<div class="foot">
    <input type="submit" value="Submit" class="button" />
</div>


flyingbuddha 08-21-2009 02:15 PM

I thought I'd update this thread myself. Decided to change approach, I've spent hours working out how to do what I want, and mananged to achieve something close but not exact. I'm really unimpressed with how much work needs to be completed to create a simple form like the one above.

For people wishing to achieve what my original goal was, here are a couple of useful links I found:

http://devzone.zend.com/article/3450
See: "Example: Full Customization Using the ViewScript Decorator"

http://www.nabble.com/Zend_Form_Deco...d19638477.html
Using $this->element->fieldId in your templates (not $this->form!)

http://www.vayanis.com/2008/03/17/us...h-zend_config/
Using Zend_Form and Zend_Config together to produce ini-driven forms


All times are GMT. The time now is 10:39 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0