Re-fixing Model::set() to allow for fields that don't exist, fixing model validation with messages, adding tests to prove parameter order for custom validation errors, refs #50005

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7408 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-08-01 06:28:59 +00:00
parent 661e757612
commit 793295036f
3 changed files with 77 additions and 5 deletions

View file

@ -726,8 +726,7 @@ class Model extends Overloadable {
if (is_array($one)) {
$data = $one;
if (empty($one[$this->alias])) {
$keys = array_keys($one);
if (in_array($keys[0], array_keys($this->_schema))) {
if ($this->getAssociated(key($one)) === null) {
$data = array($this->alias => $one);
}
}
@ -2186,7 +2185,7 @@ class Model extends Overloadable {
$valid = true;
if (in_array(strtolower($rule), $methods)) {
$ruleParams[] = array_diff_key($validator, $default);
$ruleParams[] = $validator;
$ruleParams[0] = array($fieldName => $ruleParams[0]);
$valid = $this->dispatchMethod($rule, $ruleParams);
} elseif (in_array($rule, $behaviorMethods) || in_array(strtolower($rule), $behaviorMethods)) {
@ -2198,8 +2197,11 @@ class Model extends Overloadable {
} elseif (!is_array($validator['rule'])) {
$valid = preg_match($rule, $data[$fieldName]);
}
if (!$valid) {
if (!isset($validator['message'])) {
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) {

View file

@ -4125,6 +4125,57 @@ class ModelTest extends CakeTestCase {
function testMultipleValidation() {
$TestModel =& new ValidationTest();
}
/**
* Tests validation parameter order in custom validation methods
*
* @access public
* @return void
*/
function testValidationParams() {
$TestModel =& new ValidationTest();
$TestModel->validate['title'] = array('rule' => 'customValidatorWithParams', 'required' => true);
$TestModel->create(array('title' => 'foo'));
$TestModel->invalidFields();
$expected = array(
'data' => array('title' => 'foo'),
'validator' => array(
'rule' => 'customValidatorWithParams', 'on' => null,
'last' => false, 'allowEmpty' => false, 'required' => true
),
'or' => true,
'ignore_on_same' => 'id'
);
$this->assertEqual($TestModel->validatorParams, $expected);
$TestModel->validate['title'] = array('rule' => 'customValidatorWithMessage', 'required' => true);
$expected = array('title' => 'This field will *never* validate! Muhahaha!');
$this->assertEqual($TestModel->invalidFields(), $expected);
}
/**
* Tests validation parameter order in custom validation methods
*
* @access public
* @return void
*/
function testAllowSimulatedFields() {
$TestModel =& new ValidationTest();
$TestModel->create(array('title' => 'foo', 'bar' => 'baz'));
$expected = array('ValidationTest' => array('title' => 'foo', 'bar' => 'baz'));
$this->assertEqual($TestModel->data, $expected);
}
/**
* Tests validation parameter order in custom validation methods
*
* @access public
* @return void
*/
function testInvalidAssociation() {
$TestModel =& new ValidationTest();
$this->assertNull($TestModel->getAssociated('Foo'));
}
/**
* testLoadModelSecondIteration method
*

View file

@ -1797,6 +1797,25 @@ class ValidationTest extends CakeTestModel {
function customValidationMethod($data) {
return $data === 1;
}
/**
* Custom validator with parameters + default values
*
* @access public
* @return array
*/
function customValidatorWithParams($data, $validator, $or = true, $ignore_on_same = 'id') {
$this->validatorParams = get_defined_vars();
return true;
}
/**
* Custom validator with messaage
*
* @access public
* @return array
*/
function customValidatorWithMessage($data) {
return 'This field will *never* validate! Muhahaha!';
}
}
/**