How to add a radio button to a form with Zend Framework?
Asked Answered
U

1

7

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

Unbreathed answered 22/11, 2011 at 19:14 Comment(0)
R
15

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.

Rhoads answered 22/11, 2011 at 19:31 Comment(1)
Set a default option by adding 'value' => 'male' in the same format as 'label' => 'Test Thing'Ruse

© 2022 - 2024 — McMap. All rights reserved.