CakeValidationSet.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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-2012, 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-2012, 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('CakeValidationRule', 'Model/Validator');
  22. /**
  23. * CakeValidationSet object. Holds all validation rules for a field and exposes
  24. * methods to dynamically add or remove validation rules
  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 CakeValidationRule objects
  32. *
  33. * @var array
  34. */
  35. protected $_rules = array();
  36. /**
  37. * List 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. * Whether 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 CakeValidationRule($validateProp);
  79. }
  80. $this->ruleSet = $ruleSet;
  81. }
  82. /**
  83. * Sets the list of methods to use for validation
  84. *
  85. * @param array $methods Methods list
  86. * @return void
  87. */
  88. public function setMethods(&$methods) {
  89. $this->_methods =& $methods;
  90. }
  91. /**
  92. * Sets the I18n domain for validation messages.
  93. *
  94. * @param string $validationDomain The validation domain to be used.
  95. * @return void
  96. */
  97. public function setValidationDomain($validationDomain) {
  98. $this->_validationDomain = $validationDomain;
  99. }
  100. /**
  101. * Runs all validation rules in this set and returns a list of
  102. * validation errors
  103. *
  104. * @param array $data Data array
  105. * @param boolean $isUpdate Is record being updated or created
  106. * @return array list of validation errors for this field
  107. */
  108. public function validate($data, $isUpdate = false) {
  109. $this->reset();
  110. $errors = array();
  111. foreach ($this->getRules() as $name => $rule) {
  112. $rule->isUpdate($isUpdate);
  113. if ($rule->skip()) {
  114. continue;
  115. }
  116. $checkRequired = $rule->checkRequired($this->field, $data);
  117. if (!$checkRequired && array_key_exists($this->field, $data)) {
  118. if ($rule->checkEmpty($this->field, $data)) {
  119. break;
  120. }
  121. $rule->process($this->field, $data, $this->_methods);
  122. }
  123. if ($checkRequired || !$rule->isValid()) {
  124. $errors[] = $this->_processValidationResponse($name, $rule);
  125. if ($rule->isLast()) {
  126. break;
  127. }
  128. }
  129. }
  130. return $errors;
  131. }
  132. /**
  133. * Resets interal state for all validation rules in this set
  134. *
  135. * @return void
  136. */
  137. public function reset() {
  138. foreach ($this->getRules() as $rule) {
  139. $rule->reset();
  140. }
  141. }
  142. /**
  143. * Gets a rule for a given name if exists
  144. *
  145. * @param string $name
  146. * @return CakeValidationRule
  147. */
  148. public function getRule($name) {
  149. if (!empty($this->_rules[$name])) {
  150. return $this->_rules[$name];
  151. }
  152. }
  153. /**
  154. * Returns all rules for this validation set
  155. *
  156. * @return array
  157. */
  158. public function getRules() {
  159. return $this->_rules;
  160. }
  161. /**
  162. * Sets a CakeValidationRule $rule with a $name
  163. *
  164. * ## Example:
  165. *
  166. * {{{
  167. * $set
  168. * ->setRule('required', array('rule' => 'notEmpty', 'required' => true))
  169. * ->setRule('inRange', array('rule' => array('between', 4, 10))
  170. * }}}
  171. *
  172. * @param string $name The name under which the rule should be set
  173. * @param CakeValidationRule|array $rule The validation rule to be set
  174. * @return CakeValidationSet this instance
  175. */
  176. public function setRule($name, $rule) {
  177. if (!($rule instanceof CakeValidationRule)) {
  178. $rule = new CakeValidationRule($rule);
  179. }
  180. $this->_rules[$name] = $rule;
  181. return $this;
  182. }
  183. /**
  184. * Removes a validation rule from the set
  185. *
  186. * ## Example:
  187. *
  188. * {{{
  189. * $set
  190. * ->removeRule('required')
  191. * ->removeRule('inRange')
  192. * }}}
  193. *
  194. * @param string $name The name under which the rule should be unset
  195. * @return CakeValidationSet this instance
  196. */
  197. public function removeRule($name) {
  198. unset($this->_rules[$name]);
  199. return $this;
  200. }
  201. /**
  202. * Sets the rules for a given field
  203. *
  204. * ## Example:
  205. *
  206. * {{{
  207. * $set->setRules(array(
  208. * 'required' => array('rule' => 'notEmpty', 'required' => true),
  209. * 'inRange' => array('rule' => array('between', 4, 10)
  210. * ));
  211. * }}}
  212. *
  213. * @param array $rules The rules to be set
  214. * @param bolean $mergeVars [optional] If true, merges vars instead of replace. Defaults to true.
  215. * @return ModelField
  216. */
  217. public function setRules($rules = array(), $mergeVars = true) {
  218. if ($mergeVars === false) {
  219. $this->_rules = array();
  220. }
  221. foreach ($rules as $name => $rule) {
  222. $this->setRule($name, $rule);
  223. }
  224. return $this;
  225. }
  226. /**
  227. * Fetches the correct error message for a failed validation
  228. *
  229. * @param string $name the name of the rule as it was configured
  230. * @param CakeValidationRule $rule the object containing validation information
  231. * @return string
  232. */
  233. protected function _processValidationResponse($name, $rule) {
  234. $message = $rule->getValidationResult();
  235. if (is_string($message)) {
  236. return $message;
  237. }
  238. $message = $rule->message;
  239. if ($message !== null) {
  240. $args = null;
  241. if (is_array($message)) {
  242. $result = $message[0];
  243. $args = array_slice($message, 1);
  244. } else {
  245. $result = $message;
  246. }
  247. if (is_array($rule->rule) && $args === null) {
  248. $args = array_slice($rule->rule, 1);
  249. }
  250. $args = $this->_translateArgs($args);
  251. $message = __d($this->_validationDomain, $result, $args);
  252. } elseif (is_string($name)) {
  253. if (is_array($rule->rule)) {
  254. $args = array_slice($rule->rule, 1);
  255. $args = $this->_translateArgs($args);
  256. $message = __d($this->_validationDomain, $name, $args);
  257. } else {
  258. $message = __d($this->_validationDomain, $name);
  259. }
  260. } else {
  261. $message = __d('cake', 'This field cannot be left blank');
  262. }
  263. return $message;
  264. }
  265. /**
  266. * Applies translations to validator arguments.
  267. *
  268. * @param array $args The args to translate
  269. * @return array Translated args.
  270. */
  271. protected function _translateArgs($args) {
  272. foreach ((array)$args as $k => $arg) {
  273. if (is_string($arg)) {
  274. $args[$k] = __d($this->_validationDomain, $arg);
  275. }
  276. }
  277. return $args;
  278. }
  279. /**
  280. * Returns wheter an index exists in the rule set
  281. *
  282. * @param string $index name of the rule
  283. * @return boolean
  284. */
  285. public function offsetExists($index) {
  286. return isset($this->_rules[$index]);
  287. }
  288. /**
  289. * Returns a rule object by its index
  290. *
  291. * @param string $index name of the rule
  292. * @return CakeValidationRule
  293. */
  294. public function offsetGet($index) {
  295. return $this->_rules[$index];
  296. }
  297. /**
  298. * Sets or replace a validation rule
  299. *
  300. * @param string $index name of the rule
  301. * @param CakeValidationRule|array rule to add to $index
  302. */
  303. public function offsetSet($index, $rule) {
  304. $this->setRule($index, $rule);
  305. }
  306. /**
  307. * Unsets a validation rule
  308. *
  309. * @param string $index name of the rule
  310. * @return void
  311. */
  312. public function offsetUnset($index) {
  313. unset($this->_rules[$index]);
  314. }
  315. /**
  316. * Returns an iterator for each of the rules to be applied
  317. *
  318. * @return ArrayIterator
  319. */
  320. public function getIterator() {
  321. return new ArrayIterator($this->_rules);
  322. }
  323. /**
  324. * Returns the number of rules in this set
  325. *
  326. * @return int
  327. */
  328. public function count() {
  329. return count($this->_rules);
  330. }
  331. }