Zend_Form; easy ways to customise the output

January 22nd, 2010 by Colin Seaman

I’ve been following a thread on LinkedIn recently regarding people’s concerns about how to “easily” get the output from Zend Form to look how they want on the screen.  There are numerous articles on this, especially those involving how to customise the decorators which Zend Form uses or writing plugins which take care of the whole lot.

I guess some people’s concerns are that they either can’t follow these articles (some of them do make some huge presumptions!) or they don’t want to have to write a plugin (for example) just for one custom render of a form.

The easiest way to achieve a half way house is to use the power of Zend Form and all it’s validators and such, but to control the form element layouts in your phtml files.  I’ve placed some sample code below which I hope helps people in this situation:

IndexController.php

public function blahAction()

{

$form = new forms_myform();

if ($this->getRequest()->isPost())
  {
   $postdata = $this->_request->getPost();

if (!$form->isValid())

{

$form->populate($postdata); // form is invalid, send the data back to the form

} else { // the form has validated successfully, now do something with it

$filtered_data = $form->getValues();

// Do something with the output

}

}

$this->view->form = $form; // assign the form object for use in out view script

blah.phtml (view script)

<p><?php echo $this->form->username ?></p>

<p><?php echo $this->form->email?></p>

<p><?php echo $this->form->submit?></p>

myform.php

<?php

class forms_myform extends Zend_Form
{
    public function __construct()
    {
  $translate = Zend_Registry::get(‘Zend_Translate’);
             $this->setDisableLoadDefaultDecorators(false);

  // Create the non empty validator
        $validatorUsername = new Zend_Validate_NotEmpty();
        $validatorUsername->setMessage(‘Username cannot be empty’);

      
        $username = new Zend_Form_Element_Text(‘username’);
        $username->setLabel($translate->_(‘Username’))
           ->setRequired(true)
           ->setDecorators(array(‘ViewHelper’,
                         ‘Description’,
                         ‘Errors’,
                         array(‘HtmlTag’, array(‘tag’ => ’span’)),
                         array(‘Label’, array(‘class’ => ”),)))
                 ->addValidator($validatorUsername);

$email= new Zend_Form_Element_Text(‘email’);
        $email->setLabel($translate->_(‘Email’))
           ->setRequired(true)
           ->setDecorators(array(‘ViewHelper’,
                         ‘Description’,
                         ‘Errors’,
                         array(‘HtmlTag’, array(‘tag’ => ’span’)),
                         array(‘Label’, array(‘class’ => ”),)));

$submit = new Zend_Form_Element_Submit(’submit’);
        $submit->setLabel($translate->_(‘Submit’))
          ->setValue(‘1′)
          ->setDecorators(array(‘ViewHelper’,
                         ‘Description’,
                         ‘Errors’,
                         array(‘HtmlTag’, array(‘tag’ => ’span’)),
                         array(‘Label’, array(‘class’ => ‘nolabel’),)))
          ->setAttrib(‘class’,'loginbutton’);
        $this->addElements(array($username, $email, $submit));

    }
}

//  You’ll notice that we set the decorators in the form so that we don’t get all the junk DD, DT etc tags that by standard Zend Form produces.

->setDecorators(array(‘ViewHelper’,
                         ‘Description’,
                         ‘Errors’,
                         array(‘HtmlTag’, array(‘tag’ => ’span’)),
                         array(‘Label’, array(‘class’ => ”),)))

By wrapping the HTML tag in a span, it means that we can control it easily via CSS.  By default it doesn’t display any differently if you have no specific span style defined.

Leave a Reply

Categories

Contact Us

info@colinseaman.com

Telephone: 07940 423446

Colin Seaman Consultancy Ltd. Registered in England and Wales. Company No. 07040812. VAT 980212830
Registered Office: Suite 1, Dubarry House, Hove Park Villas, Hove, East Sussex, BN3 6HP, United Kingdom