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.3 API

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