views:

685

answers:

3

I assume it's a common requirement to have forms in your web apps that have Edit Delete buttons under them. But ZF puts one button under another, which is counter-intuitive. I guess ViewScript decorator could help me completely override button html.

But how to do it across other forms, to avoid duplicating? May be I am overcomplicating and I just should somehow paste html code instead of button element objects?

+1  A: 

Have a read through this tutorial on Zend Developer Zone:

http://devzone.zend.com/article/3450-Decorators-with-Zend_Form

karim79
A: 

Button decorators can be modified in Form's constructor. Buttons should be left without HtmlTag decorator to disable being on separate lines due to dt/dd tags, HtmlTag decorator can be remove like this:

$buttonobject->setDecorators(array( 'ViewHelper', //array('HtmlTag', array('tag' => 'dd')), //array('Label', array('tag' => 'dt')), ));

Comments are for demonstration purposes only. Additionally, buttons may be grouped into a fieldset, for styling purposes:

$this->addDisplayGroup(array('delete','submit'),'buttons');

Optional site.css code:

fieldset-buttons { border: none; }

PHP thinker
A: 

This is the code I use in my own Form class that all my forms inherit from. The main trick is to only use the ViewHelper Decorator on the button itself, and stick the buttons in a displaygroup that uses a DtDdWrapper and wraps the buttons in a <div class='buttons'> for extra styling options

  protected $_buttons = array();

  /**
   * Sets a list of buttons - Buttons will be standard submits, or in the getJson() version
   * they are removed from display - but stuck in the json in the .buttons property
   *
   * $buttons = array('save'=>'Save This Thing', 'cancel'=>'Cancel') as an example
   *
   * @param array $buttons 
   * @return void
   * @author Corey Frang
   */
  public function setButtons($buttons)
  {
    $this->_buttons = $buttons;
    foreach ($buttons as $name => $label)
    {
      $this->addElement('submit', $name, array(
          'label'=>$label,
          'class'=>$name,
          'decorators'=>array('ViewHelper'),
        ));
    }
    $this->addDisplayGroup(array_keys($this->_buttons),'buttons', array(
      'decorators'=>array(
        'FormElements',
        array('HtmlTag', array('tag'=>'div', 'class'=>'buttons')),
        'DtDdWrapper'
      )
    ));
  }

  // Example from form::init()
  $this->setButtons(array('save'=>'Save Entry', 'delete'=>'Delete Entry'));
gnarf