CakeField.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * ModelValidator.
  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. * CakeField object.
  25. *
  26. * @package Cake.Model.Validator
  27. * @link http://book.cakephp.org/2.0/en/data-validation.html
  28. */
  29. class CakeField {
  30. /**
  31. * Holds the parent Validator instance
  32. *
  33. * @var ModelValidator
  34. */
  35. protected $_validator = null;
  36. /**
  37. * Holds the ValidationRule objects
  38. *
  39. * @var array
  40. */
  41. protected $_rules = array();
  42. /**
  43. * If the validation is stopped
  44. *
  45. * @var boolean
  46. */
  47. public $isStopped = false;
  48. /**
  49. * Holds the fieldname
  50. *
  51. * @var string
  52. */
  53. public $field = null;
  54. /**
  55. * Holds the original ruleSet
  56. *
  57. * @var array
  58. */
  59. public $ruleSet = array();
  60. /**
  61. * Constructor
  62. *
  63. * @param ModelValidator $validator The parent ModelValidator
  64. * @param string $fieldName The fieldname
  65. * @param
  66. */
  67. public function __construct(ModelValidator $validator, $fieldName, $ruleSet) {
  68. $this->_validator = $validator;
  69. $this->data = &$this->getValidator()->data;
  70. $this->field = $fieldName;
  71. if (!is_array($ruleSet) || (is_array($ruleSet) && isset($ruleSet['rule']))) {
  72. $ruleSet = array($ruleSet);
  73. }
  74. foreach ($ruleSet as $index => $validateProp) {
  75. $this->_rules[$index] = new CakeRule($this, $validateProp, $index);
  76. }
  77. $this->ruleSet = $ruleSet;
  78. unset($ruleSet, $validateProp);
  79. }
  80. /**
  81. * Validates a ModelField
  82. *
  83. * @return mixed
  84. */
  85. public function validate() {
  86. foreach ($this->getRules() as $rule) {
  87. if ($rule->skip()) {
  88. continue;
  89. }
  90. $rule->isRequired();
  91. if (!$rule->checkRequired() && array_key_exists($this->field, $this->data)) {
  92. if ($rule->checkEmpty()) {
  93. break;
  94. }
  95. $rule->dispatchValidation();
  96. }
  97. if ($rule->checkRequired() || !$rule->isValid()) {
  98. $this->getValidator()->invalidate($this->field, $rule->getMessage());
  99. if ($rule->isLast()) {
  100. return false;
  101. }
  102. }
  103. }
  104. return true;
  105. }
  106. /**
  107. * Gets a rule for a certain index
  108. *
  109. * @param mixed index
  110. * @return ValidationRule
  111. */
  112. public function getRule($index) {
  113. if (!empty($this->_rules[$index])) {
  114. return $this->_rules[$index];
  115. }
  116. }
  117. /**
  118. * Gets all rules for this ModelField
  119. *
  120. * @return array
  121. */
  122. public function getRules() {
  123. return $this->_rules;
  124. }
  125. /**
  126. * Sets a ValidationRule $rule for key $key
  127. *
  128. * @param mixed $key The key under which the rule should be set
  129. * @param ValidationRule $rule The ValidationRule to be set
  130. * @return ModelField
  131. */
  132. public function setRule($key, CakeRule $rule) {
  133. $this->_rules[$key] = $rule;
  134. return $this;
  135. }
  136. /**
  137. * Sets the rules for a given field
  138. *
  139. * @param array $rules The rules to be set
  140. * @param bolean $mergeVars [optional] If true, merges vars instead of replace. Defaults to true.
  141. * @return ModelField
  142. */
  143. public function setRules($rules = array(), $mergeVars = true) {
  144. if ($mergeVars === false) {
  145. $this->_rules = $rules;
  146. } else {
  147. $this->_rules = array_merge($this->_rules, $rules);
  148. }
  149. return $this;
  150. }
  151. /**
  152. * Gets the validator this field is atached to
  153. *
  154. * @return ModelValidator The parent ModelValidator instance
  155. */
  156. public function getValidator() {
  157. return $this->_validator;
  158. }
  159. /**
  160. * Magic isset
  161. *
  162. * @return true if the field exists in data, false otherwise
  163. */
  164. public function __isset($fieldName) {
  165. return array_key_exists($fieldName, $this->getValidator()->getData());
  166. }
  167. }