Wrap a check mark on a label with ZF decorators

I ran into some problems with Zend Decorators (ZF1) by checking the checkbox in the label. In my form, I have something very simple:

$remember = new Zend_Form_Element_Checkbox('remember'); $remember ->setLabel('Remember me'); 

And in the class of my decorators:

 $checkboxDecorator = array( 'ViewHelper', 'Errors', 'Label', array('HtmlTag', array('tag' => 'div', 'class' => 'controls')), array('decorator' => array('Holder' => 'HtmlTag'), 'options' => array('tag' => 'div', 'class' => 'control-group')), ); $this->setDefaultElementsDecorators($this->_checkboxElement, $checkboxDecorator); 

The resulting source code is as follows:

 <div class="control-group"> <div class="controls"> <label for="remember" class="optional">Remember me</label> <input type="hidden" name="remember" value="0"> <input type="checkbox" name="remember" id="remember" value="1"> </div> </div> 

I want too:

 <div class="control-group"> <div class="controls"> <label for="remember" class="optional"> <input type="hidden" name="remember" value="0"> <input type="checkbox" name="remember" id="remember" value="1"> Remember me </label> </div> </div> 

All the workarounds that I tested failed, please help me: P

+4
source share
1 answer

The easiest option is to create a new custom decorator ...

 class App_Form_Decorator_MyLabel extends Zend_Form_Decorator_Abstract { public function render($content) { $el = $this->getElement(); $id = htmlentities($el->getId()); $label = htmlentities($el->getLabel()); return '<label for="' . $id . '">' . $content . ' ' . $label . '</label>'; } } 

Then your $checkboxDecorator will become:

 $checkboxDecorator = array( 'ViewHelper', 'MyLabel', // Your new class 'Errors', array('HtmlTag', array('tag' => 'div', 'class' => 'controls')), array( 'decorator' => array('Holder' => 'HtmlTag'), 'options' => array( 'tag' => 'div', 'class' => 'control-group' ) ) ); 
0
source

Source: https://habr.com/ru/post/1445255/


All Articles