/libraries/Zend/Dojo/Form/Element/ValidationTextBox.php

https://github.com/kiranatama/sagalaya · PHP · 217 lines · 86 code · 16 blank · 115 comment · 4 complexity · eb7bde183a816615355d224963d4601d MD5 · raw file

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