Skip to content

Commit 16443e3

Browse files
authored
Merge pull request #38 from rakit/fix-get-valid-data-on-array-attr
Fix getValidData and getInvalidData on array attribute
2 parents 06d8482 + 7ed34a5 commit 16443e3

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

src/Validation.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ protected function validateAttribute(Attribute $attribute)
103103
}
104104

105105
if ($isValid) {
106-
$this->validData[$attributeKey] = $value;
106+
$this->setValidData($attribute, $value);
107107
} else {
108-
$this->invalidData[$attributeKey] = $value;
108+
$this->setInvalidData($attribute, $value);
109109
}
110110
}
111111

@@ -462,11 +462,31 @@ public function getValidatedData() {
462462
return array_merge($this->validData, $this->invalidData);
463463
}
464464

465+
protected function setValidData(Attribute $attribute, $value)
466+
{
467+
$key = $attribute->getKey();
468+
if ($attribute->isArrayAttribute()) {
469+
Helper::arraySet($this->validData, $key, $value);
470+
} else {
471+
$this->validData[$key] = $value;
472+
}
473+
}
474+
465475
public function getValidData()
466476
{
467477
return $this->validData;
468478
}
469479

480+
protected function setInvalidData(Attribute $attribute, $value)
481+
{
482+
$key = $attribute->getKey();
483+
if ($attribute->isArrayAttribute()) {
484+
Helper::arraySet($this->invalidData, $key, $value);
485+
} else {
486+
$this->invalidData[$key] = $value;
487+
}
488+
}
489+
470490
public function getInvalidData()
471491
{
472492
return $this->invalidData;

tests/ValidatorTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,4 +900,63 @@ public function testSameRuleOnArrayAttribute()
900900
$this->assertNotNull($errors->first('users.1.password_confirmation:same'));
901901
}
902902

903+
public function testGetValidData()
904+
{
905+
$validation = $this->validator->validate([
906+
'items' => [
907+
[
908+
'product_id' => 1,
909+
'qty' => 'invalid'
910+
]
911+
],
912+
'thing' => 'exists',
913+
], [
914+
'thing' => 'required',
915+
'items.*.product_id' => 'required|numeric',
916+
'items.*.qty' => 'required|numeric',
917+
'something' => 'default:on|required|in:on,off'
918+
]);
919+
920+
$validData = $validation->getValidData();
921+
922+
$this->assertEquals([
923+
'items' => [
924+
[
925+
'product_id' => 1
926+
]
927+
],
928+
'thing' => 'exists',
929+
'something' => 'on'
930+
], $validData);
931+
}
932+
933+
public function testGetInvalidData()
934+
{
935+
$validation = $this->validator->validate([
936+
'items' => [
937+
[
938+
'product_id' => 1,
939+
'qty' => 'invalid'
940+
]
941+
],
942+
'thing' => 'exists',
943+
], [
944+
'thing' => 'required',
945+
'items.*.product_id' => 'required|numeric',
946+
'items.*.qty' => 'required|numeric',
947+
'something' => 'required|in:on,off'
948+
]);
949+
950+
$invalidData = $validation->getInvalidData();
951+
952+
$this->assertEquals([
953+
'items' => [
954+
[
955+
'qty' => 'invalid'
956+
]
957+
],
958+
'something' => null
959+
], $invalidData);
960+
}
961+
903962
}

0 commit comments

Comments
 (0)