Checkbox.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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-2015 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-2015 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. * Options that will be passed to the view helper
  47. * @var array
  48. */
  49. public $options = array(
  50. 'checkedValue' => '1',
  51. 'uncheckedValue' => '0',
  52. );
  53. /**
  54. * Value when checked
  55. * @var string
  56. */
  57. protected $_checkedValue = '1';
  58. /**
  59. * Value when not checked
  60. * @var string
  61. */
  62. protected $_uncheckedValue = '0';
  63. /**
  64. * Current value
  65. * @var string 0 or 1
  66. */
  67. protected $_value = '0';
  68. /**
  69. * Set options
  70. *
  71. * Intercept checked and unchecked values and set them early; test stored
  72. * value against checked and unchecked values after configuration.
  73. *
  74. * @param array $options
  75. * @return Zend_Form_Element_Checkbox
  76. */
  77. public function setOptions(array $options)
  78. {
  79. if (array_key_exists('checkedValue', $options)) {
  80. $this->setCheckedValue($options['checkedValue']);
  81. unset($options['checkedValue']);
  82. }
  83. if (array_key_exists('uncheckedValue', $options)) {
  84. $this->setUncheckedValue($options['uncheckedValue']);
  85. unset($options['uncheckedValue']);
  86. }
  87. parent::setOptions($options);
  88. $curValue = $this->getValue();
  89. $test = array($this->getCheckedValue(), $this->getUncheckedValue());
  90. if (!in_array($curValue, $test)) {
  91. $this->setValue($curValue);
  92. }
  93. return $this;
  94. }
  95. /**
  96. * Set value
  97. *
  98. * If value matches checked value, sets to that value, and sets the checked
  99. * flag to true.
  100. *
  101. * Any other value causes the unchecked value to be set as the current
  102. * value, and the checked flag to be set as false.
  103. *
  104. *
  105. * @param mixed $value
  106. * @return Zend_Form_Element_Checkbox
  107. */
  108. public function setValue($value)
  109. {
  110. if ($value == $this->getCheckedValue()) {
  111. parent::setValue($value);
  112. $this->checked = true;
  113. } else {
  114. parent::setValue($this->getUncheckedValue());
  115. $this->checked = false;
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Set checked value
  121. *
  122. * @param string $value
  123. * @return Zend_Form_Element_Checkbox
  124. */
  125. public function setCheckedValue($value)
  126. {
  127. $this->_checkedValue = (string) $value;
  128. $this->options['checkedValue'] = $value;
  129. return $this;
  130. }
  131. /**
  132. * Get value when checked
  133. *
  134. * @return string
  135. */
  136. public function getCheckedValue()
  137. {
  138. return $this->_checkedValue;
  139. }
  140. /**
  141. * Set unchecked value
  142. *
  143. * @param string $value
  144. * @return Zend_Form_Element_Checkbox
  145. */
  146. public function setUncheckedValue($value)
  147. {
  148. $this->_uncheckedValue = (string) $value;
  149. $this->options['uncheckedValue'] = $value;
  150. return $this;
  151. }
  152. /**
  153. * Get value when not checked
  154. *
  155. * @return string
  156. */
  157. public function getUncheckedValue()
  158. {
  159. return $this->_uncheckedValue;
  160. }
  161. /**
  162. * Set checked flag
  163. *
  164. * @param bool $flag
  165. * @return Zend_Form_Element_Checkbox
  166. */
  167. public function setChecked($flag)
  168. {
  169. $this->checked = (bool) $flag;
  170. if ($this->checked) {
  171. $this->setValue($this->getCheckedValue());
  172. } else {
  173. $this->setValue($this->getUncheckedValue());
  174. }
  175. return $this;
  176. }
  177. /**
  178. * Get checked flag
  179. *
  180. * @return bool
  181. */
  182. public function isChecked()
  183. {
  184. return $this->checked;
  185. }
  186. }