Skip to content

Commit 6f5748c

Browse files
authored
Merge pull request #76 from ADmad/bug/valdation-arg-array
Fix error when validation arg is an array
2 parents 4208305 + 1c4f493 commit 6f5748c

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/Validation/Validator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ protected function _processRules(string $field, ValidationSet $rules, array $dat
8888
protected function _translateArgs(array $args): array
8989
{
9090
foreach ($args as $k => $arg) {
91-
if (is_string($arg)) {
92-
$args[$k] = __d($this->_validationDomain, $arg);
91+
if (is_array($arg)) {
92+
$arg = implode(', ', $arg);
9393
}
94+
95+
$args[$k] = __d($this->_validationDomain, (string)$arg);
9496
}
9597

9698
return $args;

tests/TestCase/Validation/ValidatorTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Cake\Cache\Cache;
88
use Cake\I18n\I18n;
99
use Cake\TestSuite\TestCase;
10+
use Laminas\Diactoros\UploadedFile;
1011

1112
/**
1213
* Tests for Validator.
@@ -80,6 +81,15 @@ public function setUp(): void
8081
'value_0' => 'Message from validation_non_default',
8182
'value_1' => '',
8283
],
84+
[
85+
'domain' => 'validation',
86+
'locale' => I18n::getLocale(),
87+
'context' => '',
88+
'singular' => 'mimeType',
89+
'plural' => '',
90+
'value_0' => 'Valid mime types: {0}',
91+
'value_1' => '',
92+
],
8393
];
8494
foreach ($messages as $row) {
8595
$I18nMessages->save($I18nMessages->newEntity($row));
@@ -89,7 +99,8 @@ public function setUp(): void
8999

90100
$this->validator
91101
->add('email', 'email', ['rule' => 'email'])
92-
->add('field', 'comparison', ['rule' => ['comparison', '<', 50]]);
102+
->add('field', 'comparison', ['rule' => ['comparison', '<', 50]])
103+
->add('file', 'mimeType', ['rule' => ['mimeType', ['image/jpeg, image/png']]]);
93104
}
94105

95106
/**
@@ -99,14 +110,18 @@ public function setUp(): void
99110
*/
100111
public function testErrors()
101112
{
113+
$file = new UploadedFile(__FILE__, 1, UPLOAD_ERR_OK, 'foo.txt', 'text/plain');
114+
102115
$errors = $this->validator->validate([
103116
'email' => 'foo',
104117
'field' => '100',
118+
'file' => $file,
105119
]);
106120

107121
$expected = [
108122
'email' => ['email' => 'Enter a valid email'],
109123
'field' => ['comparison' => 'This value must be less than 50'],
124+
'file' => ['mimeType' => 'Valid mime types: image/jpeg, image/png'],
110125
];
111126
$this->assertEquals($expected, $errors);
112127
}

0 commit comments

Comments
 (0)