php - Zend Framework: Setting decorators and labels - should this be done in the view or the form class? -
i notice many (most?) people when working zend framework add decorators , labels in form class itself.
class user_form_add extends zend_form { public function init() { parent::init(); $username = new zend_form_element_text('username'); $username->setlabel('username:') ->setrequired(true) ->addfilter('stringtrim') ->addvalidator('stringlength', $breakchainonfailure = false, $options = array(1, 30)) ->setdecorators(array( 'viewhelper', array('description', array('tag' => 'p', 'class' => 'description')), array('label', array('requiredprefix' => '<span class="asterisk">*</span> ', 'escape' => false)), array('htmltag', array('tag' => 'p', 'class' => 'element')) )); } }
but surely not practice? have thought decorators , labels part of view layer in mvc application. when @ form class, looks "poluted" sorts of markup, tags , text should in view layer.
this approach means if need fiddle markup of form, need work both form class and viewscript.
i don't concept, , have been separating forms , decorators actual view scripts when rendering forms. want keep these conflicting "concerns" of application separate.
class user_form_add extends zend_form { public function init() { parent::init(); $username = new zend_form_element_text('username'); $username->setrequired(true) ->addfilter('stringtrim') ->addvalidator('stringlength', $breakchainonfailure = false, $options = array(1, 30)); } }
//add.phtml:
$this->form->username->setlabel('username:'); $this->form->username->setdecorators(array( 'viewhelper', array('description', array('tag' => 'p', 'class' => 'description')), array('label', array('requiredprefix' => '<span class="asterisk">*</span> ', 'escape' => false)), array('htmltag', array('tag' => 'p', 'class' => 'element')) )); echo $this->form->render();
this leaves form class clean, , quite analogous model class - that's how percieve form class be; contains filters, validators etc, business-logic related.
if follow approach, makes easier integrate forms models, such can reuse/access form validators , filters directly within models - without overhead of having created decorators , whatnot unnessesarily.
http://weierophinney.net/matthew/archives/200-using-zend_form-in-your-models.html
as far keeping view scripts dry, such you're not repeating same labels , decorators in multiple views (i.e. when need render same form multiple times, in different view scripts), find can separate re-usable parts of form out using viewscript decorator keep things dry.
edit: well, can override default decorators ones appropriate our project avoid having unnessesarily declare decorators in first place.
so actual question this:
why isn't else working forms this? drawbacks see approach?
why should decorators , form labels created in form class, if can add them in view layer?
i don't why every usage of zend_form
see includes adding decorators/labels in form class itself.
why isn't else working forms this?
i never thought of. approach.
Comments
Post a Comment