Reputation: 2705
I have this element inside my Zend form
$genderOptions = array( 'male'=>'male', 'Female'=>'Female');
$gender= new Zend_Form_Element_Radio('gender');
$gender->setDecorators(array('ViewHelper'))
->setAttrib('name', 'gender')
->setAttrib('class', 'required error pull-right')
->setAttrib('id', 'gender')
->setRequired(false)
->setMultiOptions($genderOptions);
And I want to retrieve the inputs individually in the viewscript (phtml file). I've tried as
<div>
<span>Male</span>
echo $this->myForm->gender['male'];
</div>
<div>
<span>Female</span>
echo $this->myForm->gender['female'];
</div>
how can I do that using Zend Form?
Thanks
Upvotes: 2
Views: 763
Reputation: 9857
For form elements extending Zend_Form_Element_Multi
you can use the getMultiOption($option)
to fetch a single option.
View.phtml
<div>
<span>Male</span>
<?php echo $this->myForm->gender->getMultiOption('male'); ?>
</div>
<div>
<span>Female</span>
<?php echo $this->myForm->gender->getMultiOption('female'); ?>
</div>
Alternatively, you may want to check that the option is available before you attempt to use it (or you will get NULL
)
<?php
$gender = $this->myForm->gender;
$option = $gender->getMultiOptions(); // returns assoc array
if (isset($option['male']))
printf('<div><span>Male</span>%s</div>', $option['male']);
if (isset($option['female']))
printf('<div><span>Female</span>%s</div>', $option['female']);
?>
Edit
Having re-read your question I can see you're looking for the individual radio elements, rather than the value.
This may be harder to achieve as the Zend_Form_Element_Radio
class actually represents all the radio options; Where the view helper Zend_View_Helper_FormRadio
loops over each 'option' (i.e. male, female) and returns the complete HTML string with each <input type="radio"/>
already included.
Shockingly the Zend_View_Helper_FormRadio
helper actually has all of its HTML generation code within one method; This makes it very hard to override it without duplication.
Personally, I would :
MyNamespace_View_Helper_CustomFormRadio
extending Zend_View_Helper_FormElement
Zend_View_Helper_FormRadio
(FormRadio()
) into your new helperinput
is created.For example
$radio = '<div><span'
. $this->_htmlAttribs($label_attribs) . '>'
. (('prepend' == $labelPlacement) ? $opt_label : '')
. '<input type="' . $this->_inputType . '"'
. ' name="' . $name . '"'
. ' id="' . $optId . '"'
. ' value="' . $this->view->escape($opt_value) . '"'
. $checked
. $disabled
. $this->_htmlAttribs($attribs)
. $this->getClosingBracket()
. (('append' == $labelPlacement) ? $opt_label : '')
. '</span></div>';
$this->customFormRadio()
Upvotes: 1