mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Refactoring Model::invalidFields() a bit.
This commit is contained in:
parent
653163c891
commit
97a975c6c4
4 changed files with 57 additions and 45 deletions
|
@ -389,12 +389,17 @@ class ExtractTask extends Shell {
|
|||
}
|
||||
|
||||
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 @@ class Model extends Object {
|
|||
$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,21 +2938,12 @@ class Model extends Object {
|
|||
}
|
||||
$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]) &&
|
||||
|
@ -2955,12 +2951,7 @@ class Model extends Object {
|
|||
)
|
||||
);
|
||||
|
||||
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 @@ class Model extends Object {
|
|||
$ruleParams = array($data[$fieldName]);
|
||||
}
|
||||
|
||||
$valid = true;
|
||||
|
||||
if (in_array(strtolower($rule), $methods)) {
|
||||
$ruleParams[] = $validator;
|
||||
$ruleParams[0] = array($fieldName => $ruleParams[0]);
|
||||
|
@ -2990,31 +2979,39 @@ class Model extends Object {
|
|||
} 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 (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');
|
||||
}
|
||||
|
||||
if ($validator['last']) {
|
||||
break;
|
||||
}
|
||||
$this->invalidate($fieldName, $message);
|
||||
if ($validator['last']) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -686,12 +686,12 @@ class ModelValidationTest extends BaseModelTest {
|
|||
*/
|
||||
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 @@ class ModelValidationTest extends BaseModelTest {
|
|||
)
|
||||
)
|
||||
);
|
||||
|
||||
$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'
|
||||
)
|
||||
),
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue