Zend - 如何在viewscript中获取单独的单选按钮

I'm setting the radio button like this:

$gender = new Zend_Form_Element_Radio('gender');
$gender->setLabel('Gender')
    ->addMultiOptions(array('m'=>'Male','f'=>'Female'))
    ->setDecorators(array('ViewHelper'))
    ->setRequired(true)
    ->addValidator('NotEmpty', true, $required);

And I want to retrieve the inputs it creates individually in the viewscript. I've tried:

$fields = $this->element->getElements();
$fields['gender']->getMultiOption('m')

But that just gets the value 'Male'. How can I get the individual radio inputs that addMultiOptions generates?

Thanks

Try using getMultiOptions()-method:

$fields = $this->element->getElements();
$options = $fields['gender']->getMultiOptions();

Here's an example how to get the radio buttons rendered:

$fields = $this->element->getElements();
echo "<label>{$fields['gender']->getLabel()}</label>";
foreach($fields['gender']->getMultiOptions() as $value => $label) {
    echo '<input type="radio" value="' . $value . '" name="gender" /> ' . $label;
}

Probably the better solution is to extend the radio class to include a renderMultiOption($opt) function. In that function, duplicate the way the radio button renders in its stock render option (I believe you'll need to look at Zend_Form_Element_Multi as well, but not sure)

Really this should be something Zend includes, since one of the main reasons to viewscript is to arrange the rendered elements in a custom manner, not to hand-render the elements. I think it would be worth your while to extend the class in this case, as this would keep your viewscript more consistent. Probably you'd also want to make a method to render the singleton aspects of the radio button - label and open/close tag for the element itself.

I'm going to have to do this myself, so when I do I'll probably post how I did it. We need to custom-arrange radio buttons for a print form, but that shouldn't entail writing markup for the radio buttons individually.

Simple but Zend-like way to render own radio is to create own View Helper:

<?php

class Zend_View_Helper_Radio extends Zend_View_Helper_Abstract {

    protected $name;
    protected $value;
    protected $id;

    public function radio($name, $value, $id = null) {
        $this->id = $id;
        $this->name = $name;
        $this->value = $value;
        return $this;
    }

    public function render() {
        $html = '';

        $html .= '<input type=radio';
        if ($this->id) {
            $html .= ' id="' . htmlspecialchars($this->id) . '"';
        }
        $html .= ' name="' . htmlspecialchars($this->name) . '"';
        $html .= ' value="' . htmlspecialchars($this->value) . '"';
        $html .= ' />';

        return $html;
    }

    public function __toString() {
        try {
        return $this->render();
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }
}

Then you can render the elements as same as already mentioned by Hikaru-Shindo using the helper:

foreach ($form->gender->getMultiOptions() as $value => $name) {
    echo '<label for="gender-', $value, '">';
    echo $this->radio('gender', $value, 'gender-' . $value);
    echo '<img src="/css/gender-', $value, '.png">';
    echo $name, '</label>';
};

Of course you can create another View Helper specifically for your case and then render as

$this->genderSelection($form->gender->getMultiOptions());