Question

I am trying to use a zend decorator to use custom containers and add css classes on my elements.

$form->setElementDecorators(array(
    'viewHelper',
    'Errors',
    array('Label'),
    array(
        array('row'=>'HtmlTag'),
        array('tag'=>'div', 'class'=>'col-md-6')
    )
));
$form->setDecorators(array(
    'FormElements',
    array(
        array('data'=>'HtmlTag'),
        array('tag'=>'div', 'class'=>'row')
    ),
    'Form'
));

Is there a way to add a css class directly on my inputs? <input class="form-control">

Is there a way to encapsulate label and input in 2 divs?

Actually i have is

<div class="col-md-6">
    <label></label>
    <input>
</div>

and what i wish is

<div class="col-md-6">
    <div class="form-group">
        <label></label>
        <input class="form-control">
    </div>
</div>

Also where can i found documentation about array to pass to setElementDecorators() function?

Thanks

Was it helpful?

Solution

Try to add HtmlTag decorator like this:

$form->setElementDecorators(array(
    'viewHelper',
    'Errors',
    array('Label'),
    array(
        array('row'=>'HtmlTag'),
        array('tag'=>'div', 'class'=>'form-group'),        
    ),
    array('HtmlTag', array('tag'=>'div', 'class'=>'col-md-6')),
));

For all elements, you can add a class like this:

Example to set form-control class:

foreach($form->getElements() as $element){
    $element->setAttrib('class', 'form-control');
}

Example to add form-control class:

foreach($form->getElements() as $element){
    $element->setAttrib('class', 'form-control' . ($element->getAttrib('class') == '' ? '' :  ' ' . $element->getAttrib('class')));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top