Search code examples
phpformszend-framework2formcollection

zf2 Zend 2 formCollection / FormElement Helper class


Goodmorning to all,

i'm having some trouble with ZF2, i'm new to this Framework, so have mercy.... XDD well i wanted to use the function formCollection() to generate the form i customized the form collection class to add a wrapper ul and it's ok, now my problem is that if i customize formelement telling him to wrap the element inside a li, now the problem is that the label remain outside of the li tag, is there any way to solve it? without using formRow() or directly writing the html?

FormCollection.php

namespace Users\View\Helper;

use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\FormCollection as BaseFormCollection;

class FormCollection extends BaseFormCollection {
    public function render(ElementInterface $element) {
        return '<ul>'. parent::render($element).'</ul>;
    }

}

FormElement.php

namespace Users\View\Helper;

use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\FormElement as BaseFormElement;

class FormElement extends BaseFormElement {
    public function render(ElementInterface $element) {
       return  '<li>'. parent::render($element).'</li>';
    }
}

Resulted HTML

<form name="Register" method="post" action="/">
<ul>
    <label for="name">Full Name</label>
    <li>
        <input type="text" value="" name="name">
    </li>
    <label for="password">Password</label>
    <li>
         <input type="password" value="" required="required" name="password">
    </li>
</ul>

it's literally driving me nuts and probably is an easy thing to fix T_T

Thanks.


Solution

  • For <li><label></label><input></li> you must create new helper FormLabel.php:

    namespace Users\View\Helper;
    
    use Zend\Form\View\Helper\FormLabel as BaseFormLabel;
    
    class FormLabel extends BaseFormLabel{
    
        public function openTag($attributesOrElement = null){
            return '<li>'.parent::openTag($attributesOrElement); 
        }
    }
    

    and update FormElement.php for сorrectly wrap submit:

    namespace Users\View\Helper;
    
    use Zend\Form\ElementInterface;
    use Zend\Form\View\Helper\FormElement as BaseFormElement;
    
    class FormElement extends BaseFormElement{
    
        public function render(ElementInterface $element){
            if($element->getAttribute('type') == 'submit'){
                return '<li>'.parent::render($element).'</li>';
            }else{
                return parent::render($element).'</li>';
            }
        }
    } 
    

    For <li><label></label></li><li><input></li> you need just create FormLabel.php:

    namespace Users\View\Helper;
    
    use Zend\Form\View\Helper\FormLabel as BaseFormLabel;
    
    class FormLabel extends BaseFormLabel{
    
        public function openTag($attributesOrElement = null){
            return '<li>'.parent::openTag($attributesOrElement); 
        }
    
        public function closeTag(){
            return '</label></li>';    
       }
    }
    

    whithout any updates in yours FormElement.php

    somethink like this... =) Hope it's help.