Skip to content

Commit 9631e15

Browse files
committed
Merge branch 'pr20090' into 7.x
2 parents 7852011 + c649ad3 commit 9631e15

File tree

2 files changed

+57
-31
lines changed

2 files changed

+57
-31
lines changed

src/Illuminate/Validation/Validator.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public function __construct(Translator $translator, array $data, array $rules,
277277
}
278278

279279
/**
280-
* Parse the data array, converting dots to ->.
280+
* Parse the data array, converting dots and asterisks.
281281
*
282282
* @param array $data
283283
* @return array
@@ -303,6 +303,33 @@ public function parseData(array $data)
303303
return $newData;
304304
}
305305

306+
/**
307+
* Replace the placeholders used in data keys.
308+
*
309+
* @param array $data
310+
* @return array
311+
*/
312+
protected function replacePlaceholders($data)
313+
{
314+
$originalData = [];
315+
316+
foreach ($data as $key => $value) {
317+
if (is_array($value)) {
318+
$value = $this->replacePlaceholders($value);
319+
}
320+
321+
$key = str_replace(
322+
[$this->dotPlaceholder, '__asterisk__'],
323+
['.', '*'],
324+
$key
325+
);
326+
327+
$originalData[$key] = $value;
328+
}
329+
330+
return $originalData;
331+
}
332+
306333
/**
307334
* Add an after validation callback.
308335
*
@@ -464,7 +491,7 @@ public function validated()
464491
}
465492
}
466493

467-
return $results;
494+
return $this->replacePlaceholders($results);
468495
}
469496

470497
/**

tests/Validation/ValidationValidatorTest.php

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3880,6 +3880,32 @@ public function testPassingSlashVulnerability()
38803880
$this->assertTrue($v->fails());
38813881
}
38823882

3883+
public function testPlaceholdersAreReplaced()
3884+
{
3885+
$trans = $this->getIlluminateArrayTranslator();
3886+
3887+
$v = new Validator($trans, [
3888+
'matrix' => ['\\' => ['invalid'], '1\\' => ['invalid']],
3889+
], [
3890+
'matrix.*.*' => 'integer',
3891+
]);
3892+
$this->assertTrue($v->fails());
3893+
3894+
$v = new Validator($trans, [
3895+
'matrix' => ['\\' => [1], '1\\' => [1]],
3896+
], [
3897+
'matrix.*.*' => 'integer',
3898+
]);
3899+
$this->assertTrue($v->passes());
3900+
3901+
$v = new Validator($trans, [
3902+
'foo' => ['bar' => 'valid'], 'foo.bar' => 'invalid', 'foo->bar' => 'valid',
3903+
], [
3904+
'foo\.bar' => 'required|in:valid',
3905+
]);
3906+
$this->assertTrue($v->fails());
3907+
}
3908+
38833909
public function testCoveringEmptyKeys()
38843910
{
38853911
$trans = $this->getIlluminateArrayTranslator();
@@ -3891,35 +3917,8 @@ public function testImplicitEachWithAsterisksWithArrayValues()
38913917
{
38923918
$trans = $this->getIlluminateArrayTranslator();
38933919

3894-
$v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'size:4']);
3895-
$this->assertFalse($v->passes());
3896-
3897-
$v = new Validator($trans, ['foo' => [1, 2, 3, 4]], ['foo' => 'size:4']);
3898-
$this->assertTrue($v->passes());
3899-
3900-
$v = new Validator($trans, ['foo' => [1, 2, 3, 4]], ['foo.*' => 'integer', 'foo.0' => 'required']);
3901-
$this->assertTrue($v->passes());
3902-
3903-
$v = new Validator($trans, ['foo' => [['bar' => [1, 2, 3]], ['bar' => [1, 2, 3]]]], ['foo.*.bar' => 'size:4']);
3904-
$this->assertFalse($v->passes());
3905-
3906-
$v = new Validator($trans,
3907-
['foo' => [['bar' => [1, 2, 3]], ['bar' => [1, 2, 3]]]], ['foo.*.bar' => 'min:3']);
3908-
$this->assertTrue($v->passes());
3909-
3910-
$v = new Validator($trans,
3911-
['foo' => [['bar' => [1, 2, 3]], ['bar' => [1, 2, 3]]]], ['foo.*.bar' => 'between:3,6']);
3912-
$this->assertTrue($v->passes());
3913-
3914-
$v = new Validator($trans,
3915-
['foo' => [['name' => 'first', 'votes' => [1, 2]], ['name' => 'second', 'votes' => ['something', 2]]]],
3916-
['foo.*.votes' => ['Required', 'Size:2']]);
3917-
$this->assertTrue($v->passes());
3918-
3919-
$v = new Validator($trans,
3920-
['foo' => [['name' => 'first', 'votes' => [1, 2, 3]], ['name' => 'second', 'votes' => ['something', 2]]]],
3921-
['foo.*.votes' => ['Required', 'Size:2']]);
3922-
$this->assertFalse($v->passes());
3920+
$v = new Validator($trans, ['foo' => ['bar.baz' => '']], ['foo' => 'required']);
3921+
$this->assertEquals(['foo' => ['bar.baz' => '']], $v->validated());
39233922
}
39243923

39253924
public function testValidateNestedArrayWithCommonParentChildKey()

0 commit comments

Comments
 (0)