How to add a radio button to a form with Zend Framework?

Patrioticcow picture Patrioticcow · Nov 22, 2011 · Viewed 12.4k times · Source

the issue is not so much as adding a radio button form element but how to i match it in my script format. It looks like there are multiple ways of doing things in zend.

here is my script:

 $this->addElement('text', 'time_from', array(
        'id' => 'datepicker',
        'class' => 'time_from',
        'filters' => array('StringTrim', 'StripTags'),
        'required' => true,
        'label' => 'Start Date of the Week: ',
        'value' => ''
    ));

    $update = $this->addElement('submit', 'update', array(
        'required' => false,
        'ignore' => true,
        'label' => 'Change Time'
            ));

based on this template, how do i add a radio button?

i believe a logical approach would be to use something like this:

$this->addElement('radio', 'test', array(
            'label'=>'Test Thing',
            'addMultiOptions'=>array(
                'male' => 'Male',
            'female' => 'Female'
            ),
    ));

but this doesn't display any radio buttons.

I've also tried to replace the :

'male' => 'Male',
'female' => 'Female'

with and array, but i get all kind of errors:

array('male'=>'Male', 'female'=>'Female')

any ideas on how to do this?

and , if is ot too much to ask maybe someone can explain why so many ways to do this forms and what is the common nominator in between them.

thanks

Answer

Jona picture Jona · Nov 22, 2011

You were quite right, but since you construct the element with addElement() you don't have addMultiOptions (the function name), it's just multiOptions.

$this->addElement('radio', 'test', array(
    'label'=>'Test Thing',
    'multiOptions'=>array(
        'male' => 'Male',
        'female' => 'Female',
    ),
));

You can see a summary of all Zend_Form elements here Edit: updated link.