views:

367

answers:

1

I'm in the process of switching my forms to use a View Script as their decorators. The examples I've seen so far do the following in the View Script:

<td><label for='textEmail'>Email:</label></td>
<td><?php echo $this->element->textEmail; ?></td>

I would like to find a way to have the text to be displayed in the label from the Form Object as well.

class RegisterForm extends Zend_Form {
public function init () {
 $this->setAction('')
  ->setMethod('post')
  ->setAttrib('id','formRegister');

 $this->addElement('text', 'textEmail', array('label' => 'Email: '));
 $oEmail = $this->getElement('textEmail')
  ->setRequired(true)
  ->addFilter('StringTrim')
  ->addValidator('EmailAddress');
 $oEmail->setDecorators(array('ViewHelper', 'Errors'));

 $this->setDecorators(array(array('ViewScript', array('viewScript' => 'forms/RegisterForm.phtml'))));
 }
}

Above is how my Form Object is defined. Is anyone aware of how to access the defined label value? In the following format perhaps?

<?php echo $this->element->textEmail->label; ?>

Naturally that doesn't work. :p Thanks~

+2  A: 

$this->element->getLabel()

Here is my view script for standard fields:

<div class="field<?php if($this->element->hasErrors()): ?> errors<?php endif; ?>" id="field_<?php echo $this->element->getId(); ?>">
    <?php if (0 < strlen($this->element->getLabel())) : ?>
        <?php echo $this->formLabel($this->element->getName(), $this->element->getLabel());?>
    <?php endif; ?>
    <span class="value"><?php echo $this->{$this->element->helper}(
        $this->element->getName(),
        $this->element->getValue(),
        $this->element->getAttribs()
    ) ?></span>
    <?php if ($this->element->hasErrors()) : ?>
        <?php echo $this->formErrors($this->element->getMessages()); ?>
    <?php endif; ?>
    <?php if (0 < strlen($this->element->getDescription())) : ?>
        <span class="hint"><?php echo $this->element->getDescription(); ?></span>
    <?php endif; ?>
</div>
Sonny