Search code examples
phpzend-frameworkzend-decorators

Decorators Zend Framework PHP


I have a label and i want to change it's color: so i'm doing this: i have my form class in zend:

$this->addElement('text','name',array('label' => 'Name Event'));
$this->getElement("name")
        ->addDecorator("EditLabel");

and here is my decorator:

class OrarLib_Form_Decorator_EditLabel extends Zend_Form_Decorator_Abstract
{

protected $html = '<div class = "label"> %s </div>';
public function init()
{
    parent::init();
}

public function render($content)
{
    $label = $this->getElement()->getLabel();
    return sprintf($this->html, $label, $content);
}
}

And the problem is that my textbox disappear when i use this decorator. I'm using Zend framework. I'm not so good, what i'm doing wrong ?


Solution

  • I think you forgot to concate $content like this:

    class OrarLib_Form_Decorator_EditLabel extends Zend_Form_Decorator_Abstract
    {
    
        protected $html = '<div class = "label"> %s </div>';
        public function init()
        {
            parent::init();
        }
    
        public function render($content)
        {
            $label = $this->getElement()->getLabel();
            $markup = sprintf($this->html, $label);
    
            $placement = $this->getPlacement();
            $separator = $this->getSeparator();
    
            switch ($placement) {
                case self::PREPEND:
                    return $markup . $separator . $content;
                case self::APPEND:
                default:
                    return $content . $separator . $markup;
            }
        }
    }
    

    Now try this:

    $this->getElement("name")->addDecorator(array('EditLabel', array('placement' => 'append'));
    

    If you want to know more check out this:

    Layering Decorators