I am creating checkbox elements in Zend Form like this:
$element = $this->CreateElement('checkbox', 'CheckIt');
$element->setLabel('Check It');
$elements[] = $element;
When I get label like this:
echo $this->element->getElement("CheckIt")->getLabel();
It outputs:
Check It
But I want following output:
<label for='CheckIt'>Check It</label>
Is there any option in ge开发者_StackOverflowtLabel() function or is there any other function to achieve this.
Thanks
renderLabel()
<?php echo $this->form->getElement("CheckIt")->renderLabel(); ?>
render that label
i'm affraid that while decorators are boring to study and understand, once you understand them, they are usefull
this is one of my classes, you can easily addapt it to meet your needs
<?php
class My_Label extends Zend_Form_Decorator_Abstract
{
protected $_format = '<td class="nome_campo"><label for="%s">%s%s</label></td>';
public function render($content)
{
$element = $this->getElement();
$id = htmlentities($element->getId(), ENT_QUOTES, "UTF-8");
$label = htmlentities($element->getLabel(), ENT_QUOTES, "UTF-8");
if ($element->isRequired())
$asterisk = '<span class="required">*</span> ';
else
$asterisk = '';
$markup = sprintf($this->_format, $id, $asterisk, $label);
//per avere valid xhtml/html
if (stripos($element->getType(), 'radio') !== false )//Zend_Form_Element_Radio
{
$this->_format = '<td class="nome_campo">%s%s</td>';
$markup = sprintf($this->_format, $asterisk, $label);
}
$placement = $this->getPlacement();
$separator = $this->getSeparator();
switch ($placement) {
case self::APPEND:
return $content . $separator . $markup;
case self::PREPEND:
default:
return $markup . $separator . $content;
}
}
}
There is a conceptual misunderstanding in your question: getLabel()
is a simple getter method for a property on an object.
$element; // is a Zend_Element object
$label = $element->getLabel(); // returns the value of the label property and not a HTML string
If you want an HTML output you have to call render()
on the object but this will render the whole form and not just the label value. You may disable the decorators for the element but then you will have enable them again when you render the form itself.
Plus there is a formal error in your question. It is either
$element->getLabel();
// or once you have added the element to the form
$this->getElement('CheckIt')->getLabel();
精彩评论