views:

779

answers:

4

I would like to wrap form elements with labels as such

<label for="email">E-mail <input type="text" name="email" id="email" /></label>

The only option I could find is to alter the placement; however it only accepts 'prepend' and 'append':

<!-- prepend -->
<label for="email">E-mail</label> <input type="text" name="email" id="email" />
<!-- append -->
<input type="text" name="email" id="email" /> <label for="email">E-mail</label>

That is not what I am after. I could alter the Zend_Form_Decorator_Label class, but that is the last alternative.

+3  A: 

This doesn't seem to be possible unfortunately, you'll probably have to write a custom decorator. It should be pretty simple, though.

Please note im just going to write this here, it's untested but based off another decorator so it should work with little/no modification

class Your_Form_Decorator_Date extends Zend_Form_Decorator_Abstract {
    public function render($content) {
       $element = $this->getElement();

       $name = $element->getFullyQualifiedName();
       return '<label for="'.$name.'">'. $element->getLabel() . ' '.$content.'</label>';

    }
}

Now if you add the correct prefix to your form.

$this->addPrefixPath('Your_Form', 'Your/Form/');

You should be able to use this to wrap your input (FormElement decorator) in a label tag.

Sorry I haven't had a chance to actually test this but given how my other decorators work. It should be fine.

EDIT: Thanks for pointing out that the label text wasn't being rendered gnarf. This is now fixed.

linead
You forgot to render the label text for $element in the "return"
gnarf
A: 

I've always found the decorators to be waaaay over-engineered for any given situation and it sort of defeats the purpose of separating the presentation layer from the business logic. You could use Zend's partial helper or create your own widgets using Zend_Form or a combination of the two, which would give you much more control over the presentation.

On some XHTML reference sites, they don't list input elements as valid child elements for labels, although text and any inline element should be valid. This could be another reason it's not allowed.

Tres
A: 

I guess you shouldn't use that ;) It's because label's contents arethe description of the field. It's nonsense to include the element into description. If you need, you can wrap both into a div or something like that.

Only reason for this are badly written CSSs :)

Tomáš Fejfar
A: 

I am assuming you want the label to line up right next to the element.

Example

$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email: ');
$form = new Zend_Form();
$form->addElement($email);

Now, that will render the form as such

<label>Email:</label>
      <input type="text"/>

In order to have the text box right next to the label, you need to render the 'label' decorator separately. To do this:

I created a form in my controller, assigned it to a variable in the view:

//in controller
$this->view->form = $form;

//in view script
$form = $this->form;

//('tag', null) tells the renderer not to wrap the label in the usual <dt> tag,  
//but not to use anything at all
$form->getElement('email')->getDecorator('label')->setOption('tag', null);

and then in your view script...

<?php print($form->email) ?>

This will render the Text element next to its own label