Skip to content

Commit ea9eee4

Browse files
committed
Fix array arguments being translated.
Fixes #2902
1 parent 33eb46c commit ea9eee4

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

lib/Cake/Model/Validator/CakeValidationSet.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,13 @@ protected function _processValidationResponse($name, $rule) {
256256
if (is_array($rule->rule) && $args === null) {
257257
$args = array_slice($rule->rule, 1);
258258
}
259-
if (!empty($args)) {
260-
foreach ($args as $k => $arg) {
261-
$args[$k] = __d($this->_validationDomain, $arg);
262-
}
263-
}
259+
$args = $this->_translateArgs($args);
264260

265261
$message = __d($this->_validationDomain, $result, $args);
266262
} elseif (is_string($name)) {
267263
if (is_array($rule->rule)) {
268264
$args = array_slice($rule->rule, 1);
269-
if (!empty($args)) {
270-
foreach ($args as $k => $arg) {
271-
$args[$k] = __d($this->_validationDomain, $arg);
272-
}
273-
}
265+
$args = $this->_translateArgs($args);
274266
$message = __d($this->_validationDomain, $name, $args);
275267
} else {
276268
$message = __d($this->_validationDomain, $name);
@@ -282,6 +274,21 @@ protected function _processValidationResponse($name, $rule) {
282274
return $message;
283275
}
284276

277+
/**
278+
* Applies translations to validator arguments.
279+
*
280+
* @param array $args The args to translate
281+
* @return array Translated args.
282+
*/
283+
protected function _translateArgs($args) {
284+
foreach ((array)$args as $k => $arg) {
285+
if (is_string($arg)) {
286+
$args[$k] = __d($this->_validationDomain, $arg);
287+
}
288+
}
289+
return $args;
290+
}
291+
285292
/**
286293
* Returns wheter an index exists in the rule set
287294
*
@@ -340,4 +347,4 @@ public function count() {
340347
return count($this->_rules);
341348
}
342349

343-
}
350+
}

lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,25 @@ public function testValidate() {
5656
$result = $Field->validate($data);
5757
$this->assertEmpty($result);
5858

59-
$Field = new CakeValidationSet('nothere', array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
59+
$Field = new CakeValidationSet('nothere', array(
60+
'notEmpty' => array(
61+
'rule' => 'notEmpty',
62+
'required' => true
63+
)
64+
));
6065

6166
$result = $Field->validate($data);
6267
$expected = array('notEmpty');
6368
$this->assertEquals($expected, $result);
69+
70+
$Field = new CakeValidationSet('body', array(
71+
'inList' => array(
72+
'rule' => array('inList', array('test'))
73+
)
74+
));
75+
$result = $Field->validate($data);
76+
$expected = array('inList');
77+
$this->assertEquals($expected, $result);
6478
}
6579

6680
/**
@@ -313,4 +327,4 @@ public function testRemoveRule() {
313327
$this->assertFalse(isset($Set['other']));
314328
}
315329

316-
}
330+
}

0 commit comments

Comments
 (0)