Permalink
Browse files

Refactoring Model::invalidFields() a bit.

  • Loading branch information...
1 parent 653163c commit 97a975c6c4e3b11543c6d4e0be2e892e52fe3f3f @ADmad ADmad committed Sep 11, 2011
@@ -389,12 +389,17 @@ protected function _processValidationRules($field, $rules, $file, $domain) {
}
foreach ($rules as $rule => $validateProp) {
+ $message = null;
if (isset($validateProp['message'])) {
if (is_array($validateProp['message'])) {
$message = $validateProp['message'][0];
} else {
$message = $validateProp['message'];
}
+ } elseif (is_string($rule)) {
+ $message = $rule;
+ }
+ if ($message) {
$this->_strings[$domain][$message][$file][] = 'validation for field ' . $field;
}
}
@@ -2915,6 +2915,11 @@ public function invalidFields($options = array()) {
$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);
@@ -2933,34 +2938,20 @@ public function invalidFields($options = array()) {
}
$validator = array_merge($default, $validator);
- $validationDomain = $this->validationDomain;
- if (empty($validationDomain)) {
- $validationDomain = 'default';
- }
- if (isset($validator['message'])) {
- $message = $validator['message'];
- } else {
- $message = __d('cake_dev', 'This field cannot be left blank');
- }
-
if (
empty($validator['on']) || ($validator['on'] == 'create' &&
!$exists) || ($validator['on'] == 'update' && $exists
)) {
- $required = (
+ $valid = true;
+ $requiredFail = (
(!isset($data[$fieldName]) && $validator['required'] === true) ||
(
isset($data[$fieldName]) && (empty($data[$fieldName]) &&
!is_numeric($data[$fieldName])) && $validator['allowEmpty'] === false
)
);
- if ($required) {
- $this->invalidate($fieldName, __d($validationDomain, $message));
- if ($validator['last']) {
- break;
- }
- } elseif (array_key_exists($fieldName, $data)) {
+ if (!$requiredFail && array_key_exists($fieldName, $data)) {
if (empty($data[$fieldName]) && $data[$fieldName] != '0' && $validator['allowEmpty'] === true) {
break;
}
@@ -2973,8 +2964,6 @@ public function invalidFields($options = array()) {
$ruleParams = array($data[$fieldName]);
}
- $valid = true;
-
if (in_array(strtolower($rule), $methods)) {
$ruleParams[] = $validator;
$ruleParams[0] = array($fieldName => $ruleParams[0]);
@@ -2990,31 +2979,39 @@ public function invalidFields($options = array()) {
} elseif (Configure::read('debug') > 0) {
trigger_error(__d('cake_dev', 'Could not find validation handler %s for %s', $rule, $fieldName), E_USER_WARNING);
}
+ }
- 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'] = __d($validationDomain, $message);
- }
- } elseif (is_array($validator['message'])) {
- if (count($validator['message']) > 1) {
- $args = array_slice($validator['message'], 1);
- } else {
- $args = $validator['rule'];
- }
- $validator['message'] = __d($validationDomain, $validator['message'][0], $args);
+ 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'];
}
- $this->invalidate($fieldName, $validator['message']);
-
- if ($validator['last']) {
- break;
+ 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;
}
}
}
@@ -686,12 +686,12 @@ public function testMissingValidationErrorNoTriggering() {
*/
public function testValidationMessageAsArray() {
$TestModel = new ValidationTest1();
- $TestModel->create(array('title' => 'foo'));
$TestModel->validate = array(
'title' => array(
'minLength' => array(
'rule' => array('minLength', 6),
- 'message' => array('Minimum length allowed is %d chars'),
+ 'required' => true,
+ 'message' => 'Minimum length allowed is %d chars',
'last' => false
),
'between' => array(
@@ -700,6 +700,17 @@ public function testValidationMessageAsArray() {
)
)
);
+
+ $TestModel->create();
+ $TestModel->invalidFields();
+ $expected = array(
+ 'title' => array(
+ 'Minimum length allowed is 6 chars',
+ )
+ );
+ $this->assertEquals($TestModel->validationErrors, $expected);
+
+ $TestModel->create(array('title' => 'foo'));
$TestModel->invalidFields();
$expected = array(
'title' => array(
@@ -53,11 +53,10 @@ class TestPluginPost extends TestPluginAppModel {
'required' => false,
'message' => 'Post body is required'
),
- 'second_rule' => array(
+ 'Post body is super required' => array(
'rule' => array('custom', '.*'),
'allowEmpty' => true,
'required' => false,
- 'message' => 'Post body is super required'
)
),
);

0 comments on commit 97a975c

Please sign in to comment.