Emagister/zend-form-decorators-bootstrap — pull request #21 by mrbubblesort (2012-04-18 02:38:55+00:00)
Title: Rearragned form classes to make them more easily extendable

This change should make it easier for anyone that wants to use Twitter_Bootstrap_Form_* in their existing project, but all of their forms already extend a master form that extends Zend_Form.

My problem:
In many of my other projects I have a master form that all other forms extend. This master form extends Zend_Form, and is used for logging and other housekeeping. I wanted to use all of the vertical, horizontal, etc. bootstrap forms in my project, but the problem is figuring out how to extend them. If I have all of my base forms extend the twitter forms, then I lose everything in my master form. I could make my master form extend the Horizontal form, but then I couldn't use Vertical. I could extend Twitter_Bootstrap_Form directly, but then I have to manually set the decorators on each child form, defeating the purpose of this project entirely.

My fix:
I removed the contructors from Horizontal, Vertical, and Inline, and moved their decorators to a protected array called $defaultElementDecorators. Then in the Twitter_Bootstrap_Form's constructor I set the decorators based on whatever the extending form's $_disposition is. So you can still extend any of the bootstrap forms as normal, or you can extend Twitter_Bootstrap_Form directly like so:

class My_Master_Form extends Twitter_Bootstrap_Form {
//set all forms to horizontal by default
protected $_disposition = Twitter_Bootstrap_Form::DISPOSITION_HORIZONTAL;
...
}

class My_Page_Form extends My_Master_Form {
//set this form to inline
protected $_disposition = Twitter_Bootstrap_Form::DISPOSITION_INLINE;
....
}