Permalink
Browse files

Fix array arguments being translated.

Fixes #2902
  • Loading branch information...
1 parent 33eb46c commit ea9eee4c773c6fa77c58939c932ce10afcaa6353 @markstory markstory committed May 24, 2012
@@ -256,21 +256,13 @@ protected function _processValidationResponse($name, $rule) {
if (is_array($rule->rule) && $args === null) {
$args = array_slice($rule->rule, 1);
}
- if (!empty($args)) {
- foreach ($args as $k => $arg) {
- $args[$k] = __d($this->_validationDomain, $arg);
- }
- }
+ $args = $this->_translateArgs($args);
$message = __d($this->_validationDomain, $result, $args);
} elseif (is_string($name)) {
if (is_array($rule->rule)) {
$args = array_slice($rule->rule, 1);
- if (!empty($args)) {
- foreach ($args as $k => $arg) {
- $args[$k] = __d($this->_validationDomain, $arg);
- }
- }
+ $args = $this->_translateArgs($args);
$message = __d($this->_validationDomain, $name, $args);
} else {
$message = __d($this->_validationDomain, $name);
@@ -282,6 +274,21 @@ protected function _processValidationResponse($name, $rule) {
return $message;
}
+/**
+ * Applies translations to validator arguments.
+ *
+ * @param array $args The args to translate
+ * @return array Translated args.
+ */
+ protected function _translateArgs($args) {
+ foreach ((array)$args as $k => $arg) {
+ if (is_string($arg)) {
+ $args[$k] = __d($this->_validationDomain, $arg);
+ }
+ }
+ return $args;
+ }
+
/**
* Returns wheter an index exists in the rule set
*
@@ -340,4 +347,4 @@ public function count() {
return count($this->_rules);
}
-}
+}
@@ -56,11 +56,25 @@ public function testValidate() {
$result = $Field->validate($data);
$this->assertEmpty($result);
- $Field = new CakeValidationSet('nothere', array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
+ $Field = new CakeValidationSet('nothere', array(
+ 'notEmpty' => array(
+ 'rule' => 'notEmpty',
+ 'required' => true
+ )
+ ));
$result = $Field->validate($data);
$expected = array('notEmpty');
$this->assertEquals($expected, $result);
+
+ $Field = new CakeValidationSet('body', array(
+ 'inList' => array(
+ 'rule' => array('inList', array('test'))
+ )
+ ));
+ $result = $Field->validate($data);
+ $expected = array('inList');
+ $this->assertEquals($expected, $result);
}
/**
@@ -313,4 +327,4 @@ public function testRemoveRule() {
$this->assertFalse(isset($Set['other']));
}
-}
+}

0 comments on commit ea9eee4

Please sign in to comment.