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) {
|
foreach ($rules as $rule => $validateProp) {
|
||||||
|
$message = null;
|
||||||
if (isset($validateProp['message'])) {
|
if (isset($validateProp['message'])) {
|
||||||
if (is_array($validateProp['message'])) {
|
if (is_array($validateProp['message'])) {
|
||||||
$message = $validateProp['message'][0];
|
$message = $validateProp['message'][0];
|
||||||
} else {
|
} else {
|
||||||
$message = $validateProp['message'];
|
$message = $validateProp['message'];
|
||||||
}
|
}
|
||||||
|
} elseif (is_string($rule)) {
|
||||||
|
$message = $rule;
|
||||||
|
}
|
||||||
|
if ($message) {
|
||||||
$this->_strings[$domain][$message][$file][] = 'validation for field ' . $field;
|
$this->_strings[$domain][$message][$file][] = 'validation for field ' . $field;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2915,6 +2915,11 @@ class Model extends Object {
|
||||||
$this->validate = $validate;
|
$this->validate = $validate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$validationDomain = $this->validationDomain;
|
||||||
|
if (empty($validationDomain)) {
|
||||||
|
$validationDomain = 'default';
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($this->validate as $fieldName => $ruleSet) {
|
foreach ($this->validate as $fieldName => $ruleSet) {
|
||||||
if (!is_array($ruleSet) || (is_array($ruleSet) && isset($ruleSet['rule']))) {
|
if (!is_array($ruleSet) || (is_array($ruleSet) && isset($ruleSet['rule']))) {
|
||||||
$ruleSet = array($ruleSet);
|
$ruleSet = array($ruleSet);
|
||||||
|
@ -2933,21 +2938,12 @@ class Model extends Object {
|
||||||
}
|
}
|
||||||
$validator = array_merge($default, $validator);
|
$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 (
|
if (
|
||||||
empty($validator['on']) || ($validator['on'] == 'create' &&
|
empty($validator['on']) || ($validator['on'] == 'create' &&
|
||||||
!$exists) || ($validator['on'] == 'update' && $exists
|
!$exists) || ($validator['on'] == 'update' && $exists
|
||||||
)) {
|
)) {
|
||||||
$required = (
|
$valid = true;
|
||||||
|
$requiredFail = (
|
||||||
(!isset($data[$fieldName]) && $validator['required'] === true) ||
|
(!isset($data[$fieldName]) && $validator['required'] === true) ||
|
||||||
(
|
(
|
||||||
isset($data[$fieldName]) && (empty($data[$fieldName]) &&
|
isset($data[$fieldName]) && (empty($data[$fieldName]) &&
|
||||||
|
@ -2955,12 +2951,7 @@ class Model extends Object {
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($required) {
|
if (!$requiredFail && array_key_exists($fieldName, $data)) {
|
||||||
$this->invalidate($fieldName, __d($validationDomain, $message));
|
|
||||||
if ($validator['last']) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} elseif (array_key_exists($fieldName, $data)) {
|
|
||||||
if (empty($data[$fieldName]) && $data[$fieldName] != '0' && $validator['allowEmpty'] === true) {
|
if (empty($data[$fieldName]) && $data[$fieldName] != '0' && $validator['allowEmpty'] === true) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -2973,8 +2964,6 @@ class Model extends Object {
|
||||||
$ruleParams = array($data[$fieldName]);
|
$ruleParams = array($data[$fieldName]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$valid = true;
|
|
||||||
|
|
||||||
if (in_array(strtolower($rule), $methods)) {
|
if (in_array(strtolower($rule), $methods)) {
|
||||||
$ruleParams[] = $validator;
|
$ruleParams[] = $validator;
|
||||||
$ruleParams[0] = array($fieldName => $ruleParams[0]);
|
$ruleParams[0] = array($fieldName => $ruleParams[0]);
|
||||||
|
@ -2990,31 +2979,39 @@ class Model extends Object {
|
||||||
} elseif (Configure::read('debug') > 0) {
|
} elseif (Configure::read('debug') > 0) {
|
||||||
trigger_error(__d('cake_dev', 'Could not find validation handler %s for %s', $rule, $fieldName), E_USER_WARNING);
|
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 ($requiredFail || !$valid || (is_string($valid) && strlen($valid) > 0)) {
|
||||||
if (is_string($valid) && strlen($valid) > 0) {
|
if (is_string($valid)) {
|
||||||
$validator['message'] = $valid;
|
$message = $valid;
|
||||||
} elseif (!isset($validator['message'])) {
|
} elseif (isset($validator['message'])) {
|
||||||
if (is_string($index)) {
|
$args = null;
|
||||||
$validator['message'] = $index;
|
if (is_array($validator['message'])) {
|
||||||
} elseif (is_numeric($index) && count($ruleSet) > 1) {
|
$message = $validator['message'][0];
|
||||||
$validator['message'] = $index + 1;
|
$args = array_slice($validator['message'], 1);
|
||||||
} else {
|
} else {
|
||||||
$validator['message'] = __d($validationDomain, $message);
|
$message = $validator['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);
|
|
||||||
}
|
}
|
||||||
$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']) {
|
$this->invalidate($fieldName, $message);
|
||||||
break;
|
if ($validator['last']) {
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -686,12 +686,12 @@ class ModelValidationTest extends BaseModelTest {
|
||||||
*/
|
*/
|
||||||
public function testValidationMessageAsArray() {
|
public function testValidationMessageAsArray() {
|
||||||
$TestModel = new ValidationTest1();
|
$TestModel = new ValidationTest1();
|
||||||
$TestModel->create(array('title' => 'foo'));
|
|
||||||
$TestModel->validate = array(
|
$TestModel->validate = array(
|
||||||
'title' => array(
|
'title' => array(
|
||||||
'minLength' => array(
|
'minLength' => array(
|
||||||
'rule' => array('minLength', 6),
|
'rule' => array('minLength', 6),
|
||||||
'message' => array('Minimum length allowed is %d chars'),
|
'required' => true,
|
||||||
|
'message' => 'Minimum length allowed is %d chars',
|
||||||
'last' => false
|
'last' => false
|
||||||
),
|
),
|
||||||
'between' => array(
|
'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();
|
$TestModel->invalidFields();
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'title' => array(
|
'title' => array(
|
||||||
|
|
|
@ -53,11 +53,10 @@ class TestPluginPost extends TestPluginAppModel {
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'message' => 'Post body is required'
|
'message' => 'Post body is required'
|
||||||
),
|
),
|
||||||
'second_rule' => array(
|
'Post body is super required' => array(
|
||||||
'rule' => array('custom', '.*'),
|
'rule' => array('custom', '.*'),
|
||||||
'allowEmpty' => true,
|
'allowEmpty' => true,
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'message' => 'Post body is super required'
|
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue