7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
25 * Abstract class for extension
27 require_once 'Zend/View/Helper/FormElement.php';
31 * Helper to generate a set of radio button elements
36 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license http://framework.zend.com/license/new-bsd New BSD License
39 class Zend_View_Helper_FormRadio
extends Zend_View_Helper_FormElement
45 protected $_inputType = 'radio';
48 * Whether or not this element represents an array collection by default
51 protected $_isArray = false;
54 * Generates a set of radio button elements.
58 * @param string|array $name If a string, the element name. If an
59 * array, all other parameters are ignored, and the array elements
60 * are extracted in place of added parameters.
62 * @param mixed $value The radio value to mark as 'checked'.
64 * @param array $options An array of key-value pairs where the array
65 * key is the radio value, and the array value is the radio text.
67 * @param array|string $attribs Attributes added to each radio.
69 * @return string The radio buttons XHTML.
71 public function formRadio($name, $value = null, $attribs = null,
72 $options = null, $listsep = "<br />\n")
75 $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
76 extract($info); // name, value, attribs, options, listsep, disable
78 // retrieve attributes for labels (prefixed with 'label_' or 'label')
79 $label_attribs = array();
80 foreach ($attribs as $key => $val) {
82 $keyLen = strlen($key);
83 if ((6 < $keyLen) && (substr($key, 0, 6) == 'label_')) {
84 $tmp = substr($key, 6);
85 } elseif ((5 < $keyLen) && (substr($key, 0, 5) == 'label')) {
86 $tmp = substr($key, 5);
90 // make sure first char is lowercase
91 $tmp[0] = strtolower($tmp[0]);
92 $label_attribs[$tmp] = $val;
93 unset($attribs[$key]);
97 $labelPlacement = 'append';
98 foreach ($label_attribs as $key => $val) {
99 switch (strtolower($key)) {
101 unset($label_attribs[$key]);
102 $val = strtolower($val);
103 if (in_array($val, array('prepend', 'append'))) {
104 $labelPlacement = $val;
110 // the radio button values and labels
111 $options = (array) $options;
117 // should the name affect an array collection?
118 $name = $this->view
->escape($name);
119 if ($this->_isArray
&& ('[]' != substr($name, -2))) {
123 // ensure value is an array to allow matching multiple times
124 $value = (array) $value;
126 // XHTML or HTML end tag?
128 if (($this->view
instanceof Zend_View_Abstract
) && !$this->view
->doctype()->isXhtml()) {
132 // add radio buttons to the list.
133 require_once 'Zend/Filter/Alnum.php';
134 $filter = new Zend_Filter_Alnum();
135 foreach ($options as $opt_value => $opt_label) {
137 // Should the label be escaped?
139 $opt_label = $this->view
->escape($opt_label);
144 if (true === $disable) {
145 $disabled = ' disabled="disabled"';
146 } elseif (is_array($disable) && in_array($opt_value, $disable)) {
147 $disabled = ' disabled="disabled"';
152 if (in_array($opt_value, $value)) {
153 $checked = ' checked="checked"';
157 $optId = $id . '-' . $filter->filter($opt_value);
159 // Wrap the radios in labels
161 . $this->_htmlAttribs($label_attribs) . ' for="' . $optId . '">'
162 . (('prepend' == $labelPlacement) ?
$opt_label : '')
163 . '<input type="' . $this->_inputType
. '"'
164 . ' name="' . $name . '"'
165 . ' id="' . $optId . '"'
166 . ' value="' . $this->view
->escape($opt_value) . '"'
169 . $this->_htmlAttribs($attribs)
171 . (('append' == $labelPlacement) ?
$opt_label : '')
174 // add to the array of radio buttons
179 $xhtml .= implode($listsep, $list);