views:

3365

answers:

3

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!

+5  A: 

I think you're looking for full control of the decorator via a View Script:

Zend Framework Manual

Basically, you want to set the viewScript property of the element to the path to your script, and then pass any additional information you'd want to send, perhaps the linkHref or title of the link you're building.

$name = new Zend_Form_Element_Text( 'name' );
$name->setLabel( 'Name' );   
$name->viewScript = 'path/to/viewScript.phtml';
$name->decorators = array('ViewScript', array('linkHref' => '#',
                                              'linkTitle' => 'My Link');
$this->addElements( $name );
$this->addDisplayGroup( array( 'name' ), 'people');

Then your viewScript would look like this, I'm not sure how all the Helpers are within an instance of a Zend_Form_Element, but its kinda like this:

<dt><?= $this->formLabel($this->element->getName(),
                     $this->element->getLabel()) ?></dt>
<dd><?= $this->{$this->element->helper}(
                     $this->element->getName(),
                     $this->element->getValue(),
                     $this->element->getAttribs()
                ) ?>
<a href="<?= $this->linkHref; ?>"><?= $this->linkTitle; ?></a>
<?= $this->formErrors($this->element->getMessages()) ?>

</dd>

Sometimes, doing it in a viewScript it better because it gives you 100% control of the element, while still being pretty DRY.

Aaron
Thank you - this will work perfectly in the most complicated forms
+8  A: 

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.

monzee
Looks like I was close. This works perfectly. Thank you!
A: 

Just to add some support to Aaron's post, after hours of head-bashing to do something REALLY simple (this seems to negate the point of having a framework), if you find that this does not work on your checkboxes then that is because your must use this in your .phtml (based on Aaron's example):

<dt><?= $this->formLabel($this->element->getName(),
                     $this->element->getLabel()) ?></dt>
<dd><?= $this->{$this->element->helper}(
                     $this->element->getName(),
                     $this->element->getValue(),
                     $this->element->getAttribs(),
                     $this->element->options
                ) ?>
<a href="<?= $this->linkHref; ?>"><?= $this->linkTitle; ?></a>
<?= $this->formErrors($this->element->getMessages()) ?>

</dd>

OPTIONS! OPTIONS! OPTIONS! Where were you when I needed to find you?! So do not forget to add the following to your helper if you are using checkboxes:

$this->element->options

This works well for adding a Terms and Conditions checkbox with Zend_Form_Element_Checkbox (that's for the SEO)

wiseguydigital