Renaming some methods ands removing others

This commit is contained in:
Jose Lorenzo Rodriguez 2012-05-05 13:34:14 -04:30
parent 05abff6ecd
commit 843d95bb77
3 changed files with 22 additions and 115 deletions

View file

@ -2989,7 +2989,7 @@ class Model extends Object implements CakeEventListener {
* @see Model::validates() * @see Model::validates()
*/ */
public function invalidFields($options = array()) { public function invalidFields($options = array()) {
return $this->validator()->invalidFields($options); return $this->validator()->errors($options);
} }
/** /**

View file

@ -36,13 +36,6 @@ class ModelValidator {
*/ */
public $validationErrors = array(); public $validationErrors = array();
/**
* Holds the options
*
* @var array
*/
public $options = array();
/** /**
* Holds the CakeField objects array * Holds the CakeField objects array
* *
@ -90,7 +83,7 @@ class ModelValidator {
* @return boolean True if there are no errors * @return boolean True if there are no errors
*/ */
public function validates($options = array()) { public function validates($options = array()) {
$errors = $this->invalidFields($options); $errors = $this->errors($options);
if (empty($errors) && $errors !== false) { if (empty($errors) && $errors !== false) {
$errors = $this->_validateWithModels($options); $errors = $this->_validateWithModels($options);
} }
@ -218,20 +211,22 @@ class ModelValidator {
* @return array Array of invalid fields * @return array Array of invalid fields
* @see Model::validates() * @see Model::validates()
*/ */
public function invalidFields($options = array()) { public function errors($options = array()) {
if (!$this->propagateBeforeValidate($options)) { if (!$this->_triggerBeforeValidate($options)) {
return false; return false;
} }
$model = $this->getModel(); $model = $this->getModel();
$this->setOptions($options);
if (!$this->_parseRules()) { if (!$this->_parseRules()) {
return $model->validationErrors; return $model->validationErrors;
} }
$fieldList = isset($options['fieldList']) ? $options['fieldList'] : array();
$exists = $model->exists(); $exists = $model->exists();
$methods = $this->getMethods(); $methods = $this->getMethods();
foreach ($this->_fields as $field) { $fields = $this->_validationList($fieldList);
foreach ($fields as $field) {
$field->setMethods($methods); $field->setMethods($methods);
$field->setValidationDomain($model->validationDomain); $field->setValidationDomain($model->validationDomain);
$data = isset($model->data[$model->alias]) ? $model->data[$model->alias] : array(); $data = isset($model->data[$model->alias]) ? $model->data[$model->alias] : array();
@ -249,14 +244,11 @@ class ModelValidator {
* rule (in case of multiple validation for field) that was broken. * rule (in case of multiple validation for field) that was broken.
* *
* @param string $field The name of the field to invalidate * @param string $field The name of the field to invalidate
* @param mixed $value Name of validation rule that was not failed, or validation message to * @param string $value Name of validation rule that failed, or validation message to
* be returned. If no validation key is provided, defaults to true. * be returned. If no validation key is provided, defaults to true.
* @return void * @return void
*/ */
public function invalidate($field, $value = true) { public function invalidate($field, $value = true) {
if (!is_array($this->validationErrors)) {
$this->validationErrors = array();
}
$this->getModel()->validationErrors[$field][] = $value; $this->getModel()->validationErrors[$field][] = $value;
} }
@ -316,7 +308,6 @@ class ModelValidator {
} }
$this->_validate = $this->_model->validate; $this->_validate = $this->_model->validate;
$this->_processWhitelist();
$this->_fields = array(); $this->_fields = array();
$methods = $this->getMethods(); $methods = $this->getMethods();
foreach ($this->_validate as $fieldName => $ruleSet) { foreach ($this->_validate as $fieldName => $ruleSet) {
@ -325,51 +316,6 @@ class ModelValidator {
return true; return true;
} }
/**
* Sets an options array. If $mergeVars is true, the options will be merged with the existing ones.
* Otherwise they will get replaced. The default is merging the vars.
*
* @param array $options [optional] The options to be set
* @param boolean $mergeVars [optional] If true, the options will be merged, otherwise they get replaced
* @return ModelValidator
*/
public function setOptions($options = array(), $mergeVars = false) {
if ($mergeVars === false) {
$this->options = $options;
} else {
$this->options = array_merge($this->options, $options);
}
return $this;
}
/**
* Sets an option $name with $value. This method is chainable
*
* @param string $name The options name to be set
* @param mixed $value [optional] The value to be set. Defaults to null.
* @return ModelValidator
*/
public function setOption($name, $value = null) {
$this->options[$name] = $value;
return $this;
}
/**
* Gets an options value by $name. If $name is not set or no option has been found, returns null.
*
* @param string $name The options name to look up
* @return mixed Either null or the option value
*/
public function getOptions($name = null) {
if ($name !== null) {
if (!isset($this->options[$name])) {
return null;
}
return $this->options[$name];
}
return $this->options;
}
/** /**
* Sets the I18n domain for validation messages. This method is chainable. * Sets the I18n domain for validation messages. This method is chainable.
* *
@ -394,14 +340,14 @@ class ModelValidator {
} }
/** /**
* Processes the Model's whitelist and adjusts the validate array accordingly * Processes the Model's whitelist and returns the list of fields
* to be validated
* *
* @return void * @return array List of validation rules to be applied
*/ */
protected function _processWhitelist() { protected function _validationList($fieldList = array()) {
$model = $this->getModel(); $model = $this->getModel();
$whitelist = $model->whitelist; $whitelist = $model->whitelist;
$fieldList = $this->getOptions('fieldList');
if (!empty($fieldList)) { if (!empty($fieldList)) {
if (!empty($fieldList[$model->alias]) && is_array($fieldList[$model->alias])) { if (!empty($fieldList[$model->alias]) && is_array($fieldList[$model->alias])) {
@ -412,16 +358,20 @@ class ModelValidator {
} }
unset($fieldList); unset($fieldList);
$validateList = array();
if (!empty($whitelist)) { if (!empty($whitelist)) {
$this->validationErrors = array(); $this->validationErrors = array();
$validate = array();
foreach ((array)$whitelist as $f) { foreach ((array)$whitelist as $f) {
if (!empty($this->_validate[$f])) { if (!empty($this->_fields[$f])) {
$validate[$f] = $this->_validate[$f]; $validateList[$f] = $this->_fields[$f];
} }
} }
$this->_validate = $validate; } else {
return $this->_fields;
} }
return $validateList;
} }
/** /**
@ -469,7 +419,7 @@ class ModelValidator {
* @param array $options * @param array $options
* @return boolean * @return boolean
*/ */
public function propagateBeforeValidate($options = array()) { protected function _triggerBeforeValidate($options = array()) {
$model = $this->getModel(); $model = $this->getModel();
$event = new CakeEvent('Model.beforeValidate', $model, array($options)); $event = new CakeEvent('Model.beforeValidate', $model, array($options));
list($event->break, $event->breakOn) = array(true, false); list($event->break, $event->breakOn) = array(true, false);

View file

@ -1665,49 +1665,6 @@ class ModelValidationTest extends BaseModelTest {
$this->assertEquals($expected, array_keys($result)); $this->assertEquals($expected, array_keys($result));
} }
/**
* testSetOptions method
*
* @return void
*/
public function testSetOptions() {
$this->loadFixtures('Article', 'Comment');
$TestModel = new Article();
$Validator = $TestModel->validator();
$options = array('atomic' => false, 'validate' => true);
$Validator->setOptions($options);
$this->assertEquals($options, $Validator->options);
$options = array('callbacks' => false);
$Validator->setOptions($options);
$this->assertEquals($options, $Validator->options);
$options = array('atomic' => true);
$expected = array_merge($Validator->options, $options);
$Validator->setOptions($options, true);
$this->assertEquals($expected, $Validator->options);
}
/**
* testGetOptions method
*
* @return void
*/
public function testGetOptions() {
$this->loadFixtures('Article', 'Comment');
$TestModel = new Article();
$Validator = $TestModel->validator();
$options = array('atomic' => false, 'validate' => true);
$Validator->setOptions($options);
$result = $Validator->getOptions();
$this->assertEquals($options, $result);
$result = $Validator->getOptions('atomic');
$this->assertFalse($result);
}
/** /**
* testSetValidationDomain method * testSetValidationDomain method
* *