CakeValidationSet.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * For full copyright and license information, please see the LICENSE.txt
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP(tm) Project
  18. * @package Cake.Model.Validator
  19. * @since CakePHP(tm) v 2.2.0
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::uses('CakeValidationRule', 'Model/Validator');
  23. /**
  24. * CakeValidationSet object. Holds all validation rules for a field and exposes
  25. * methods to dynamically add or remove validation rules
  26. *
  27. * @package Cake.Model.Validator
  28. * @link http://book.cakephp.org/2.0/en/data-validation.html
  29. */
  30. class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
  31. /**
  32. * Holds the CakeValidationRule objects
  33. *
  34. * @var array
  35. */
  36. protected $_rules = array();
  37. /**
  38. * List of methods available for validation
  39. *
  40. * @var array
  41. */
  42. protected $_methods = array();
  43. /**
  44. * I18n domain for validation messages.
  45. *
  46. * @var string
  47. */
  48. protected $_validationDomain = null;
  49. /**
  50. * Whether the validation is stopped
  51. *
  52. * @var boolean
  53. */
  54. public $isStopped = false;
  55. /**
  56. * Holds the fieldname
  57. *
  58. * @var string
  59. */
  60. public $field = null;
  61. /**
  62. * Holds the original ruleSet
  63. *
  64. * @var array
  65. */
  66. public $ruleSet = array();
  67. /**
  68. * Constructor
  69. *
  70. * @param string $fieldName The fieldname
  71. * @param array $ruleset
  72. */
  73. public function __construct($fieldName, $ruleSet) {
  74. $this->field = $fieldName;
  75. if (!is_array($ruleSet) || (is_array($ruleSet) && isset($ruleSet['rule']))) {
  76. $ruleSet = array($ruleSet);
  77. }
  78. foreach ($ruleSet as $index => $validateProp) {
  79. $this->_rules[$index] = new CakeValidationRule($validateProp);
  80. }
  81. $this->ruleSet = $ruleSet;
  82. }
  83. /**
  84. * Sets the list of methods to use for validation
  85. *
  86. * @param array $methods Methods list
  87. * @return void
  88. */
  89. public function setMethods(&$methods) {
  90. $this->_methods =& $methods;
  91. }
  92. /**
  93. * Sets the I18n domain for validation messages.
  94. *
  95. * @param string $validationDomain The validation domain to be used.
  96. * @return void
  97. */
  98. public function setValidationDomain($validationDomain) {
  99. $this->_validationDomain = $validationDomain;
  100. }
  101. /**
  102. * Runs all validation rules in this set and returns a list of
  103. * validation errors
  104. *
  105. * @param array $data Data array
  106. * @param boolean $isUpdate Is record being updated or created
  107. * @return array list of validation errors for this field
  108. */
  109. public function validate($data, $isUpdate = false) {
  110. $this->reset();
  111. $errors = array();
  112. foreach ($this->getRules() as $name => $rule) {
  113. $rule->isUpdate($isUpdate);
  114. if ($rule->skip()) {
  115. continue;
  116. }
  117. $checkRequired = $rule->checkRequired($this->field, $data);
  118. if (!$checkRequired && array_key_exists($this->field, $data)) {
  119. if ($rule->checkEmpty($this->field, $data)) {
  120. break;
  121. }
  122. $rule->process($this->field, $data, $this->_methods);
  123. }
  124. if ($checkRequired || !$rule->isValid()) {
  125. $errors[] = $this->_processValidationResponse($name, $rule);
  126. if ($rule->isLast()) {
  127. break;
  128. }
  129. }
  130. }
  131. return $errors;
  132. }
  133. /**
  134. * Resets internal state for all validation rules in this set
  135. *
  136. * @return void
  137. */
  138. public function reset() {
  139. foreach ($this->getRules() as $rule) {
  140. $rule->reset();
  141. }
  142. }
  143. /**
  144. * Gets a rule for a given name if exists
  145. *
  146. * @param string $name
  147. * @return CakeValidationRule
  148. */
  149. public function getRule($name) {
  150. if (!empty($this->_rules[$name])) {
  151. return $this->_rules[$name];
  152. }
  153. }
  154. /**
  155. * Returns all rules for this validation set
  156. *
  157. * @return array
  158. */
  159. public function getRules() {
  160. return $this->_rules;
  161. }
  162. /**
  163. * Sets a CakeValidationRule $rule with a $name
  164. *
  165. * ## Example:
  166. *
  167. * {{{
  168. * $set
  169. * ->setRule('required', array('rule' => 'notEmpty', 'required' => true))
  170. * ->setRule('inRange', array('rule' => array('between', 4, 10))
  171. * }}}
  172. *
  173. * @param string $name The name under which the rule should be set
  174. * @param CakeValidationRule|array $rule The validation rule to be set
  175. * @return CakeValidationSet this instance
  176. */
  177. public function setRule($name, $rule) {
  178. if (!($rule instanceof CakeValidationRule)) {
  179. $rule = new CakeValidationRule($rule);
  180. }
  181. $this->_rules[$name] = $rule;
  182. return $this;
  183. }
  184. /**
  185. * Removes a validation rule from the set
  186. *
  187. * ## Example:
  188. *
  189. * {{{
  190. * $set
  191. * ->removeRule('required')
  192. * ->removeRule('inRange')
  193. * }}}
  194. *
  195. * @param string $name The name under which the rule should be unset
  196. * @return CakeValidationSet this instance
  197. */
  198. public function removeRule($name) {
  199. unset($this->_rules[$name]);
  200. return $this;
  201. }
  202. /**
  203. * Sets the rules for a given field
  204. *
  205. * ## Example:
  206. *
  207. * {{{
  208. * $set->setRules(array(
  209. * 'required' => array('rule' => 'notEmpty', 'required' => true),
  210. * 'inRange' => array('rule' => array('between', 4, 10)
  211. * ));
  212. * }}}
  213. *
  214. * @param array $rules The rules to be set
  215. * @param boolean $mergeVars [optional] If true, merges vars instead of replace. Defaults to true.
  216. * @return ModelField
  217. */
  218. public function setRules($rules = array(), $mergeVars = true) {
  219. if ($mergeVars === false) {
  220. $this->_rules = array();
  221. }
  222. foreach ($rules as $name => $rule) {
  223. $this->setRule($name, $rule);
  224. }
  225. return $this;
  226. }
  227. /**
  228. * Fetches the correct error message for a failed validation
  229. *
  230. * @param string $name the name of the rule as it was configured
  231. * @param CakeValidationRule $rule the object containing validation information
  232. * @return string
  233. */
  234. protected function _processValidationResponse($name, $rule) {
  235. $message = $rule->getValidationResult();
  236. if (is_string($message)) {
  237. return $message;
  238. }
  239. $message = $rule->message;
  240. if ($message !== null) {
  241. $args = null;
  242. if (is_array($message)) {
  243. $result = $message[0];
  244. $args = array_slice($message, 1);
  245. } else {
  246. $result = $message;
  247. }
  248. if (is_array($rule->rule) && $args === null) {
  249. $args = array_slice($rule->rule, 1);
  250. }
  251. $args = $this->_translateArgs($args);
  252. $message = __d($this->_validationDomain, $result, $args);
  253. } elseif (is_string($name)) {
  254. if (is_array($rule->rule)) {
  255. $args = array_slice($rule->rule, 1);
  256. $args = $this->_translateArgs($args);
  257. $message = __d($this->_validationDomain, $name, $args);
  258. } else {
  259. $message = __d($this->_validationDomain, $name);
  260. }
  261. } else {
  262. $message = __d('cake', 'This field cannot be left blank');
  263. }
  264. return $message;
  265. }
  266. /**
  267. * Applies translations to validator arguments.
  268. *
  269. * @param array $args The args to translate
  270. * @return array Translated args.
  271. */
  272. protected function _translateArgs($args) {
  273. foreach ((array)$args as $k => $arg) {
  274. if (is_string($arg)) {
  275. $args[$k] = __d($this->_validationDomain, $arg);
  276. }
  277. }
  278. return $args;
  279. }
  280. /**
  281. * Returns whether an index exists in the rule set
  282. *
  283. * @param string $index name of the rule
  284. * @return boolean
  285. */
  286. public function offsetExists($index) {
  287. return isset($this->_rules[$index]);
  288. }
  289. /**
  290. * Returns a rule object by its index
  291. *
  292. * @param string $index name of the rule
  293. * @return CakeValidationRule
  294. */
  295. public function offsetGet($index) {
  296. return $this->_rules[$index];
  297. }
  298. /**
  299. * Sets or replace a validation rule
  300. *
  301. * @param string $index name of the rule
  302. * @param CakeValidationRule|array rule to add to $index
  303. */
  304. public function offsetSet($index, $rule) {
  305. $this->setRule($index, $rule);
  306. }
  307. /**
  308. * Unsets a validation rule
  309. *
  310. * @param string $index name of the rule
  311. * @return void
  312. */
  313. public function offsetUnset($index) {
  314. unset($this->_rules[$index]);
  315. }
  316. /**
  317. * Returns an iterator for each of the rules to be applied
  318. *
  319. * @return ArrayIterator
  320. */
  321. public function getIterator() {
  322. return new ArrayIterator($this->_rules);
  323. }
  324. /**
  325. * Returns the number of rules in this set
  326. *
  327. * @return int
  328. */
  329. public function count() {
  330. return count($this->_rules);
  331. }
  332. }