CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CakeValidationRule
  • CakeValidationSet
  1: <?php
  2: /**
  3:  * CakeValidationRule.
  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: 
 22: App::uses('Validation', 'Utility');
 23: 
 24: /**
 25:  * CakeValidationRule object. Represents a validation method, error message and
 26:  * rules for applying such method to a field.
 27:  *
 28:  * @package       Cake.Model.Validator
 29:  * @link          http://book.cakephp.org/2.0/en/data-validation.html
 30:  */
 31: class CakeValidationRule {
 32: 
 33: /**
 34:  * Whether the field passed this validation rule
 35:  *
 36:  * @var mixed
 37:  */
 38:     protected $_valid = true;
 39: 
 40: /**
 41:  * Holds whether the record being validated exists in datasource or not
 42:  *
 43:  * @var boolean
 44:  */
 45:     protected $_recordExists = false;
 46: 
 47: /**
 48:  * Validation method
 49:  *
 50:  * @var mixed
 51:  */
 52:     protected $_rule = null;
 53: 
 54: /**
 55:  * Validation method arguments
 56:  *
 57:  * @var array
 58:  */
 59:     protected $_ruleParams = array();
 60: 
 61: /**
 62:  * Holds passed in options
 63:  *
 64:  * @var array
 65:  */
 66:     protected $_passedOptions = array();
 67: 
 68: /**
 69:  * The 'rule' key
 70:  *
 71:  * @var mixed
 72:  */
 73:     public $rule = 'blank';
 74: 
 75: /**
 76:  * The 'required' key
 77:  *
 78:  * @var mixed
 79:  */
 80:     public $required = null;
 81: 
 82: /**
 83:  * The 'allowEmpty' key
 84:  *
 85:  * @var boolean
 86:  */
 87:     public $allowEmpty = null;
 88: 
 89: /**
 90:  * The 'on' key
 91:  *
 92:  * @var string
 93:  */
 94:     public $on = null;
 95: 
 96: /**
 97:  * The 'last' key
 98:  *
 99:  * @var boolean
100:  */
101:     public $last = true;
102: 
103: /**
104:  * The 'message' key
105:  *
106:  * @var string
107:  */
108:     public $message = null;
109: 
110: /**
111:  * Constructor
112:  *
113:  * @param array $validator [optional] The validator properties
114:  */
115:     public function __construct($validator = array()) {
116:         $this->_addValidatorProps($validator);
117:     }
118: 
119: /**
120:  * Checks if the rule is valid
121:  *
122:  * @return boolean
123:  */
124:     public function isValid() {
125:         if (!$this->_valid || (is_string($this->_valid) && !empty($this->_valid))) {
126:             return false;
127:         }
128: 
129:         return true;
130:     }
131: 
132: /**
133:  * Returns whether the field can be left blank according to this rule
134:  *
135:  * @return boolean
136:  */
137:     public function isEmptyAllowed() {
138:         return $this->skip() || $this->allowEmpty === true;
139:     }
140: 
141: /**
142:  * Checks if the field is required according to the `required` property
143:  *
144:  * @return boolean
145:  */
146:     public function isRequired() {
147:         if (in_array($this->required, array('create', 'update'), true)) {
148:             if ($this->required === 'create' && !$this->isUpdate() || $this->required === 'update' && $this->isUpdate()) {
149:                 return true;
150:             } else {
151:                 return false;
152:             }
153:         }
154: 
155:         return $this->required;
156:     }
157: 
158: /**
159:  * Checks whether the field failed the `field should be present` validation
160:  *
161:  * @param array $data data to check rule against
162:  * @return boolean
163:  */
164:     public function checkRequired($field, &$data) {
165:         return (
166:             (!array_key_exists($field, $data) && $this->isRequired() === true) ||
167:             (
168:                 array_key_exists($field, $data) && (empty($data[$field]) &&
169:                 !is_numeric($data[$field])) && $this->allowEmpty === false
170:             )
171:         );
172:     }
173: 
174: /**
175:  * Checks if the allowEmpty key applies
176:  *
177:  * @param array $data data to check rule against
178:  * @return boolean
179:  */
180:     public function checkEmpty($field, &$data) {
181:         if (empty($data[$field]) && $data[$field] != '0' && $this->allowEmpty === true) {
182:             return true;
183:         }
184:         return false;
185:     }
186: 
187: /**
188:  * Checks if the validation rule should be skipped
189:  *
190:  * @return boolean True if the ValidationRule can be skipped
191:  */
192:     public function skip() {
193:         if (!empty($this->on)) {
194:             if ($this->on == 'create' && $this->isUpdate() || $this->on == 'update' && !$this->isUpdate()) {
195:                 return true;
196:             }
197:         }
198:         return false;
199:     }
200: 
201: /**
202:  * Returns whethere this rule should break validation process for associated field
203:  * after it fails
204:  *
205:  * @return boolean
206:  */
207:     public function isLast() {
208:         return (bool)$this->last;
209:     }
210: 
211: /**
212:  * Gets the validation error message
213:  *
214:  * @return string
215:  */
216:     public function getValidationResult() {
217:         return $this->_valid;
218:     }
219: 
220: /**
221:  * Gets an array with the rule properties
222:  *
223:  * @return array
224:  */
225:     protected function _getPropertiesArray() {
226:         $rule = $this->rule;
227:         if (!is_string($rule)) {
228:             unset($rule[0]);
229:         }
230:         return array(
231:             'rule' => $rule,
232:             'required' => $this->required,
233:             'allowEmpty' => $this->allowEmpty,
234:             'on' => $this->on,
235:             'last' => $this->last,
236:             'message' => $this->message
237:         );
238:     }
239: 
240: /**
241:  * Sets the recordExists configuration value for this rule,
242:  * ir refers to wheter the model record it is validating exists
243:  * exists in the collection or not (create or update operation)
244:  *
245:  * If called with no parameters it will return whether this rule
246:  * is configured for update operations or not.
247:  *
248:  * @return boolean
249:  **/
250:     public function isUpdate($exists = null) {
251:         if ($exists === null) {
252:             return $this->_recordExists;
253:         }
254:         return $this->_recordExists = $exists;
255:     }
256: 
257: /**
258:  * Dispatches the validation rule to the given validator method
259:  *
260:  * @return boolean True if the rule could be dispatched, false otherwise
261:  */
262:     public function process($field, &$data, &$methods) {
263:         $this->_valid = true;
264:         $this->_parseRule($field, $data);
265: 
266:         $validator = $this->_getPropertiesArray();
267:         $rule = strtolower($this->_rule);
268:         if (isset($methods[$rule])) {
269:             $this->_ruleParams[] = array_merge($validator, $this->_passedOptions);
270:             $this->_ruleParams[0] = array($field => $this->_ruleParams[0]);
271:             $this->_valid = call_user_func_array($methods[$rule], $this->_ruleParams);
272:         } elseif (class_exists('Validation') && method_exists('Validation', $this->_rule)) {
273:             $this->_valid = call_user_func_array(array('Validation', $this->_rule), $this->_ruleParams);
274:         } elseif (is_string($validator['rule'])) {
275:             $this->_valid = preg_match($this->_rule, $data[$field]);
276:         } elseif (Configure::read('debug') > 0) {
277:             trigger_error(__d('cake_dev', 'Could not find validation handler %s for %s', $this->_rule, $field), E_USER_WARNING);
278:             return false;
279:         }
280: 
281:         return true;
282:     }
283: 
284: /**
285:  * Resets interal state for this rule, by default it will become valid
286:  * and it will set isUpdate() to false
287:  *
288:  * @return void
289:  **/
290:     public function reset() {
291:         $this->_valid = true;
292:         $this->_recordExists = false;
293:     }
294: 
295: /**
296:  * Returns passed options for this rule
297:  *
298:  * @return array
299:  **/
300:     public function getOptions($key) {
301:         if (!isset($this->_passedOptions[$key])) {
302:             return null;
303:         }
304:         return $this->_passedOptions[$key];
305:     }
306: 
307: /**
308:  * Sets the rule properties from the rule entry in validate
309:  *
310:  * @param array $validator [optional]
311:  * @return void
312:  */
313:     protected function _addValidatorProps($validator = array()) {
314:         if (!is_array($validator)) {
315:             $validator = array('rule' => $validator);
316:         }
317:         foreach ($validator as $key => $value) {
318:             if (isset($value) || !empty($value)) {
319:                 if (in_array($key, array('rule', 'required', 'allowEmpty', 'on', 'message', 'last'))) {
320:                     $this->{$key} = $validator[$key];
321:                 } else {
322:                     $this->_passedOptions[$key] = $value;
323:                 }
324:             }
325:         }
326:     }
327: 
328: /**
329:  * Parses the rule and sets the rule and ruleParams
330:  *
331:  * @return void
332:  */
333:     protected function _parseRule($field, &$data) {
334:         if (is_array($this->rule)) {
335:             $this->_rule = $this->rule[0];
336:             $this->_ruleParams = array_merge(array($data[$field]), array_values(array_slice($this->rule, 1)));
337:         } else {
338:             $this->_rule = $this->rule;
339:             $this->_ruleParams = array($data[$field]);
340:         }
341:     }
342: 
343: }
344: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs