Hi,

I'm trying to achieve what I thought was a simple task, but turns out to be rather complex... I just want to double check that it's not a totally stupid approach.

So I have a form, and I want to ensure that required elements are rendered with a nice background colour to indicate this to the user.

Now it seems that the Label decorator will helpfully put a class into the containing dt element (by default it's a dt... I know you can change this). That's great, but it's not the containing element for the actual field itself, so it's not overly useful for the stylistic result I want to achieve.

So the approach I've taken is to create my own decorator, call "RequiredClass", all it does is add either a "required" or "optional" class to the element. In order for this to work, it has to be processed before ViewHelper decorator.


As the forms may be reused in a different context, I do not want to apply the decorators at a base class level. I therefore created an action helper called DecorateForm. This means I can do something like:

function fooAction()
{
  $this->view->form = $this->_helper->decorateForm(new My_Form());
}


In order to ensure that each element in the form has the RequiredClass decorator I do the following in my DecorateForm action helper:

foreach ($form->getElements() as $el)
{
  $decorators = $el->getDecorators();
  array_unshift($decorators, new My_Form_Decorator_RequiredClass());
  $el->setDecorators($decorators);
}


While this is all abstracted etc. and doesn't really get in the way, it seems like a bit of a long road for a shortcut! Is there a better way to achieve this end result?

e.g. Would it be better to instead change the HtmlTag decorator to add the class to the containing element? In the default case, this would make the dt and the dd consistent in terms of the class attached to them.

If this is desirable and acceptable, I'd happily make that change and submit the patch.

Cheers for any insights.

Col


--

Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/

Day Job:
  Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
  Mandriva Linux Contributor [http://www.mandriva.com/]
  PulseAudio Hacker [http://www.pulseaudio.org/]
  Trac Hacker [http://trac.edgewall.org/]

Reply via email to