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

12,716

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.

Share:
12,716
Patrioticcow
Author by

Patrioticcow

spooky action at a distance

Updated on June 30, 2022

Comments

  • Patrioticcow
    Patrioticcow almost 2 years

    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

  • Jonathan Clark
    Jonathan Clark over 5 years
    Set a default option by adding 'value' => 'male' in the same format as 'label' => 'Test Thing'