Skip to content

Commit

Permalink
[8.x] Fix sometimes conditions that add rules for sibling values with…
Browse files Browse the repository at this point in the history
…in an array of data (laravel#38899)

* Hotfix for laravel#38443 (comment)

* Fix style

* fix bug

* Update ValidationValidatorTest.php

Co-authored-by: Taylor Otwell <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored and victorvilella committed Oct 12, 2021
1 parent 16e4ff1 commit 396704a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,8 @@ public function sometimes($attribute, $rules, callable $callback)
foreach ((array) $attribute as $key) {
$response = (new ValidationRuleParser($this->data))->explode([$key => $rules]);

$this->implicitAttributes = array_merge($response->implicitAttributes, $this->implicitAttributes);

foreach ($response->rules as $ruleKey => $ruleValue) {
if ($callback($payload, $this->dataForSometimesIteration($ruleKey, ! Str::endsWith($key, '.*')))) {
$this->addRules([$ruleKey => $ruleValue]);
Expand Down
56 changes: 56 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4379,6 +4379,62 @@ public function testItemAwareSometimesAddingRules()
$this->assertEquals(['attendee.name' => ['string', 'required'], 'attendee.title' => ['string', 'required'], 'attendee.type' => ['string', 'required']], $v->getRules());
}

public function testValidateSometimesImplicitEachWithAsterisksBeforeAndAfter()
{
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, [
'foo' => [
['start' => '2016-04-19', 'end' => '2017-04-19'],
],
], []);
$v->sometimes('foo.*.start', ['before:foo.*.end'], function () {
return true;
});
$this->assertTrue($v->passes());

$v = new Validator($trans, [
'foo' => [
['start' => '2016-04-19', 'end' => '2017-04-19'],
],
], []);
$v->sometimes('foo.*.start', 'before:foo.*.end', function () {
return true;
});
$this->assertTrue($v->passes());

$v = new Validator($trans, [
'foo' => [
['start' => '2016-04-19', 'end' => '2017-04-19'],
],
], []);
$v->sometimes('foo.*.end', ['before:foo.*.start'], function () {
return true;
});

$this->assertTrue($v->fails());

$v = new Validator($trans, [
'foo' => [
['start' => '2016-04-19', 'end' => '2017-04-19'],
],
], []);
$v->sometimes('foo.*.end', ['after:foo.*.start'], function () {
return true;
});
$this->assertTrue($v->passes());

$v = new Validator($trans, [
'foo' => [
['start' => '2016-04-19', 'end' => '2017-04-19'],
],
], []);
$v->sometimes('foo.*.start', ['after:foo.*.end'], function () {
return true;
});
$this->assertTrue($v->fails());
}

public function testCustomValidators()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down

0 comments on commit 396704a

Please sign in to comment.