/**
  * Attaches a model object and loads a list of behaviors
  *
  * @todo Make this method a constructor instead..
  * @param string $modelName
  * @param array $behaviors
  * @return void
  */
 public function init($modelName, $behaviors = array())
 {
     $this->modelName = $modelName;
     if (!empty($behaviors)) {
         foreach (BehaviorCollection::normalizeObjectArray($behaviors) as $behavior => $config) {
             $this->load($config['class'], $config['settings']);
         }
     }
 }
Example #2
0
 /**
  * Returns an array of fields that have failed validation. On the current model.
  *
  * @param string $options An optional array of custom options to be made available in the beforeValidate callback
  * @return array Array of invalid fields
  * @see Model::validates()
  */
 public function invalidFields($options = array())
 {
     if (!$this->Behaviors->trigger('beforeValidate', array(&$this, $options), array('break' => true, 'breakOn' => false)) || $this->beforeValidate($options) === false) {
         return false;
     }
     if (!isset($this->validate) || empty($this->validate)) {
         return $this->validationErrors;
     }
     $data = $this->data;
     $methods = array_map('strtolower', get_class_methods($this));
     $behaviorMethods = array_keys($this->Behaviors->methods());
     if (isset($data[$this->alias])) {
         $data = $data[$this->alias];
     } elseif (!is_array($data)) {
         $data = array();
     }
     $exists = $this->exists();
     $_validate = $this->validate;
     $whitelist = $this->whitelist;
     if (!empty($options['fieldList'])) {
         $whitelist = $options['fieldList'];
     }
     if (!empty($whitelist)) {
         $validate = array();
         foreach ((array) $whitelist as $f) {
             if (!empty($this->validate[$f])) {
                 $validate[$f] = $this->validate[$f];
             }
         }
         $this->validate = $validate;
     }
     $validationDomain = $this->validationDomain;
     if (empty($validationDomain)) {
         $validationDomain = 'default';
     }
     foreach ($this->validate as $fieldName => $ruleSet) {
         if (!is_array($ruleSet) || is_array($ruleSet) && isset($ruleSet['rule'])) {
             $ruleSet = array($ruleSet);
         }
         $default = array('allowEmpty' => null, 'required' => null, 'rule' => 'blank', 'last' => true, 'on' => null);
         foreach ($ruleSet as $index => $validator) {
             if (!is_array($validator)) {
                 $validator = array('rule' => $validator);
             }
             $validator = array_merge($default, $validator);
             if (empty($validator['on']) || $validator['on'] == 'create' && !$exists || $validator['on'] == 'update' && $exists) {
                 $valid = true;
                 $requiredFail = !isset($data[$fieldName]) && $validator['required'] === true || isset($data[$fieldName]) && (empty($data[$fieldName]) && !is_numeric($data[$fieldName])) && $validator['allowEmpty'] === false;
                 if (!$requiredFail && array_key_exists($fieldName, $data)) {
                     if (empty($data[$fieldName]) && $data[$fieldName] != '0' && $validator['allowEmpty'] === true) {
                         break;
                     }
                     if (is_array($validator['rule'])) {
                         $rule = $validator['rule'][0];
                         unset($validator['rule'][0]);
                         $ruleParams = array_merge(array($data[$fieldName]), array_values($validator['rule']));
                     } else {
                         $rule = $validator['rule'];
                         $ruleParams = array($data[$fieldName]);
                     }
                     if (in_array(strtolower($rule), $methods)) {
                         $ruleParams[] = $validator;
                         $ruleParams[0] = array($fieldName => $ruleParams[0]);
                         $valid = $this->dispatchMethod($rule, $ruleParams);
                     } elseif (in_array($rule, $behaviorMethods) || in_array(strtolower($rule), $behaviorMethods)) {
                         $ruleParams[] = $validator;
                         $ruleParams[0] = array($fieldName => $ruleParams[0]);
                         $valid = $this->Behaviors->dispatchMethod($this, $rule, $ruleParams);
                     } elseif (method_exists('Validation', $rule)) {
                         $valid = call_user_func_array(array('Validation', $rule), $ruleParams);
                     } elseif (!is_array($validator['rule'])) {
                         $valid = preg_match($rule, $data[$fieldName]);
                     } elseif (Configure::read('debug') > 0) {
                         trigger_error(__d('cake_dev', 'Could not find validation handler %s for %s', $rule, $fieldName), E_USER_WARNING);
                     }
                 }
                 if ($requiredFail || !$valid || is_string($valid) && strlen($valid) > 0) {
                     if (is_string($valid)) {
                         $message = $valid;
                     } elseif (isset($validator['message'])) {
                         $args = null;
                         if (is_array($validator['message'])) {
                             $message = $validator['message'][0];
                             $args = array_slice($validator['message'], 1);
                         } else {
                             $message = $validator['message'];
                         }
                         if (is_array($validator['rule']) && $args === null) {
                             $args = array_slice($ruleSet[$index]['rule'], 1);
                         }
                         $message = __d($validationDomain, $message, $args);
                     } elseif (is_string($index)) {
                         if (is_array($validator['rule'])) {
                             $args = array_slice($ruleSet[$index]['rule'], 1);
                             $message = __d($validationDomain, $index, $args);
                         } else {
                             $message = __d($validationDomain, $index);
                         }
                     } elseif (!$requiredFail && is_numeric($index) && count($ruleSet) > 1) {
                         $message = $index + 1;
                     } else {
                         $message = __d('cake_dev', 'This field cannot be left blank');
                     }
                     $this->invalidate($fieldName, $message);
                     if ($validator['last']) {
                         break;
                     }
                 }
             }
         }
     }
     $this->validate = $_validate;
     return $this->validationErrors;
 }
Example #3
0
 /**
  * Returns an array of fields that have failed validation.
  *
  * @param string $options An optional array of custom options to be made available in the beforeValidate callback
  * @return array Array of invalid fields
  * @access public
  * @link http://book.cakephp.org/view/410/Validating-Data-from-the-Controller
  */
 function invalidFields($options = array())
 {
     if (!$this->Behaviors->trigger($this, 'beforeValidate', array($options), array('break' => true, 'breakOn' => false)) || $this->beforeValidate($options) === false) {
         return $this->validationErrors;
     }
     if (!isset($this->validate) || empty($this->validate)) {
         return $this->validationErrors;
     }
     $data = $this->data;
     $methods = array_map('strtolower', get_class_methods($this));
     $behaviorMethods = array_keys($this->Behaviors->methods());
     if (isset($data[$this->alias])) {
         $data = $data[$this->alias];
     } elseif (!is_array($data)) {
         $data = array();
     }
     $Validation =& Validation::getInstance();
     $this->exists();
     $_validate = $this->validate;
     $whitelist = $this->whitelist;
     if (array_key_exists('fieldList', $options)) {
         $whitelist = $options['fieldList'];
     }
     if (!empty($whitelist)) {
         $validate = array();
         foreach ((array) $whitelist as $f) {
             if (!empty($this->validate[$f])) {
                 $validate[$f] = $this->validate[$f];
             }
         }
         $this->validate = $validate;
     }
     foreach ($this->validate as $fieldName => $ruleSet) {
         if (!is_array($ruleSet) || is_array($ruleSet) && isset($ruleSet['rule'])) {
             $ruleSet = array($ruleSet);
         }
         $default = array('allowEmpty' => null, 'required' => null, 'rule' => 'blank', 'last' => false, 'on' => null);
         foreach ($ruleSet as $index => $validator) {
             if (!is_array($validator)) {
                 $validator = array('rule' => $validator);
             }
             $validator = array_merge($default, $validator);
             if (isset($validator['message'])) {
                 $message = $validator['message'];
             } else {
                 $message = __('This field cannot be left blank', true);
             }
             if (empty($validator['on']) || $validator['on'] == 'create' && !$this->__exists || $validator['on'] == 'update' && $this->__exists) {
                 $required = !isset($data[$fieldName]) && $validator['required'] === true || isset($data[$fieldName]) && (empty($data[$fieldName]) && !is_numeric($data[$fieldName])) && $validator['allowEmpty'] === false;
                 if ($required) {
                     $this->invalidate($fieldName, $message);
                     if ($validator['last']) {
                         break;
                     }
                 } elseif (array_key_exists($fieldName, $data)) {
                     if (empty($data[$fieldName]) && $data[$fieldName] != '0' && $validator['allowEmpty'] === true) {
                         break;
                     }
                     if (is_array($validator['rule'])) {
                         $rule = $validator['rule'][0];
                         unset($validator['rule'][0]);
                         $ruleParams = array_merge(array($data[$fieldName]), array_values($validator['rule']));
                     } else {
                         $rule = $validator['rule'];
                         $ruleParams = array($data[$fieldName]);
                     }
                     $valid = true;
                     if (in_array(strtolower($rule), $methods)) {
                         $ruleParams[] = $validator;
                         $ruleParams[0] = array($fieldName => $ruleParams[0]);
                         $valid = $this->dispatchMethod($rule, $ruleParams);
                     } elseif (in_array($rule, $behaviorMethods) || in_array(strtolower($rule), $behaviorMethods)) {
                         $ruleParams[] = $validator;
                         $ruleParams[0] = array($fieldName => $ruleParams[0]);
                         $valid = $this->Behaviors->dispatchMethod($this, $rule, $ruleParams);
                     } elseif (method_exists($Validation, $rule)) {
                         $valid = $Validation->dispatchMethod($rule, $ruleParams);
                     } elseif (!is_array($validator['rule'])) {
                         $valid = preg_match($rule, $data[$fieldName]);
                     }
                     if (!$valid || is_string($valid) && strlen($valid) > 0) {
                         if (is_string($valid) && strlen($valid) > 0) {
                             $validator['message'] = $valid;
                         } elseif (!isset($validator['message'])) {
                             if (is_string($index)) {
                                 $validator['message'] = $index;
                             } elseif (is_numeric($index) && count($ruleSet) > 1) {
                                 $validator['message'] = $index + 1;
                             } else {
                                 $validator['message'] = $message;
                             }
                         }
                         $this->invalidate($fieldName, $validator['message']);
                         if ($validator['last']) {
                             break;
                         }
                     }
                 }
             }
         }
     }
     $this->validate = $_validate;
     return $this->validationErrors;
 }
 /**
  * test hasMethod returning a 'callback'
  *
  * @return void
  */
 public function testHasMethodAsCallback()
 {
     new Sample();
     $Collection = new BehaviorCollection();
     $Collection->init('Sample', array('Test', 'Test2'));
     $result = $Collection->hasMethod('testMethod', true);
     $expected = array('Test', 'testMethod');
     $this->assertEquals($expected, $result);
     $result = $Collection->hasMethod('resolveMethod', true);
     $expected = array('Test2', 'resolveMethod');
     $this->assertEquals($expected, $result);
     $result = $Collection->hasMethod('mappingRobotOnTheRoof', true);
     $expected = array('Test2', 'mapped', 'mappingRobotOnTheRoof');
     $this->assertEquals($expected, $result);
 }
Example #5
0
 /**
  * Check that a method is callable on a model. This will check both the model's own methods, its
  * inherited methods and methods that could be callable through behaviors.
  *
  * @param string $method The method to be called.
  * @return boolean True on method being callable.
  */
 public function hasMethod($method)
 {
     if (method_exists($this, $method)) {
         return true;
     }
     return $this->Behaviors->hasMethod($method);
 }