Question

I want the following html with two radio button groups in zend form

        <label class="inline">
        <input type="radio" name="form-field-radio">
    <span class="lbl"> Male</span>
    </label>
        &nbsp; &nbsp; &nbsp;
   <label class="inline">
    <input type="radio" name="form-field-radio">
    <span class="lbl"> Female</span>
   </label>

and i'm using the following code to make this button in Zend Form

$gender = $this->CreateElement('radio','gender')
        ->addFilter(new Zend_Filter_StringTrim())
        ->setMultiOptions(array('M'=>'Male', 'F'=>'Female'))
        ->setDecorators(array( array('ViewHelper') ));

But I don't know where to set the lable and span classes in this code.

Please help.

Thanks.

Was it helpful?

Solution

I am not sure whether its the perfect method to do this its the first time i am using zend framework, But still here are the steps that i did, if you may find useful:

First I created a custom decorator which extends Zend_Form_Decorator_Abstract and saved it in the location 'decorator/My_Form_Decorator.php'. decorator is a directory created by me in root.

.
 /decorator
 /application 

etc...

Then i included it in a controller. I have read that there are certain methods for adding the decorator like addPrefixPath() but for time sake i just included the decorator file in top with 'include "../decorator/My_Form_Decorator.php";'. Then instead of using the CreateElement method i used Zend_Form_Element.

The following is the code of custom radio decorator

My_Form_Decorator.php

class My_Decorator_RadioInput extends Zend_Form_Decorator_Abstract
{


    public function render($content)
    {
        $element = $this->getElement();

        $label   = htmlentities($element->getLabel());
        $type = $element->type;
        $name = $element->elemName;
        $multiOptions = $element->multiOptions;
        $labelClass = $element->labelClass;
        $spanClass = $element->spanClass;
        $markup='';

        if(!empty($type) && !empty($name) && !empty($multiOptions)  && is_array($multiOptions)){
         foreach($multiOptions as $key=>$value){
           $markup .='<label class="'.$labelClass.'"><input type="radio" name="'.$name.'" value="'.$key.'"> <span class="'.$spanClass.'">'.$value.'</span></label>';                    
         }
        }
        return $markup;
    }
}

and this is the code in my controller function

IndexController.php

$decorator = new My_Decorator_RadioInput();
$form = new Zend_Form();
$form->setAttrib('id', 'test');
$element   = new Zend_Form_Element('foo', array(
    'elemName'=>'gender',
    'type' =>'radio',
    'multiOptions' => array('M'=>'Male', 'F'=>'Female'),
    'labelClass'=>'inline',
    'spanClass'=>'lbl',
    'decorators' => array($decorator),
));
$form->addElement($element);
$this->view->form = $form;

And in view index.phtml

echo $this->form

Hope this is helpful to you..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top