Zend Form: Add a link to the right of a text field

 picture · Mar 13, 2009 · Viewed 17.2k times · Source

I realize that I should be able to do this, but what can I say, I don't get it. I've even rtfm'ed until my eyes fried. I learn best by examples, not the deep explanations that Zend's docs give, or the typical "use decorators" response that this type of questions usually produce. What I need is markup like this:

<dt>
    <label for="name">Name</label>
</dt>
<dd>
    <input type="text" name="name" id="name" value="">
    <a href="#">My Link</a>
</dd>

Its all vanilla, except the extra LINK after the input. Yes, its inside the dd, right next to the link, and that's what I can't get to happen.

Here is the (slightly modified) code that I used to create the above HTML

$name = new Zend_Form_Element_Text( 'name' );
$name->setLabel( 'Name' );        
$this->addElements( $name );
$this->addDisplayGroup( array( 'name' ), 'people');

Any example code or better explanation would make this noob very very happy.

Cheers!

Answer

monzee picture monzee · Mar 14, 2009

See my reply to this thread in the mailing list and my blog post about this. It's basically the same process Aaron described.

You can also go the decorator way, using the description property to hold the link (not tested):

<?php
$foo = new Zend_Form_Element_Text('name');
$foo->setLabel('Name')
    ->setDescription('<a href="#">Link</a>')
    ->setDecorators(array(
        'ViewHelper',
        array('Description', array('escape' => false, 'tag' => false)),
        array('HtmlTag', array('tag' => 'dd')),
        array('Label', array('tag' => 'dt')),
        'Errors',
      ));
$form->addElement($foo);

I'm not sure if the 'tag'=>false in the Description decorator would work, but it's worth a shot. Sorry I can't test this now, my development box is broken at the moment. If it fails, try the manual decorator rendering method described in those two links.