ValidationTextBox.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_Dojo
  17. * @subpackage Form_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_Dojo_Form_Element_TextBox */
  22. require_once 'Zend/Dojo/Form/Element/TextBox.php';
  23. /**
  24. * ValidationTextBox dijit
  25. *
  26. * @uses Zend_Dojo_Form_Element_TextBox
  27. * @package Zend_Dojo
  28. * @subpackage Form_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_Dojo_Form_Element_ValidationTextBox extends Zend_Dojo_Form_Element_TextBox
  34. {
  35. /**
  36. * Use ValidationTextBox dijit view helper
  37. * @var string
  38. */
  39. public $helper = 'ValidationTextBox';
  40. /**
  41. * Set invalidMessage
  42. *
  43. * @param string $message
  44. * @return Zend_Dojo_Form_Element_ValidationTextBox
  45. */
  46. public function setInvalidMessage($message)
  47. {
  48. $this->setDijitParam('invalidMessage', (string) $message);
  49. return $this;
  50. }
  51. /**
  52. * Retrieve invalidMessage
  53. *
  54. * @return string|null
  55. */
  56. public function getInvalidMessage()
  57. {
  58. return $this->getDijitParam('invalidMessage');
  59. }
  60. /**
  61. * Set promptMessage
  62. *
  63. * @param string $message
  64. * @return Zend_Dojo_Form_Element_ValidationTextBox
  65. */
  66. public function setPromptMessage($message)
  67. {
  68. $this->setDijitParam('promptMessage', (string) $message);
  69. return $this;
  70. }
  71. /**
  72. * Retrieve promptMessage
  73. *
  74. * @return string|null
  75. */
  76. public function getPromptMessage()
  77. {
  78. return $this->getDijitParam('promptMessage');
  79. }
  80. /**
  81. * Set regExp
  82. *
  83. * @param string $regexp
  84. * @return Zend_Dojo_Form_Element_ValidationTextBox
  85. */
  86. public function setRegExp($regexp)
  87. {
  88. $this->setDijitParam('regExp', (string) $regexp);
  89. return $this;
  90. }
  91. /**
  92. * Retrieve regExp
  93. *
  94. * @return string|null
  95. */
  96. public function getRegExp()
  97. {
  98. return $this->getDijitParam('regExp');
  99. }
  100. /**
  101. * Set an individual constraint
  102. *
  103. * @param string $key
  104. * @param mixed $value
  105. * @return Zend_Dojo_Form_Element_ValidationTextBox
  106. */
  107. public function setConstraint($key, $value)
  108. {
  109. $constraints = $this->getConstraints();
  110. $constraints[(string) $key] = $value;
  111. $this->setConstraints($constraints);
  112. return $this;
  113. }
  114. /**
  115. * Set validation constraints
  116. *
  117. * Refer to Dojo dijit.form.ValidationTextBox documentation for valid
  118. * structure.
  119. *
  120. * @param array $constraints
  121. * @return Zend_Dojo_Form_Element_ValidationTextBox
  122. */
  123. public function setConstraints(array $constraints)
  124. {
  125. $tmp = $this->getConstraints();
  126. $constraints = array_merge($tmp, $constraints);
  127. array_walk_recursive($constraints, array($this, '_castBoolToString'));
  128. $this->setDijitParam('constraints', $constraints);
  129. return $this;
  130. }
  131. /**
  132. * Is the given constraint set?
  133. *
  134. * @param string $key
  135. * @return bool
  136. */
  137. public function hasConstraint($key)
  138. {
  139. $constraints = $this->getConstraints();
  140. return array_key_exists((string)$key, $constraints);
  141. }
  142. /**
  143. * Get an individual constraint
  144. *
  145. * @param string $key
  146. * @return mixed
  147. */
  148. public function getConstraint($key)
  149. {
  150. $key = (string) $key;
  151. if (!$this->hasConstraint($key)) {
  152. return null;
  153. }
  154. return $this->dijitParams['constraints'][$key];
  155. }
  156. /**
  157. * Get constraints
  158. *
  159. * @return array
  160. */
  161. public function getConstraints()
  162. {
  163. if ($this->hasDijitParam('constraints')) {
  164. return $this->getDijitParam('constraints');
  165. }
  166. return array();
  167. }
  168. /**
  169. * Remove a single constraint
  170. *
  171. * @param string $key
  172. * @return Zend_Dojo_Form_Element_ValidationTextBox
  173. */
  174. public function removeConstraint($key)
  175. {
  176. $key = (string) $key;
  177. if ($this->hasConstraint($key)) {
  178. unset($this->dijitParams['constraints'][$key]);
  179. }
  180. return $this;
  181. }
  182. /**
  183. * Clear all constraints
  184. *
  185. * @return Zend_Dojo_Form_Element_ValidationTextBox
  186. */
  187. public function clearConstraints()
  188. {
  189. return $this->removeDijitParam('constraints');
  190. }
  191. /**
  192. * Cast a boolean value to a string
  193. *
  194. * @param mixed $item
  195. * @param string $key
  196. * @return void
  197. */
  198. protected function _castBoolToString(&$item, $key)
  199. {
  200. if (is_bool($item)) {
  201. $item = ($item) ? 'true' : 'false';
  202. }
  203. }
  204. }