php - How To Remove All DtDdWrappers and Labels on Zend Form Elements -
i know can remove stuff each element individually so
$button ->removedecorator('dtddwrapper') ->removedecorator('htmltag') ->removedecorator('label');
i wondering if can achieve same elements in zend form?
, how 1 remove dl wrapping form?
markus, here solution use seems work well, suitable you.
first, in order render form no <dl>
tag, need set decorators on form object itself. inside class extending zend_form, call zend_form->setdecorators()
passing array of form decorators.
from reference guide:
the default decorators zend_form formelements, htmltag (wraps in definition list), , form; equivalent code creating them follows:
$form->setdecorators(array( 'formelements', array('htmltag', array('tag' => 'dl')), 'form' ));
to wrap form in other dl, use above decorators change dl whatever tag use, typically use div
of class form
see later.
next, elements need dealt with. zend_form elements have different decorators different types of elements. following groups of element types each have own distinct set of decorators: [submit & button], [captcha], [file], [image], , [radio*]. decorator radio similar standard elements except not specify for
attribute inside label.
all other form elements, text, password, select, checkbox, etc use same set of default decorators.
to remove dd/dt tags individual form element need apply our own set of decorators it. here example not use dd/dt tags:
array( 'viewhelper', 'errors', array('description', array('tag' => 'p', 'class' => 'description')), array('htmltag', array('class' => 'form-div')), array('label', array('class' => 'form-label')) );
this wrap each form element in div tag class form-div
. problem is, have apply set of decorators every element don't want wrapped in dd/dt tags can bit problematic.
to solve issue, create class extends zend_form , give default behavior , decorators different default decorators zend_form.
while can't quite have zend_form automatically assign correct decorators specific element types (you can assign them specific element names), can set default, , give ourselves easy access decorators 1 place, if need change, can changed forms.
here base class:
<?php class application_form_base extends zend_form { /** @var array decorators use standard form elements */ // these applied our text, password, select, checkbox , radio elements default public $elementdecorators = array( 'viewhelper', 'errors', array('description', array('tag' => 'p', 'class' => 'description')), array('htmltag', array('class' => 'form-div')), array('label', array('class' => 'form-label', 'requiredsuffix' => '*')) ); /** @var array decorators file input elements */ // these used file elements public $filedecorators = array( 'file', 'errors', array('description', array('tag' => 'p', 'class' => 'description')), array('htmltag', array('class' => 'form-div')), array('label', array('class' => 'form-label', 'requiredsuffix' => '*')) ); /** @var array decorator use standard elements except not wrap in htmltag */ // array gets set in constructor // can used if not want element wrapped in div tag @ public $elementdecoratorsnotag = array(); /** @var array decorators button , submit elements */ // decorators used submit , button elements public $buttondecorators = array( 'viewhelper', array('htmltag', array('tag' => 'div', 'class' => 'form-button')) ); public function __construct() { // first set $elementdecoratorsnotag decorator, copy of our regular element decorators, not wrapped in div tag foreach($this->elementdecorators $decorator) { if (is_array($decorator) && $decorator[0] == 'htmltag') { continue; // skip copying value decorator } $this->elementdecoratorsnotag[] = $decorator; } // set decorator form itself, wraps <form> elements in div tag instead of dl tag $this->setdecorators(array( 'formelements', array('htmltag', array('tag' => 'div', 'class' => 'form')), 'form')); // set default decorators our element decorators, elements added form // use these decorators $this->setelementdecorators($this->elementdecorators); parent::__construct(); // parent::__construct must called last because calls $form->init() // , after not executed } } /* zend_form_element default decorators: $this->adddecorator('viewhelper') ->adddecorator('errors') ->adddecorator('description', array('tag' => 'p', 'class' => 'description')) ->adddecorator('htmltag', array('tag' => 'dd', 'id' => array('callback' => $getid))) ->adddecorator('label', array('tag' => 'dt')); */
now use class, extend of forms base class , go assigning elements usual. if use zend_form_element_xxx
opposed addelement()
need pass 1 of decorators option element constructor, if use zend_form->addelement, use default decorator $elementdecorators
assigned in class.
here example shows how extend class:
<?php class application_form_test extends application_form_base { public function init() { // add text element, automatically use application_form_base->elementdecorators decorators $this->addelement('text', 'username', array( 'label' => 'user name:', 'required' => false, 'filters' => array('stringtrim'), )); // not use correct decorators unless specify them directly $text2 = new zend_form_element_text( 'text2', array( 'decorators' => $this->elementdecorators, // must give right decorator 'label' => 'text 2' ) ); $this->addelement($text2); // add element, uses $elementdecorators $this->addelement('text', 'email', array( 'label' => 'email:', 'required' => false, 'filters' => array('stringtrim', 'stringtolower'), )); // add submit button, don't want use $elementdecorators, pass button decorators instead $this->addelement('submit', 'submit', array( 'label' => 'continue', 'decorators' => $this->buttondecorators // specify button decorators )); } }
this shows pretty effective way rid of dd/dt , dl elements , replace them own. bit inconvenient have specify decorators every element, opposed being able assign decorators specific elements, seems work well.
to add 1 more solution think looking do, if render element no label, create new decorator , omit label decorator this:
$elementdecorators = array( 'viewhelper', 'errors', array('description', array('tag' => 'p', 'class' => 'description')), array('htmltag', array('class' => 'form-div')), // array('label', array('class' => 'form-label', 'requiredsuffix' => '*')) // comment out or remove label decorator element in question // can same of decorators if don't want them rendered );
feel free ask clarification on anything, out.
Comments
Post a Comment