Skip to content

Commit f091bf6

Browse files
committed
Fix required_unless on array attribute
1 parent f02d8d5 commit f091bf6

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Rules/RequiredUnless.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function check($value)
2323

2424
$anotherAttribute = $this->parameter('field');
2525
$definedValues = $this->parameter('values');
26-
$anotherValue = $this->validation->getValue($anotherAttribute);
26+
$anotherValue = $this->getAttribute()->getValue($anotherAttribute);
2727

2828
$validator = $this->validation->getValidator();
2929
$required_validator = $validator('required');

tests/ValidatorTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,4 +842,38 @@ public function testRequiredIfOnArrayAttribute()
842842
$this->assertNull($errors->first('products.14.notes'));
843843
}
844844

845+
public function testRequiredUnlessOnArrayAttribute()
846+
{
847+
$validation = $this->validator->validate([
848+
'products' => [
849+
// valid because has_notes is 1
850+
'10' => [
851+
'quantity' => 8,
852+
'has_notes' => 1,
853+
'notes' => ''
854+
],
855+
// invalid because has_notes is not 1
856+
'12' => [
857+
'quantity' => 0,
858+
'has_notes' => null,
859+
'notes' => ''
860+
],
861+
// invalid because no has_notes
862+
'14' => [
863+
'quantity' => 0,
864+
'notes' => ''
865+
],
866+
]
867+
], [
868+
'products.*.notes' => 'required_unless:products.*.has_notes,1',
869+
]);
870+
871+
$this->assertFalse($validation->passes());
872+
873+
$errors = $validation->errors();
874+
$this->assertNull($errors->first('products.10.notes'));
875+
$this->assertNotNull($errors->first('products.12.notes'));
876+
$this->assertNotNull($errors->first('products.14.notes'));
877+
}
878+
845879
}

0 commit comments

Comments
 (0)