Skip to content

Commit

Permalink
Merge pull request #76 from ADmad/bug/valdation-arg-array
Browse files Browse the repository at this point in the history
Fix error when validation arg is an array
  • Loading branch information
ADmad authored Dec 7, 2024
2 parents 4208305 + 1c4f493 commit 6f5748c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ protected function _processRules(string $field, ValidationSet $rules, array $dat
protected function _translateArgs(array $args): array
{
foreach ($args as $k => $arg) {
if (is_string($arg)) {
$args[$k] = __d($this->_validationDomain, $arg);
if (is_array($arg)) {
$arg = implode(', ', $arg);
}

$args[$k] = __d($this->_validationDomain, (string)$arg);
}

return $args;
Expand Down
17 changes: 16 additions & 1 deletion tests/TestCase/Validation/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cake\Cache\Cache;
use Cake\I18n\I18n;
use Cake\TestSuite\TestCase;
use Laminas\Diactoros\UploadedFile;

/**
* Tests for Validator.
Expand Down Expand Up @@ -80,6 +81,15 @@ public function setUp(): void
'value_0' => 'Message from validation_non_default',
'value_1' => '',
],
[
'domain' => 'validation',
'locale' => I18n::getLocale(),
'context' => '',
'singular' => 'mimeType',
'plural' => '',
'value_0' => 'Valid mime types: {0}',
'value_1' => '',
],
];
foreach ($messages as $row) {
$I18nMessages->save($I18nMessages->newEntity($row));
Expand All @@ -89,7 +99,8 @@ public function setUp(): void

$this->validator
->add('email', 'email', ['rule' => 'email'])
->add('field', 'comparison', ['rule' => ['comparison', '<', 50]]);
->add('field', 'comparison', ['rule' => ['comparison', '<', 50]])
->add('file', 'mimeType', ['rule' => ['mimeType', ['image/jpeg, image/png']]]);
}

/**
Expand All @@ -99,14 +110,18 @@ public function setUp(): void
*/
public function testErrors()
{
$file = new UploadedFile(__FILE__, 1, UPLOAD_ERR_OK, 'foo.txt', 'text/plain');

$errors = $this->validator->validate([
'email' => 'foo',
'field' => '100',
'file' => $file,
]);

$expected = [
'email' => ['email' => 'Enter a valid email'],
'field' => ['comparison' => 'This value must be less than 50'],
'file' => ['mimeType' => 'Valid mime types: image/jpeg, image/png'],
];
$this->assertEquals($expected, $errors);
}
Expand Down

0 comments on commit 6f5748c

Please sign in to comment.