Checkbox.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @subpackage Element
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Form_Element_Xhtml */
  22. require_once 'Zend/Form/Element/Xhtml.php';
  23. /**
  24. * Checkbox form element
  25. *
  26. * @category Zend
  27. * @package Zend_Form
  28. * @subpackage Element
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @version $Id$
  32. */
  33. class Zend_Form_Element_Checkbox extends Zend_Form_Element_Xhtml
  34. {
  35. /**
  36. * Is the checkbox checked?
  37. * @var bool
  38. */
  39. public $checked = false;
  40. /**
  41. * Use formCheckbox view helper by default
  42. * @var string
  43. */
  44. public $helper = 'formCheckbox';
  45. /**
  46. * Value when checked
  47. * @var string
  48. */
  49. protected $_checkedValue = '1';
  50. /**
  51. * Value when not checked
  52. * @var string
  53. */
  54. protected $_uncheckedValue = '0';
  55. /**
  56. * Current value
  57. * @var string 0 or 1
  58. */
  59. protected $_value = '0';
  60. /**
  61. * Set options
  62. *
  63. * Intercept checked and unchecked values and set them early; test stored
  64. * value against checked and unchecked values after configuration.
  65. *
  66. * @param array $options
  67. * @return Zend_Form_Element_Checkbox
  68. */
  69. public function setOptions(array $options)
  70. {
  71. if (array_key_exists('checkedValue', $options)) {
  72. $this->setCheckedValue($options['checkedValue']);
  73. unset($options['checkedValue']);
  74. }
  75. if (array_key_exists('uncheckedValue', $options)) {
  76. $this->setUncheckedValue($options['uncheckedValue']);
  77. unset($options['uncheckedValue']);
  78. }
  79. parent::setOptions($options);
  80. $curValue = $this->getValue();
  81. $test = array($this->getCheckedValue(), $this->getUncheckedValue());
  82. if (!in_array($curValue, $test)) {
  83. $this->setValue($curValue);
  84. }
  85. return $this;
  86. }
  87. /**
  88. * Set value
  89. *
  90. * If value matches checked value, sets to that value, and sets the checked
  91. * flag to true.
  92. *
  93. * Any other value causes the unchecked value to be set as the current
  94. * value, and the checked flag to be set as false.
  95. *
  96. *
  97. * @param mixed $value
  98. * @return Zend_Form_Element_Checkbox
  99. */
  100. public function setValue($value)
  101. {
  102. if ($value == $this->getCheckedValue()) {
  103. parent::setValue($value);
  104. $this->checked = true;
  105. } else {
  106. parent::setValue($this->getUncheckedValue());
  107. $this->checked = false;
  108. }
  109. return $this;
  110. }
  111. /**
  112. * Set checked value
  113. *
  114. * @param string $value
  115. * @return Zend_Form_Element_Checkbox
  116. */
  117. public function setCheckedValue($value)
  118. {
  119. $this->_checkedValue = (string) $value;
  120. return $this;
  121. }
  122. /**
  123. * Get value when checked
  124. *
  125. * @return string
  126. */
  127. public function getCheckedValue()
  128. {
  129. return $this->_checkedValue;
  130. }
  131. /**
  132. * Set unchecked value
  133. *
  134. * @param string $value
  135. * @return Zend_Form_Element_Checkbox
  136. */
  137. public function setUncheckedValue($value)
  138. {
  139. $this->_uncheckedValue = (string) $value;
  140. return $this;
  141. }
  142. /**
  143. * Get value when not checked
  144. *
  145. * @return string
  146. */
  147. public function getUncheckedValue()
  148. {
  149. return $this->_uncheckedValue;
  150. }
  151. /**
  152. * Set checked flag
  153. *
  154. * @param bool $flag
  155. * @return Zend_Form_Element_Checkbox
  156. */
  157. public function setChecked($flag)
  158. {
  159. $this->checked = (bool) $flag;
  160. if ($this->checked) {
  161. $this->setValue($this->getCheckedValue());
  162. } else {
  163. $this->setValue($this->getUncheckedValue());
  164. }
  165. return $this;
  166. }
  167. /**
  168. * Get checked flag
  169. *
  170. * @return bool
  171. */
  172. public function isChecked()
  173. {
  174. return $this->checked;
  175. }
  176. /**
  177. * Render
  178. *
  179. * Ensure that options property is set when rendering.
  180. *
  181. * @param Zend_View_Interface $view
  182. * @return string
  183. */
  184. public function render(Zend_View_Interface $view = null)
  185. {
  186. $this->options = array(
  187. 'checked' => $this->getCheckedValue(),
  188. 'unChecked' => $this->getUncheckedValue(),
  189. );
  190. return parent::render($view);
  191. }
  192. }