CakeValidationSet.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * CakeValidationSet.
  4. *
  5. * Provides the Model validation logic.
  6. *
  7. * PHP versions 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.Model.Validator
  18. * @since CakePHP(tm) v 2.2.0
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('ModelValidator', 'Model');
  22. App::uses('CakeRule', 'Model/Validator');
  23. /**
  24. * CakeValidationSet object.
  25. *
  26. * @package Cake.Model.Validator
  27. * @link http://book.cakephp.org/2.0/en/data-validation.html
  28. */
  29. class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
  30. /**
  31. * Holds the ValidationRule objects
  32. *
  33. * @var array
  34. */
  35. protected $_rules = array();
  36. /**
  37. * Set of methods available for validation
  38. *
  39. * @var array
  40. **/
  41. protected $_methods = array();
  42. /**
  43. * I18n domain for validation messages.
  44. *
  45. * @var string
  46. **/
  47. protected $_validationDomain = null;
  48. /**
  49. * If the validation is stopped
  50. *
  51. * @var boolean
  52. */
  53. public $isStopped = false;
  54. /**
  55. * Holds the fieldname
  56. *
  57. * @var string
  58. */
  59. public $field = null;
  60. /**
  61. * Holds the original ruleSet
  62. *
  63. * @var array
  64. */
  65. public $ruleSet = array();
  66. /**
  67. * Constructor
  68. *
  69. * @param string $fieldName The fieldname
  70. * @param array $ruleset
  71. */
  72. public function __construct($fieldName, $ruleSet) {
  73. $this->field = $fieldName;
  74. if (!is_array($ruleSet) || (is_array($ruleSet) && isset($ruleSet['rule']))) {
  75. $ruleSet = array($ruleSet);
  76. }
  77. foreach ($ruleSet as $index => $validateProp) {
  78. $this->_rules[$index] = new CakeRule($validateProp);
  79. }
  80. $this->ruleSet = $ruleSet;
  81. }
  82. /**
  83. * Sets the list of methods to use for validation
  84. *
  85. * @return void
  86. **/
  87. public function setMethods(&$methods) {
  88. $this->_methods =& $methods;
  89. }
  90. /**
  91. * Sets the I18n domain for validation messages.
  92. *
  93. * @param string $validationDomain The validation domain to be used.
  94. * @return void
  95. */
  96. public function setValidationDomain($validationDomain) {
  97. $this->_validationDomain = $validationDomain;
  98. }
  99. /**
  100. * Validates a ModelField
  101. *
  102. * @return array list of validation errors for this field
  103. */
  104. public function validate($data, $isUpdate = false) {
  105. $errors = array();
  106. foreach ($this->getRules() as $name => $rule) {
  107. $rule->isUpdate($isUpdate);
  108. if ($rule->skip()) {
  109. continue;
  110. }
  111. $checkRequired = $rule->checkRequired($this->field, $data);
  112. if (!$checkRequired && array_key_exists($this->field, $data)) {
  113. if ($rule->checkEmpty($this->field, $data)) {
  114. break;
  115. }
  116. $rule->process($this->field, $data, $this->_methods);
  117. }
  118. if ($checkRequired || !$rule->isValid()) {
  119. $errors[] = $this->_processValidationResponse($name, $rule);
  120. if ($rule->isLast()) {
  121. break;
  122. }
  123. }
  124. }
  125. return $errors;
  126. }
  127. /**
  128. * Gets a rule for a certain index
  129. *
  130. * @param mixed index
  131. * @return ValidationRule
  132. */
  133. public function getRule($index) {
  134. if (!empty($this->_rules[$index])) {
  135. return $this->_rules[$index];
  136. }
  137. }
  138. /**
  139. * Gets all rules for this ModelField
  140. *
  141. * @return array
  142. */
  143. public function getRules() {
  144. return $this->_rules;
  145. }
  146. /**
  147. * Sets a ValidationRule $rule for key $key
  148. *
  149. * @param mixed $key The key under which the rule should be set
  150. * @param CakeRule|array $rule The validation rule to be set
  151. * @return ModelField
  152. */
  153. public function setRule($key, $rule) {
  154. if (!$rule instanceof CakeRule) {
  155. $rule = new CakeRule($rule);
  156. }
  157. $this->_rules[$key] = $rule;
  158. return $this;
  159. }
  160. /**
  161. * Sets the rules for a given field
  162. *
  163. * @param array $rules The rules to be set
  164. * @param bolean $mergeVars [optional] If true, merges vars instead of replace. Defaults to true.
  165. * @return ModelField
  166. */
  167. public function setRules($rules = array(), $mergeVars = true) {
  168. if ($mergeVars === false) {
  169. $this->_rules = $rules;
  170. } else {
  171. $this->_rules = array_merge($this->_rules, $rules);
  172. }
  173. return $this;
  174. }
  175. /**
  176. * Fetches the correct error message for a failed validation
  177. *
  178. * @param string $name the name of the rule as it was configured
  179. * @param CakeRule $rule the object containing validation information
  180. * @return string
  181. */
  182. protected function _processValidationResponse($name, $rule) {
  183. $message = $rule->getValidationResult();
  184. if (is_string($message)) {
  185. return $message;
  186. }
  187. $message = $rule->message;
  188. if ($message !== null) {
  189. $args = null;
  190. if (is_array($message)) {
  191. $result = $message[0];
  192. $args = array_slice($message, 1);
  193. } else {
  194. $result = $message;
  195. }
  196. if (is_array($rule->rule) && $args === null) {
  197. $args = array_slice($rule->rule, 1);
  198. }
  199. if (!empty($args)) {
  200. foreach ($args as $k => $arg) {
  201. $args[$k] = __d($this->_validationDomain, $arg);
  202. }
  203. }
  204. $message = __d($this->_validationDomain, $result, $args);
  205. } elseif (is_string($name)) {
  206. if (is_array($rule->rule)) {
  207. $args = array_slice($rule->rule, 1);
  208. if (!empty($args)) {
  209. foreach ($args as $k => $arg) {
  210. $args[$k] = __d($this->_validationDomain, $arg);
  211. }
  212. }
  213. $message = __d($this->_validationDomain, $name, $args);
  214. } else {
  215. $message = __d($this->_validationDomain, $name);
  216. }
  217. } else {
  218. $message = __d('cake_dev', 'This field cannot be left blank');
  219. }
  220. return $message;
  221. }
  222. /**
  223. * Returns wheter an index exists in the rule set
  224. *
  225. * @param string $index name of the rule
  226. * @return boolean
  227. **/
  228. public function offsetExists($index) {
  229. return isset($this->_rules[$index]);
  230. }
  231. /**
  232. * Returns a rule object by its index
  233. *
  234. * @param string $index name of the rule
  235. * @return CakeRule
  236. **/
  237. public function offsetGet($index) {
  238. return $this->_rules[$index];
  239. }
  240. /**
  241. * Sets or replace a validation rule
  242. *
  243. * @param string $index name of the rule
  244. * @param CakeRule|array rule to add to $index
  245. **/
  246. public function offsetSet($index, $rule) {
  247. $this->setRule($index, $rule);
  248. }
  249. /**
  250. * Unsets a validation rule
  251. *
  252. * @param string $index name of the rule
  253. * @return void
  254. **/
  255. public function offsetUnset($index) {
  256. unset($this->_rules[$index]);
  257. }
  258. /**
  259. * Returns an iterator for each of the rules to be applied
  260. *
  261. * @return ArrayIterator
  262. **/
  263. public function getIterator() {
  264. return new ArrayIterator($this->_rules);
  265. }
  266. /**
  267. * Returns the number of rules in this set
  268. *
  269. * @return int
  270. **/
  271. public function count() {
  272. return count($this->_rules);
  273. }
  274. }