2012-03-22 11:18:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ModelValidator.
|
|
|
|
*
|
|
|
|
* Provides the Model validation logic.
|
|
|
|
*
|
|
|
|
* PHP versions 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-05-11 01:57:01 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-05-11 01:57:01 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-03-22 11:18:57 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @package Cake.Model
|
2012-04-23 11:02:26 +00:00
|
|
|
* @since CakePHP(tm) v 2.2.0
|
2012-03-22 11:18:57 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2012-05-05 20:22:02 +00:00
|
|
|
|
2012-05-05 18:13:40 +00:00
|
|
|
App::uses('CakeValidationSet', 'Model/Validator');
|
2012-03-22 11:18:57 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-11 01:57:01 +00:00
|
|
|
* ModelValidator object encapsulates all methods related to data validations for a model
|
|
|
|
* It also provides an API to dynamically change validation rules for each model field.
|
|
|
|
*
|
|
|
|
* Implements ArrayAccess to easily modify rules as usually done with `Model::$validate`
|
|
|
|
* definition array
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Model
|
|
|
|
* @link http://book.cakephp.org/2.0/en/data-validation.html
|
|
|
|
*/
|
2012-05-05 22:16:09 +00:00
|
|
|
class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
|
2012-03-22 11:18:57 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-05 18:13:40 +00:00
|
|
|
* Holds the CakeValidationSet objects array
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_fields = array();
|
|
|
|
|
|
|
|
/**
|
2012-05-11 01:57:01 +00:00
|
|
|
* Holds the reference to the model this Validator is attached to
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* @var Model
|
|
|
|
*/
|
|
|
|
protected $_model = array();
|
|
|
|
|
|
|
|
/**
|
2012-05-11 01:57:01 +00:00
|
|
|
* The validators $validate property, used for checking wheter validation
|
|
|
|
* rules definition changed in the model and should be refreshed in this class
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_validate = array();
|
|
|
|
|
|
|
|
/**
|
2012-05-11 01:57:01 +00:00
|
|
|
* Holds the available custom callback methods, usually taken from model methods
|
|
|
|
* and behavior methods
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_methods = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param Model $Model A reference to the Model the Validator is attached to
|
|
|
|
*/
|
|
|
|
public function __construct(Model $Model) {
|
|
|
|
$this->_model = $Model;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if all fields pass validation. Will validate hasAndBelongsToMany associations
|
2012-05-11 01:57:01 +00:00
|
|
|
* that use the 'with' key as well. Since `Model::_saveMulti` is incapable of exiting a save operation.
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
2012-05-11 01:57:01 +00:00
|
|
|
* Will validate the currently set data. Use `Model::set()` or `Model::create()` to set the active data.
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* @param array $options An optional array of custom options to be made available in the beforeValidate callback
|
|
|
|
* @return boolean True if there are no errors
|
|
|
|
*/
|
|
|
|
public function validates($options = array()) {
|
2012-05-05 18:04:14 +00:00
|
|
|
$errors = $this->errors($options);
|
2012-03-22 11:18:57 +00:00
|
|
|
if (empty($errors) && $errors !== false) {
|
|
|
|
$errors = $this->_validateWithModels($options);
|
|
|
|
}
|
|
|
|
if (is_array($errors)) {
|
|
|
|
return count($errors) === 0;
|
|
|
|
}
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a single record, as well as all its directly associated records.
|
|
|
|
*
|
|
|
|
* #### Options
|
|
|
|
*
|
|
|
|
* - atomic: If true (default), returns boolean. If false returns array.
|
|
|
|
* - fieldList: Equivalent to the $fieldList parameter in Model::save()
|
|
|
|
* - deep: If set to true, not only directly associated data , but deeper nested associated data is validated as well.
|
|
|
|
*
|
2012-05-11 03:06:55 +00:00
|
|
|
* Warning: This method could potentially change the passed argument `$data`,
|
|
|
|
* If you do not want this to happen, make a copy of `$data` before passing it
|
|
|
|
* to this method
|
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @param array $data Record data to validate. This should be an array indexed by association name.
|
|
|
|
* @param array $options Options to use when validating record data (see above), See also $options of validates().
|
|
|
|
* @return array|boolean If atomic: True on success, or false on failure.
|
|
|
|
* Otherwise: array similar to the $data array passed, but values are set to true/false
|
|
|
|
* depending on whether each record validated successfully.
|
|
|
|
*/
|
2012-05-11 03:06:55 +00:00
|
|
|
public function validateAssociated(&$data, $options = array()) {
|
2012-04-29 03:55:05 +00:00
|
|
|
$model = $this->getModel();
|
2012-04-29 04:54:35 +00:00
|
|
|
$options = array_merge(array('atomic' => true, 'deep' => false), $options);
|
|
|
|
$model->validationErrors = $validationErrors = $return = array();
|
2012-05-19 20:09:44 +00:00
|
|
|
$model->create(null);
|
|
|
|
if (!($model->set($data) && $model->validates($options))) {
|
2012-04-29 04:54:35 +00:00
|
|
|
$validationErrors[$model->alias] = $model->validationErrors;
|
2012-04-29 03:55:05 +00:00
|
|
|
$return[$model->alias] = false;
|
2012-03-22 11:18:57 +00:00
|
|
|
} else {
|
2012-04-29 03:55:05 +00:00
|
|
|
$return[$model->alias] = true;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
2012-05-19 20:09:44 +00:00
|
|
|
$data = $model->data;
|
2012-05-20 00:29:32 +00:00
|
|
|
if (!empty($options['deep']) && isset($data[$model->alias])) {
|
|
|
|
$recordData = $data[$model->alias];
|
|
|
|
unset($data[$model->alias]);
|
|
|
|
$data = array_merge($data, $recordData);
|
|
|
|
}
|
2012-05-11 03:06:55 +00:00
|
|
|
|
2012-04-29 03:55:05 +00:00
|
|
|
$associations = $model->getAssociated();
|
2012-05-11 03:06:55 +00:00
|
|
|
foreach ($data as $association => &$values) {
|
2012-03-22 11:18:57 +00:00
|
|
|
$validates = true;
|
|
|
|
if (isset($associations[$association])) {
|
|
|
|
if (in_array($associations[$association], array('belongsTo', 'hasOne'))) {
|
|
|
|
if ($options['deep']) {
|
2012-04-29 04:54:35 +00:00
|
|
|
$validates = $model->{$association}->validateAssociated($values, $options);
|
2012-03-22 11:18:57 +00:00
|
|
|
} else {
|
2012-06-06 15:36:06 +00:00
|
|
|
$model->{$association}->create(null);
|
|
|
|
$validates = $model->{$association}->set($values) && $model->{$association}->validates($options);
|
|
|
|
$data[$association] = $model->{$association}->data[$model->{$association}->alias];
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
if (is_array($validates)) {
|
|
|
|
if (in_array(false, $validates, true)) {
|
|
|
|
$validates = false;
|
|
|
|
} else {
|
|
|
|
$validates = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$return[$association] = $validates;
|
|
|
|
} elseif ($associations[$association] === 'hasMany') {
|
2012-04-29 04:54:35 +00:00
|
|
|
$validates = $model->{$association}->validateMany($values, $options);
|
2012-03-22 11:18:57 +00:00
|
|
|
$return[$association] = $validates;
|
|
|
|
}
|
|
|
|
if (!$validates || (is_array($validates) && in_array(false, $validates, true))) {
|
2012-04-29 04:54:35 +00:00
|
|
|
$validationErrors[$association] = $model->{$association}->validationErrors;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 04:54:35 +00:00
|
|
|
$model->validationErrors = $validationErrors;
|
|
|
|
if (isset($validationErrors[$model->alias])) {
|
|
|
|
$model->validationErrors = $validationErrors[$model->alias];
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
if (!$options['atomic']) {
|
|
|
|
return $return;
|
|
|
|
}
|
2012-04-29 04:54:35 +00:00
|
|
|
if ($return[$model->alias] === false || !empty($model->validationErrors)) {
|
2012-03-22 11:18:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates multiple individual records for a single model
|
|
|
|
*
|
|
|
|
* #### Options
|
|
|
|
*
|
|
|
|
* - atomic: If true (default), returns boolean. If false returns array.
|
|
|
|
* - fieldList: Equivalent to the $fieldList parameter in Model::save()
|
|
|
|
* - deep: If set to true, all associated data will be validated as well.
|
|
|
|
*
|
2012-05-11 03:06:55 +00:00
|
|
|
* Warning: This method could potentially change the passed argument `$data`,
|
|
|
|
* If you do not want this to happen, make a copy of `$data` before passing it
|
|
|
|
* to this method
|
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @param array $data Record data to validate. This should be a numerically-indexed array
|
|
|
|
* @param array $options Options to use when validating record data (see above), See also $options of validates().
|
|
|
|
* @return boolean True on success, or false on failure.
|
|
|
|
* @return mixed If atomic: True on success, or false on failure.
|
|
|
|
* Otherwise: array similar to the $data array passed, but values are set to true/false
|
|
|
|
* depending on whether each record validated successfully.
|
|
|
|
*/
|
2012-05-11 03:06:55 +00:00
|
|
|
public function validateMany(&$data, $options = array()) {
|
2012-04-29 03:55:05 +00:00
|
|
|
$model = $this->getModel();
|
2012-04-29 04:54:35 +00:00
|
|
|
$options = array_merge(array('atomic' => true, 'deep' => false), $options);
|
|
|
|
$model->validationErrors = $validationErrors = $return = array();
|
2012-05-11 03:06:55 +00:00
|
|
|
foreach ($data as $key => &$record) {
|
2012-03-22 11:18:57 +00:00
|
|
|
if ($options['deep']) {
|
2012-04-29 04:54:35 +00:00
|
|
|
$validates = $model->validateAssociated($record, $options);
|
2012-03-22 11:18:57 +00:00
|
|
|
} else {
|
2012-05-19 20:09:44 +00:00
|
|
|
$model->create(null);
|
|
|
|
$validates = $model->set($record) && $model->validates($options);
|
2012-05-11 03:06:55 +00:00
|
|
|
$data[$key] = $model->data;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
if ($validates === false || (is_array($validates) && in_array(false, $validates, true))) {
|
2012-04-29 04:54:35 +00:00
|
|
|
$validationErrors[$key] = $model->validationErrors;
|
2012-03-22 11:18:57 +00:00
|
|
|
$validates = false;
|
|
|
|
} else {
|
|
|
|
$validates = true;
|
|
|
|
}
|
|
|
|
$return[$key] = $validates;
|
|
|
|
}
|
2012-04-29 04:54:35 +00:00
|
|
|
$model->validationErrors = $validationErrors;
|
2012-03-22 11:18:57 +00:00
|
|
|
if (!$options['atomic']) {
|
|
|
|
return $return;
|
|
|
|
}
|
2012-04-29 04:54:35 +00:00
|
|
|
if (empty($model->validationErrors)) {
|
2012-03-22 11:18:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 01:57:01 +00:00
|
|
|
* Returns an array of fields that have failed validation. On the current model. This method will
|
|
|
|
* actually run validation rules over data, not just return the messages.
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* @param string $options An optional array of custom options to be made available in the beforeValidate callback
|
|
|
|
* @return array Array of invalid fields
|
2012-05-11 01:57:01 +00:00
|
|
|
* @see ModelValidator::validates()
|
2012-03-22 11:18:57 +00:00
|
|
|
*/
|
2012-05-05 18:04:14 +00:00
|
|
|
public function errors($options = array()) {
|
|
|
|
if (!$this->_triggerBeforeValidate($options)) {
|
2012-03-22 11:18:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-29 00:08:18 +00:00
|
|
|
$model = $this->getModel();
|
2012-03-22 11:18:57 +00:00
|
|
|
|
2012-04-29 03:55:05 +00:00
|
|
|
if (!$this->_parseRules()) {
|
2012-04-29 04:54:35 +00:00
|
|
|
return $model->validationErrors;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
2012-05-05 18:04:14 +00:00
|
|
|
$fieldList = isset($options['fieldList']) ? $options['fieldList'] : array();
|
2012-04-29 03:55:05 +00:00
|
|
|
$exists = $model->exists();
|
|
|
|
$methods = $this->getMethods();
|
2012-05-05 18:04:14 +00:00
|
|
|
$fields = $this->_validationList($fieldList);
|
|
|
|
|
|
|
|
foreach ($fields as $field) {
|
2012-04-29 03:55:05 +00:00
|
|
|
$field->setMethods($methods);
|
|
|
|
$field->setValidationDomain($model->validationDomain);
|
2012-04-29 04:54:35 +00:00
|
|
|
$data = isset($model->data[$model->alias]) ? $model->data[$model->alias] : array();
|
|
|
|
$errors = $field->validate($data, $exists);
|
2012-04-29 03:55:05 +00:00
|
|
|
foreach ($errors as $error) {
|
|
|
|
$this->invalidate($field->field, $error);
|
|
|
|
}
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
2012-05-07 04:08:29 +00:00
|
|
|
$model->getEventManager()->dispatch(new CakeEvent('Model.afterValidate', $model));
|
2012-04-29 04:54:35 +00:00
|
|
|
return $model->validationErrors;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 01:57:01 +00:00
|
|
|
* Marks a field as invalid, optionally setting a message explaining
|
|
|
|
* why the rule failed
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* @param string $field The name of the field to invalidate
|
2012-05-11 01:57:01 +00:00
|
|
|
* @param string $message Validation message explaining why the rule failed, defaults to true.
|
2012-03-22 11:18:57 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-05-11 01:57:01 +00:00
|
|
|
public function invalidate($field, $message = true) {
|
2012-05-11 02:34:55 +00:00
|
|
|
$this->getModel()->validationErrors[$field][] = $message;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 01:57:01 +00:00
|
|
|
* Gets all possible custom methods from the Model and attached Behaviors
|
|
|
|
* to be used as validators
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
2012-05-11 01:57:01 +00:00
|
|
|
* @return array List of callables to be used as validation methods
|
2012-03-22 11:18:57 +00:00
|
|
|
*/
|
2012-04-29 04:54:35 +00:00
|
|
|
public function getMethods() {
|
2012-03-22 11:18:57 +00:00
|
|
|
if (!empty($this->_methods)) {
|
|
|
|
return $this->_methods;
|
|
|
|
}
|
|
|
|
|
2012-04-29 04:54:35 +00:00
|
|
|
$methods = array();
|
2012-04-29 05:09:43 +00:00
|
|
|
foreach (get_class_methods($this->_model) as $method) {
|
2012-04-29 04:54:35 +00:00
|
|
|
$methods[strtolower($method)] = array($this->_model, $method);
|
|
|
|
}
|
2012-03-22 11:18:57 +00:00
|
|
|
|
2012-05-07 01:15:50 +00:00
|
|
|
foreach (array_keys($this->_model->Behaviors->methods()) as $method) {
|
2012-04-29 04:54:35 +00:00
|
|
|
$methods += array(strtolower($method) => array($this->_model, $method));
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
2012-04-29 04:54:35 +00:00
|
|
|
return $this->_methods = $methods;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 01:57:01 +00:00
|
|
|
* Returns a CakeValidationSet object containing all validation rules for a field, if no
|
|
|
|
* params are passed then it returns an array with all CakeValidationSet objects for each field
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* @param string $name [optional] The fieldname to fetch. Defaults to null.
|
2012-05-11 01:57:01 +00:00
|
|
|
* @return CakeValidationSet|array
|
2012-03-22 11:18:57 +00:00
|
|
|
*/
|
2012-05-11 01:57:01 +00:00
|
|
|
public function getField($name = null) {
|
2012-03-22 11:18:57 +00:00
|
|
|
if ($name !== null && !empty($this->_fields[$name])) {
|
|
|
|
return $this->_fields[$name];
|
2012-05-20 19:51:09 +00:00
|
|
|
} elseif ($name !== null) {
|
2012-05-11 02:34:55 +00:00
|
|
|
return null;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
return $this->_fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 01:57:01 +00:00
|
|
|
* Sets the CakeValidationSet objects from the `Model::$validate` property
|
|
|
|
* If `Model::$validate` is not set or empty, this method returns false. True otherwise.
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
2012-05-11 01:57:01 +00:00
|
|
|
* @return boolean true if `Model::$validate` was processed, false otherwise
|
2012-03-22 11:18:57 +00:00
|
|
|
*/
|
2012-04-29 03:55:05 +00:00
|
|
|
protected function _parseRules() {
|
2012-05-05 20:22:02 +00:00
|
|
|
if ($this->_validate === $this->_model->validate) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-29 03:55:05 +00:00
|
|
|
if (empty($this->_model->validate)) {
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->_validate = array();
|
2012-04-29 03:55:05 +00:00
|
|
|
$this->_fields = array();
|
2012-03-22 11:18:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-29 03:55:05 +00:00
|
|
|
$this->_validate = $this->_model->validate;
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->_fields = array();
|
2012-04-29 03:55:05 +00:00
|
|
|
$methods = $this->getMethods();
|
2012-03-22 11:18:57 +00:00
|
|
|
foreach ($this->_validate as $fieldName => $ruleSet) {
|
2012-05-05 18:13:40 +00:00
|
|
|
$this->_fields[$fieldName] = new CakeValidationSet($fieldName, $ruleSet, $methods);
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the I18n domain for validation messages. This method is chainable.
|
|
|
|
*
|
2012-04-29 03:55:05 +00:00
|
|
|
* @param string $validationDomain [optional] The validation domain to be used.
|
2012-03-22 11:18:57 +00:00
|
|
|
* @return ModelValidator
|
|
|
|
*/
|
2012-04-29 21:48:33 +00:00
|
|
|
public function setValidationDomain($validationDomain = null) {
|
|
|
|
if (empty($validationDomain)) {
|
|
|
|
$validationDomain = 'default';
|
|
|
|
}
|
|
|
|
$this->getModel()->validationDomain = $validationDomain;
|
2012-03-22 11:18:57 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 01:57:01 +00:00
|
|
|
* Gets the model related to this validator
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* @return Model
|
|
|
|
*/
|
|
|
|
public function getModel() {
|
|
|
|
return $this->_model;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 01:57:01 +00:00
|
|
|
* Processes the Model's whitelist or passed fieldList and returns the list of fields
|
2012-05-05 18:04:14 +00:00
|
|
|
* to be validated
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
2012-05-11 01:57:01 +00:00
|
|
|
* @param array $fieldList list of fields to be used for validation
|
2012-05-05 18:04:14 +00:00
|
|
|
* @return array List of validation rules to be applied
|
2012-03-22 11:18:57 +00:00
|
|
|
*/
|
2012-05-05 18:04:14 +00:00
|
|
|
protected function _validationList($fieldList = array()) {
|
2012-04-29 03:55:05 +00:00
|
|
|
$model = $this->getModel();
|
|
|
|
$whitelist = $model->whitelist;
|
2012-03-22 11:18:57 +00:00
|
|
|
|
|
|
|
if (!empty($fieldList)) {
|
2012-04-29 03:55:05 +00:00
|
|
|
if (!empty($fieldList[$model->alias]) && is_array($fieldList[$model->alias])) {
|
|
|
|
$whitelist = $fieldList[$model->alias];
|
2012-03-22 11:18:57 +00:00
|
|
|
} else {
|
|
|
|
$whitelist = $fieldList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($fieldList);
|
|
|
|
|
2012-05-05 18:04:14 +00:00
|
|
|
$validateList = array();
|
2012-03-22 11:18:57 +00:00
|
|
|
if (!empty($whitelist)) {
|
|
|
|
$this->validationErrors = array();
|
2012-05-05 18:04:14 +00:00
|
|
|
|
2012-04-29 00:08:18 +00:00
|
|
|
foreach ((array)$whitelist as $f) {
|
2012-05-05 18:04:14 +00:00
|
|
|
if (!empty($this->_fields[$f])) {
|
|
|
|
$validateList[$f] = $this->_fields[$f];
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-05 18:04:14 +00:00
|
|
|
} else {
|
|
|
|
return $this->_fields;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
2012-05-05 18:04:14 +00:00
|
|
|
|
|
|
|
return $validateList;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs validation for hasAndBelongsToMany associations that have 'with' keys
|
2012-05-11 01:57:01 +00:00
|
|
|
* set and data in the data set.
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* @param array $options Array of options to use on Validation of with models
|
|
|
|
* @return boolean Failure of validation on with models.
|
|
|
|
* @see Model::validates()
|
|
|
|
*/
|
|
|
|
protected function _validateWithModels($options) {
|
|
|
|
$valid = true;
|
2012-04-29 03:55:05 +00:00
|
|
|
$model = $this->getModel();
|
2012-03-22 11:18:57 +00:00
|
|
|
|
2012-04-29 03:55:05 +00:00
|
|
|
foreach ($model->hasAndBelongsToMany as $assoc => $association) {
|
|
|
|
if (empty($association['with']) || !isset($model->data[$assoc])) {
|
2012-03-22 11:18:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-04-29 03:55:05 +00:00
|
|
|
list($join) = $model->joinModel($model->hasAndBelongsToMany[$assoc]['with']);
|
|
|
|
$data = $model->data[$assoc];
|
2012-03-22 11:18:57 +00:00
|
|
|
|
|
|
|
$newData = array();
|
|
|
|
foreach ((array)$data as $row) {
|
2012-04-29 03:55:05 +00:00
|
|
|
if (isset($row[$model->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
|
2012-03-22 11:18:57 +00:00
|
|
|
$newData[] = $row;
|
2012-04-29 03:55:05 +00:00
|
|
|
} elseif (isset($row[$join]) && isset($row[$join][$model->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
|
2012-03-22 11:18:57 +00:00
|
|
|
$newData[] = $row[$join];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($newData)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($newData as $data) {
|
2012-04-29 03:55:05 +00:00
|
|
|
$data[$model->hasAndBelongsToMany[$assoc]['foreignKey']] = $model->id;
|
|
|
|
$model->{$join}->create($data);
|
|
|
|
$valid = ($valid && $model->{$join}->validator()->validates($options));
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 01:57:01 +00:00
|
|
|
* Propagates beforeValidate event
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-05-05 18:04:14 +00:00
|
|
|
protected function _triggerBeforeValidate($options = array()) {
|
2012-04-29 03:55:05 +00:00
|
|
|
$model = $this->getModel();
|
|
|
|
$event = new CakeEvent('Model.beforeValidate', $model, array($options));
|
2012-03-22 11:18:57 +00:00
|
|
|
list($event->break, $event->breakOn) = array(true, false);
|
2012-04-29 03:55:05 +00:00
|
|
|
$model->getEventManager()->dispatch($event);
|
2012-03-22 11:18:57 +00:00
|
|
|
if ($event->isStopped()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-05 20:22:02 +00:00
|
|
|
/**
|
|
|
|
* Returns wheter a rule set is defined for a field or not
|
|
|
|
*
|
|
|
|
* @param string $field name of the field to check
|
|
|
|
* @return boolean
|
|
|
|
**/
|
|
|
|
public function offsetExists($field) {
|
|
|
|
$this->_parseRules();
|
|
|
|
return isset($this->_fields[$field]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the rule set for a field
|
|
|
|
*
|
|
|
|
* @param string $field name of the field to check
|
|
|
|
* @return CakeValidationSet
|
|
|
|
**/
|
|
|
|
public function offsetGet($field) {
|
|
|
|
$this->_parseRules();
|
|
|
|
return $this->_fields[$field];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the rule set for a field
|
|
|
|
*
|
|
|
|
* @param string $field name of the field to set
|
|
|
|
* @param array|CakeValidationSet $rules set of rules to apply to field
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
public function offsetSet($field, $rules) {
|
|
|
|
$this->_parseRules();
|
|
|
|
if (!$rules instanceof CakeValidationSet) {
|
|
|
|
$rules = new CakeValidationSet($field, $rules, $this->getMethods());
|
|
|
|
}
|
|
|
|
$this->_fields[$field] = $rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsets the rulset for a field
|
|
|
|
*
|
|
|
|
* @param string $field name of the field to unset
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
public function offsetUnset($field) {
|
2012-05-06 01:30:40 +00:00
|
|
|
$this->_parseRules();
|
2012-05-05 20:22:02 +00:00
|
|
|
unset($this->_fields[$field]);
|
|
|
|
}
|
|
|
|
|
2012-05-05 22:04:35 +00:00
|
|
|
/**
|
|
|
|
* Returns an iterator for each of the fields to be validated
|
|
|
|
*
|
|
|
|
* @return ArrayIterator
|
|
|
|
**/
|
|
|
|
public function getIterator() {
|
|
|
|
$this->_parseRules();
|
|
|
|
return new ArrayIterator($this->_fields);
|
|
|
|
}
|
2012-05-05 22:16:09 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-06 05:37:13 +00:00
|
|
|
* Returns the number of fields having validation rules
|
2012-05-05 22:16:09 +00:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
**/
|
|
|
|
public function count() {
|
|
|
|
$this->_parseRules();
|
|
|
|
return count($this->_fields);
|
|
|
|
}
|
|
|
|
|
2012-05-07 03:36:28 +00:00
|
|
|
/**
|
2012-05-20 23:15:46 +00:00
|
|
|
* Adds a new rule to a field's rule set. If second argumet is an array or instance of
|
|
|
|
* CakeValidationSet then rules list for the field will be replaced with second argument and
|
|
|
|
* third argument will be ignored.
|
2012-05-07 03:36:28 +00:00
|
|
|
*
|
2012-05-11 01:57:01 +00:00
|
|
|
* ## Example:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* $validator
|
|
|
|
* ->add('title', 'required', array('rule' => 'notEmpty', 'required' => true))
|
|
|
|
* ->add('user_id', 'valid', array('rule' => 'numeric', 'message' => 'Invalid User'))
|
2012-05-20 23:15:46 +00:00
|
|
|
*
|
|
|
|
* $validator->add('password', array(
|
|
|
|
* 'size' => array('rule' => array('between', 8, 20)),
|
|
|
|
* 'hasSpecialCharacter' => array('rule' => 'validateSpecialchar', 'message' => 'not valid')
|
|
|
|
* ));
|
2012-05-11 01:57:01 +00:00
|
|
|
* }}}
|
|
|
|
*
|
2012-05-07 03:36:28 +00:00
|
|
|
* @param string $field The name of the field from wich the rule will be removed
|
2012-05-20 23:15:46 +00:00
|
|
|
* @param string|array|CakeValidationSet $name name of the rule to be added or list of rules for the field
|
|
|
|
* @param array|CakeValidationRule $rule or list of rules to be added to the field's rule set
|
2012-05-07 03:36:28 +00:00
|
|
|
* @return ModelValidator this instance
|
|
|
|
**/
|
2012-05-20 23:15:46 +00:00
|
|
|
public function add($field, $name, $rule = null) {
|
2012-05-07 03:36:28 +00:00
|
|
|
$this->_parseRules();
|
2012-05-20 23:15:46 +00:00
|
|
|
if ($name instanceof CakeValidationSet) {
|
|
|
|
$this->_fields[$field] = $name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-05-07 03:36:28 +00:00
|
|
|
if (!isset($this->_fields[$field])) {
|
2012-05-20 23:15:46 +00:00
|
|
|
$rule = (is_string($name)) ? array($name => $rule) : $name;
|
2012-05-07 03:36:28 +00:00
|
|
|
$this->_fields[$field] = new CakeValidationSet($field, $rule, $this->getMethods());
|
|
|
|
} else {
|
2012-05-20 23:15:46 +00:00
|
|
|
if (is_string($name)) {
|
|
|
|
$this->_fields[$field]->setRule($name, $rule);
|
|
|
|
} else {
|
|
|
|
$this->_fields[$field]->setRules($name);
|
|
|
|
}
|
2012-05-07 03:36:28 +00:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a rule from the set by its name
|
|
|
|
*
|
2012-05-11 01:57:01 +00:00
|
|
|
* ## Example:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* $validator
|
|
|
|
* ->remove('title', 'required')
|
|
|
|
* ->remove('user_id')
|
|
|
|
* }}}
|
|
|
|
*
|
2012-05-07 03:36:28 +00:00
|
|
|
* @param string $field The name of the field from wich the rule will be removed
|
|
|
|
* @param string $rule the name of the rule to be removed
|
|
|
|
* @return ModelValidator this instance
|
|
|
|
**/
|
|
|
|
public function remove($field, $rule = null) {
|
|
|
|
$this->_parseRules();
|
|
|
|
if ($rule === null) {
|
|
|
|
unset($this->_fields[$field]);
|
|
|
|
} else {
|
|
|
|
$this->_fields[$field]->removeRule($rule);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|