CakeRule.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /**
  3. * CakeRule.
  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('CakeField', 'Model/Validator');
  23. App::uses('Validation', 'Utility');
  24. /**
  25. * CakeRule object.
  26. *
  27. * @package Cake.Model.Validator
  28. * @link http://book.cakephp.org/2.0/en/data-validation.html
  29. */
  30. class CakeRule {
  31. /**
  32. * Holds a reference to the parent field
  33. *
  34. * @var CakeField
  35. */
  36. protected $_field = null;
  37. /**
  38. * Has the required check failed?
  39. *
  40. * @var boolean
  41. */
  42. protected $_requiredFail = null;
  43. /**
  44. * The 'valid' value
  45. *
  46. * @var mixed
  47. */
  48. protected $_valid = true;
  49. /**
  50. * Holds the index under which the Validator was attached
  51. *
  52. * @var mixed
  53. */
  54. protected $_index = null;
  55. /**
  56. * Create or Update transaction?
  57. *
  58. * @var boolean
  59. */
  60. protected $_modelExists = null;
  61. /**
  62. * The parsed rule
  63. *
  64. * @var mixed
  65. */
  66. protected $_rule = null;
  67. /**
  68. * The parsed rule parameters
  69. *
  70. * @var array
  71. */
  72. protected $_ruleParams = array();
  73. /**
  74. * The errorMessage
  75. *
  76. * @var string
  77. */
  78. protected $_errorMessage = null;
  79. /**
  80. * Holds passed in options
  81. *
  82. * @var array
  83. */
  84. protected $_passedOptions = array();
  85. /**
  86. * Flag indicating wether the allowEmpty check has failed
  87. *
  88. * @var boolean
  89. */
  90. protected $_emptyFail = null;
  91. /**
  92. * The 'rule' key
  93. *
  94. * @var mixed
  95. */
  96. public $rule = 'blank';
  97. /**
  98. * The 'required' key
  99. *
  100. * @var mixed
  101. */
  102. public $required = null;
  103. /**
  104. * The 'allowEmpty' key
  105. *
  106. * @var boolean
  107. */
  108. public $allowEmpty = false;
  109. /**
  110. * The 'on' key
  111. *
  112. * @var string
  113. */
  114. public $on = null;
  115. /**
  116. * The 'last' key
  117. *
  118. * @var boolean
  119. */
  120. public $last = true;
  121. /**
  122. * The 'message' key
  123. *
  124. * @var string
  125. */
  126. public $message = null;
  127. /**
  128. * Constructor
  129. *
  130. * @param CakeField $field
  131. * @param array $validator [optional] The validator properties
  132. * @param mixed $index [optional]
  133. */
  134. public function __construct(CakeField $field, $validator = array(), $index = null) {
  135. $this->_field = $field;
  136. $this->_index = $index;
  137. unset($field, $index);
  138. $this->data = &$this->getField()
  139. ->data;
  140. $this->_modelExists = $this->getField()
  141. ->getValidator()
  142. ->getModel()
  143. ->exists();
  144. $this->_addValidatorProps($validator);
  145. unset($validator);
  146. }
  147. /**
  148. * Checks if the rule is valid
  149. *
  150. * @return boolean
  151. */
  152. public function isValid() {
  153. if (!$this->_valid || (is_string($this->_valid) && !empty($this->_valid))) {
  154. return false;
  155. }
  156. return true;
  157. }
  158. /**
  159. * Checks if the field is required by the 'required' value
  160. *
  161. * @return boolean
  162. */
  163. public function isRequired() {
  164. if (is_bool($this->required)) {
  165. return $this->required;
  166. }
  167. if (in_array($this->required, array('create', 'update'), true)) {
  168. if ($this->required === 'create' && !$this->_modelExists || $this->required === 'update' && $this->_modelExists) {
  169. $this->required = true;
  170. }
  171. }
  172. return $this->required;
  173. }
  174. /**
  175. * Checks if the field failed the required validation
  176. *
  177. * @return boolean
  178. */
  179. public function checkRequired() {
  180. if ($this->_requiredFail !== null) {
  181. return $this->_requiredFail;
  182. }
  183. $this->_requiredFail = (
  184. (!isset($this->data[$this->getField()->field]) && $this->required === true) ||
  185. (
  186. isset($this->data[$this->getField()->field]) && (empty($this->data[$this->getField()->field]) &&
  187. !is_numeric($this->data[$this->getField()->field])) && $this->allowEmpty === false
  188. )
  189. );
  190. return $this->_requiredFail;
  191. }
  192. /**
  193. * Checks if the allowEmpty key applies
  194. *
  195. * @return boolean
  196. */
  197. public function checkEmpty() {
  198. if ($this->_emptyFail !== null) {
  199. return $this->_emptyFail;
  200. }
  201. $this->_emptyFail = false;
  202. if (empty($this->data[$this->getField()->field]) && $this->data[$this->getField()->field] != '0' && $this->allowEmpty === true) {
  203. $this->_emptyFail = true;
  204. }
  205. return $this->_emptyFail;
  206. }
  207. /**
  208. * Checks if the Validation rule can be skipped
  209. *
  210. * @return boolean True if the ValidationRule can be skipped
  211. */
  212. public function skip() {
  213. if (!empty($this->on)) {
  214. if ($this->on == 'create' && $this->_modelExists || $this->on == 'update' && !$this->_modelExists) {
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. /**
  221. * Checks if the 'last' key is true
  222. *
  223. * @return boolean
  224. */
  225. public function isLast() {
  226. return (bool) $this->last;
  227. }
  228. /**
  229. * Gets the validation error message
  230. *
  231. * @return string
  232. */
  233. public function getMessage() {
  234. return $this->_processValidationResponse();
  235. }
  236. /**
  237. * Gets the parent field
  238. *
  239. * @return CakeField
  240. */
  241. public function getField() {
  242. return $this->_field;
  243. }
  244. /**
  245. * Gets an array with the rule properties
  246. *
  247. * @return array
  248. */
  249. public function getPropertiesArray() {
  250. return array(
  251. 'rule' => $this->rule,
  252. 'required' => $this->required,
  253. 'allowEmpty' => $this->allowEmpty,
  254. 'on' => $this->on,
  255. 'last' => $this->last,
  256. 'message' => $this->message
  257. );
  258. }
  259. /**
  260. * Dispatches the validation rule to the given validator method
  261. *
  262. * @return boolean True if the rule could be dispatched, false otherwise
  263. */
  264. public function dispatchValidation() {
  265. $this->_parseRule();
  266. $validator = $this->getPropertiesArray();
  267. $methods = $this->getField()->getValidator()->getMethods();
  268. $Model = $this->getField()->getValidator()->getModel();
  269. if (in_array(strtolower($this->_rule), $methods['model'])) {
  270. $this->_ruleParams[] = array_merge($validator, $this->_passedOptions);
  271. $this->_ruleParams[0] = array($this->getField()->field => $this->_ruleParams[0]);
  272. $this->_valid = $Model->dispatchMethod($this->_rule, $this->_ruleParams);
  273. } elseif (in_array($this->_rule, $methods['behaviors']) || in_array(strtolower($this->_rule), $methods['behaviors'])) {
  274. $this->_ruleParams[] = array_merge($validator, $this->_passedOptions);
  275. $this->_ruleParams[0] = array($this->getField()->field => $this->_ruleParams[0]);
  276. $this->_valid = $Model->Behaviors->dispatchMethod($Model, $this->_rule, $this->_ruleParams);
  277. } elseif (method_exists('Validation', $this->_rule)) {
  278. $this->_valid = call_user_func_array(array('Validation', $this->_rule), $this->_ruleParams);
  279. } elseif (!is_array($validator['rule'])) {
  280. $this->_valid = preg_match($this->_rule, $this->data[$this->getField()->field]);
  281. } elseif (Configure::read('debug') > 0) {
  282. trigger_error(__d('cake_dev', 'Could not find validation handler %s for %s', $this->_rule, $this->_field->field), E_USER_WARNING);
  283. return false;
  284. }
  285. unset($validator, $methods, $Model);
  286. return true;
  287. }
  288. /**
  289. * Fetches the correct error message for a failed validation
  290. *
  291. * @return string
  292. */
  293. protected function _processValidationResponse() {
  294. $validationDomain = $this->_field->getValidator()->validationDomain;
  295. if (is_string($this->_valid)) {
  296. $this->_errorMessage = $this->_valid;
  297. } elseif ($this->message !== null) {
  298. $args = null;
  299. if (is_array($this->message)) {
  300. $this->_errorMessage = $this->message[0];
  301. $args = array_slice($this->message, 1);
  302. } else {
  303. $this->_errorMessage = $this->message;
  304. }
  305. if (is_array($this->rule) && $args === null) {
  306. $args = array_slice($this->getField()->ruleSet[$this->_index]['rule'], 1);
  307. }
  308. if (!empty($args)) {
  309. foreach ($args as $k => $arg) {
  310. $args[$k] = __d($validationDomain, $arg);
  311. }
  312. }
  313. $this->_errorMessage = __d($validationDomain, $this->_errorMessage, $args);
  314. } elseif (is_string($this->_index)) {
  315. if (is_array($this->rule)) {
  316. $args = array_slice($this->getField()->ruleSet[$this->_index]['rule'], 1);
  317. if (!empty($args)) {
  318. foreach ($args as $k => $arg) {
  319. $args[$k] = __d($validationDomain, $arg);
  320. }
  321. }
  322. $this->_errorMessage = __d($validationDomain, $this->_index, $args);
  323. } else {
  324. $this->_errorMessage = __d($validationDomain, $this->_index);
  325. }
  326. } elseif (!$this->checkRequired() && is_numeric($this->_index) && count($this->getField()->ruleSet) > 1) {
  327. $this->_errorMessage = $this->_index + 1;
  328. } else {
  329. $this->_errorMessage = __d('cake_dev', 'This field cannot be left blank');
  330. }
  331. unset($validationDomain);
  332. return $this->_errorMessage;
  333. }
  334. /**
  335. * Sets the rule properties from the rule entry in validate
  336. *
  337. * @param array $validator [optional]
  338. * @return void
  339. */
  340. protected function _addValidatorProps($validator = array()) {
  341. if (!is_array($validator)) {
  342. $validator = array('rule' => $validator);
  343. }
  344. foreach ($validator as $key => $value) {
  345. if (isset($value) || !empty($value)) {
  346. if (in_array($key, array('rule', 'required', 'allowEmpty', 'on', 'message', 'last'))) {
  347. $this->$key = $validator[$key];
  348. } else {
  349. $this->_passedOptions[$key] = $value;
  350. }
  351. }
  352. }
  353. unset($validator);
  354. }
  355. /**
  356. * Parses the rule and sets the rule and ruleParams
  357. *
  358. * @return void
  359. */
  360. protected function _parseRule() {
  361. if (is_array($this->rule)) {
  362. $this->_rule = $this->rule[0];
  363. unset($this->rule[0]);
  364. $this->_ruleParams = array_merge(array($this->data[$this->getField()->field]), array_values($this->rule));
  365. } else {
  366. $this->_rule = $this->rule;
  367. $this->_ruleParams = array($this->data[$this->getField()->field]);
  368. }
  369. }
  370. }